How to select options in five list boxes without repetiton of code(Web element- list box)?

There are 5 list boxes in which i have selected 1 options each:

WebElement listBox=driver.findElement(By.id("country"));//click on country text box
Select select1=new Select(listBox);
select1.selectByVisibleText("India");//select india

WebElement listBox1=driver.findElement(By.id("state"));//click on state text box
Select select2=new Select(listBox1);
select2.selectByVisibleText("delhi");//select delhi

WebElement listBox2=driver.findElement(By.id("district"));//click on district text box
Select select3=new Select(listBox2);
select3.selectByVisibleText("NCR");//select NCR

WebElement listBox3=driver.findElement(By.id("block"));//click on block 
Select select4=new Select(listBox3);  
select4.selectByVisibleText("Block1");//select Block1

WebElement listBox4=driver.findElement(By.id("village"));//click on village text box
Select select5=new Select(listBox4);        
select5.selectByVisibleText("south");//select south

the above code is too long but i have to reduce code length by combining the code for list boxes.

Answers


Create a method to find the dropdown and select an option

public void chooseFromDropdown(By locator, String text) {
    WebElement listBox = driver.findElement(locator);
    Select select = new Select(listBox);
    select.selectByVisibleText(text);
}

And call it like this

chooseFromDropdown(By.id("country"), "india");
chooseFromDropdown(By.id("state"), "delhi");
// ...

You can create the element once like:-

WebElement dropboxelement=driver.findElement(By.id("country"));

Select select1=new Select(dropboxelement);
select1.selectByVisibleText("India");

dropboxelement = driver.findElement(By.id("state"));
select1.selectByVisibleText("delhi");

So on ..

Have a try

Hope it will help you :)


What I would advise is, use a framework for your selenium tests like Cucumber or Specflow. Sure, it will take some learning, but a lot more efficient than using Selenium separetly. In this way, your test would look like this:

When user selects items in dropdownbox
| dropdownboxID| selectOption|
| country      | india       |
| state        | delhi       |
| district     | NCR         |
| block        | Block1      |
| village      | south       |

Once you have made the table, in the code (in this case C#) define the following code:

[When(@"user selects items in dropdownbox")]
public void UserSelectsItemsInDropdownbox(Table table){
    var parameters = table.CreateSet<Parameters>();

    foreach (var parameter in parameters) {
           var dropdownbox = Driver.FindElement(By.Name(dropdownboxID));
           var selectElement = new SelectElement(dropdownbox);
           selectElement.SelectByText(parameters.selectOption);
    }

}

This way, thats the only code you need. The rest of your data can be inserted through specflow. You need to define your parameters in a different class in this case, I named my class Parameters.cs


To iterate with multiple parameters:

String[] items = new String[] {
    "country", "India",
    "state", "delhi",
    "district", "NCR",
    "block", "Block1",
    "village", "south",
};

for (int i = 0; i < items.length; i += 2) {
    String name = items[i];
    String text = items[i + 1];
    WebElement element = driver.findElement(By.name(name));
    new Select(element).selectByVisibleText(text);
}

Need Your Help

Rails ajax background requests

jquery ruby-on-rails ruby rjs jrails

I have a remote_form which works 100%

asp.net template

asp.net html css

Where i can to buy quality web template for asp.net. Thanks.