30
Jul

Selecting drop down contents which auto-refresh on previous value selection in selenium

Many a times our test selection impacts the value to be displayed in subsequent fields. eg. on selection the State as “Karnataka” the city drop down is refreshed to reflect cities in Karnataka state.

In order to select such self-refreshing web elements using selenium following options are should work:

#1. Insert thread.sleep between calls

Though this is straight forward and handles auto-refresh elements, this has side effect on test execution timeline.

#2. Use ExpectedConditions wait as follow
WebDriver driver;
WebDriverWait wait = new WebDriverWait(driver, 20);

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“http://www.google.co.in”);
By addItem = By.name(“q”);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));

element.sendKeys(“Hello google”);

#3. Use lambda expression to wait for an element in selenium as follow:

Predicate myPredicate = x -> x.findElement(By.xpath(“//input[@name=’btnK’]”)).isDisplayed();
wait.until(myPredicate);

and then click on webelement
driver.findElement(By.xpath(“//input[@name=’btnK’]”)).click();