How to Handle GWT suggestions using Selenium?

It is a very common pattern now a days that many web application are being developed using Google Web Toolkit and it can be very challenging to test such applications sometimes using selenium. For instance, last few days, I was struggling to go past a scenario where the suggest box of one field was masking another WebElement. For this reason the other WebElement was not getting located by Selenium and my test was failing. After many hit and trials, I found a way to solve this problem. The explanation of the solution goes as below.

Problem: 

Let’s assume that, there is text box called with id “city_id”. Below the field there is button with id “find”. While we type some city name in the city_id field, it suggest some cities with matching character. Then you select a city and click on the find button, you will displayed some result. Now the problem is, you type the whole city name and do and findElement for the find button. Since the suggestion box is open, selenium is unable to find the button and the test fails.

Solution:

Here, after doing a sendKeys operation with the city name to the city_id field, we need to do another sendKey operation to press keyboard enter. Now the button becomes visible and selenium can happily find the same. Below is the sample code on how to do it.


driver.findElement(By.xpath("//*[@id='city_id']")).sendKeys("Bangalore");
driver.findElement(By.xpath("//*[@id='city_id']")).sendKeys(Keys.Enter);
driver.findElement(By.xpath("//*[@id='find']")).click();

Advertisements