0

Please help me in selecting a random country name from country name dropdown each time running the script.

I want to try using xml.

XPATH of dropdown is .//*[@id='intselect']

HTML code:
<select id="intselect" name="intselect" onchange="setCurrency(this);">
<option value="US">UNITED STATES</option>
<option value="AG">ANTIGUA AND BARBUDA</option>
<option value="AR">ARGENTINA</option>
<option value="AW">ARUBA</option>
<option value="AU">AUSTRALIA</option>
<option value="AT">AUSTRIA</option>
<option value="BH">BAHRAIN</option>
<option value="BD">BANGLADESH</option>
</select>

countryname.xml

<?xml version="1.0" encoding="UTF-8"?>

<array name="testArray">

<country>
<countryname>UNITED STATES</countryname>
</country>
<country>
<countryname>ANTIGUA AND BARBUDA</countryname>
</country>
<country>
<countryname>ARGENTINA</countryname>
</country>
<country>
<countryname>ARUBA</countryname>
</country>
<country>
<countryname>AUSTRALIA</countryname>
</country>
</array>

//Method to fetch random value from XML

public void Fetch_XML()
      {

             SAXBuilder builder = new SAXBuilder();
             File xmlFile = new File("C:\\Documents and Settings\\vlakshm\\MyTNG\\list\\countrynames.xml");
             Element node = null;
             try {

              //Element result=null;
            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren("country");

            Random random = new Random();
            int newcountryname= random.nextInt(list.size());

            node = (Element) list.get(newcountryname);

             }//End of Try loop
             catch (IOException io) {
            System.out.println(io.getMessage());
             } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
             }
             node.getChildText("countryname");
             //element_array = driver.findElement(By.xpath("//select[@id='intselect']/option"));
         }//End of randomPartnum method

// I m calling that method to chose in drop down

public void Choser() {
      Fetch_XML();
      driver.findElement(By.id("intselect")).click();      
        System.out.println("---------------------------------------");
        System.out.println("Country choser layer test case-Success");
        System.out.println("---------------------------------------");

  } 

but m getting null pointer exception.Can anybody help me to sort out the problem in code

user2353517
  • 31
  • 1
  • 2
  • 7

2 Answers2

0

In your java/selenium test script

  1. Get test data array length
  2. Generate a random number in the range 0-LengthOfArray-1 (Generate Random Number in a given Range)
  3. Use the above number to select a country from test data array
Community
  • 1
  • 1
Sumiya
  • 317
  • 4
  • 5
  • I have tried using following code.But its not selecting value. – user2353517 Jun 11 '13 at 05:36
  • Not sure if I understand your logic. Can you take a look here (http://stackoverflow.com/questions/6430462/how-to-select-get-drop-down-option-in-selenium-2). Try to select one country option by label without using your test data xml -- select.selectByVisibleText("ARGENTINA");. – Sumiya Jun 11 '13 at 06:03
0

This works for me:

private void randomSelect(String id) {
    WebElement webElement = driver.findElement(By.id(id));

    Select select = new Select(webElement);

    List<WebElement> selections = select.getOptions();

    int index = (int)( Math.random() * selections.size());

    select.selectByIndex(index);
}
alrutherford
  • 432
  • 5
  • 8