Selenium script to add Custom Field Options in JIRA.

Hello Friends,

This is one of my tutorials regarding the use of Selenium script in the Jira Configuration. Most of the Jira users may know about the Cascading Type of the Custom fields, there we need to add multiple options against the single one. Now, doing this configuration manual y is really a time consuming job. So, to avoid this, we can use the Selenium Script. All the manual work will be replaced by a single script and it will do everything for you in a very less time.

To use this script you need to have some basic knowledge of the Selenium IDE. I have explained the same in my previous articles, please go through it before implementing this. Here, we will be reading the custom fields options from the simple xml file, where the first Option will represent the Main Category and the remaining will be its sub-categories. The XML file will be as:

The folder structure is explained in the previous article.

Now, we need to add the java file which also includes the code to read the data from XML along with the Selenium script code. You can get the code using the Selenium IDE from Firefox too. I have already discussed this in my articles earlier.

Following is the java code for adding the Options to custom field.

[java]

package com.code2java.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();
}
}

[/java]

Hope this will help you. Please share your thoughts on this or write me at admin@code2java.com

Regards,

Nikhil Naoghare

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.