Monday 13 May 2013

Handle dropdown lists using WebDriver

For Example:Browse: http://apsrtconline.in/oprs-web/

Findout how many vallues in the Concession dropdown list
Select name(APSRTC-RETIRED EMPLOYEE) in the Concession drop-down list

Write the script in the following way:


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class webdriver

{
public WebDriver driver;
 @BeforeMethod
      public void BM()
     {
         //Launch the firefox browser
    driver=new FirefoxDriver();
     }
@Test
     public void Test()
    {
        //Browse the google website
        driver.get("http://apsrtconline.in/oprs-web/");
     
        Select select = new Select(driver.findElement(By.xpath(".//*[@id='concessionId']")));
     
    //finding the number of options
    int i =select.getOptions().size();
   
    //printing the number of options
    System.out.print("number of options ="+i);
   
    // select option in Drop-down using Visible Text
    select.selectByVisibleText("APSRTC-RETIRED EMPLOYEE");
   
   
    }
@AfterMethod

     public void AM()
    {
   //close the firefox window
         // driver.quit();
    }
}

You can select options in the drop-down list based on the index value also:

select.selectByIndex(index value);

1 comment:

  1. Hi Kalyan,

    Thanks for posting dropdown list handling script.
    Useful one.

    ReplyDelete