package com.come2niks.jira;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
import java.io.File;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class CascadingOptions extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://localhost:8080/");
selenium.start();
}
@Test
public void testCascadingOptions() throws Exception
{
try
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("book.xml"));
doc.getDocumentElement().normalize();
NodeList listOfOptions = doc.getElementsByTagName("option");
int totalOptions = listOfOptions.getLength();
selenium.open("/jira/login.jspa"); //Home page of your Jira Application
selenium.click("link=Log In");
selenium.waitForPageToLoad("30000");
selenium.type("id=login-form-username", "jira_admin_user");
selenium.type("id=login-form-password", "jira_password");
selenium.click("id=login-form-submit");
selenium.waitForPageToLoad("30000");
selenium.click("id=admin_link");
selenium.waitForPageToLoad("30000");
selenium.click("id=view_custom_fields");
selenium.waitForPageToLoad("30000");
selenium.type("id=login-form-authenticatePassword", "jira_password");
selenium.click("id=authenticateButton");
selenium.waitForPageToLoad("60000");
//This link may vary. Please check for this link. here 12310 is the custom field ID.
selenium.open("/jira/admin/ConfigureCustomField!default.jspa?customFieldId=12310");
selenium.waitForPageToLoad("30000");
for(int x=0; x<listOfOptions.getLength() ; x++)
{
Node firstOptionNode = listOfOptions.item(x);
try
{
String nodeNames = "one,two,three,four,five,six,seven,eight,nine,ten";
String[] listNodes = null;
listNodes = nodeNames.split(",");
for(int i=0;i<listNodes.length;i++)
{
selenium.click("link=Edit Options");
selenium.waitForPageToLoad("30000");
if(firstOptionNode.getNodeType() == Node.ELEMENT_NODE)
{
Element firstNodeElement = (Element)firstOptionNode;
NodeList firstOptionList = firstNodeElement.getElementsByTagName(listNodes[i]);
Element firstOptionElement = (Element)firstOptionList.item(0);
NodeList textList = firstOptionElement.getChildNodes();
String nameOfOption = ((Node)textList.item(0)).getNodeValue().trim();
System.out.println("The name of Category is : "+nameOfOption);
String[] options = null;
options = nameOfOption.split(",");
boolean flag = true;
for(int j=0;j<options.length;j++)
{
System.out.println("The Values of Options "+j+" are "+options[j].trim());
if(j ==0)
{
selenium.type("name=addValue", options[0]);
selenium.click("id=add_submit");
selenium.waitForPageToLoad("30000");
}
else
{
if(flag)
{
selenium.select("css=select", "label="+options[0]);
selenium.waitForPageToLoad("30000");
}
selenium.type("name=addValue", options[j]);
selenium.click("id=add_submit");
selenium.waitForPageToLoad("30000");
flag = false;
}
}
selenium.click("css=input[type="button"]");
selenium.waitForPageToLoad("30000");
}
}
}
catch(Exception e)
{
//You can use the Logger else.
System.out.println(" EXCEPTION :"+ e.getMessage ());
}
}
selenium.click("link=Access more options");
selenium.click("id=log_out");
selenium.waitForPageToLoad("30000");
}
catch (SAXParseException err)
{
System.out.println ("** Parsing error" + ", line "+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}
catch(SAXException ex)
{
Exception x = ex.getException ();
((x == null) ? ex : x).printStackTrace ();
}
catch (Throwable t)
{
t.printStackTrace ();
}
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
}