Using Explicit Wait in Selenium Webdriver

Again, like i always say in most of my posts, though there are lots of resources available on net for this topic what i discuss here is specific to the issue i faced in my app automation.

Issue – Selecting a menu option which get displayed upon mouse hover. The script was not able to do this always. sometimes it will work & sometimes it wont.

The application has got lot of small widgets on the screen. When the last widget completes loading, the app will refresh itself.

I was using implicit wait. But the problem here was

  1. implicit wait will not find out if all objects on the page is visible or not
  2. The moment it find the page is loaded, it says done.
  3. But the widget was not loading so soon.
  4. By the time the last widget loads, the script goes to next line of code and executes it. in this case, mouse hover the menu
  5. Mouse hover worked, but before a option is selected in the list that drops down, the last widget on the screen will load and app would refresh
  6. So the script was not clicking on the option in the list

This i was able to figure out after closely watching the way the app was behaving for some 2-3 days. Because i could see the mouse hover happening and the list showing up for some nano seconds. But the click was not happening and i was not able to figure out what was the issue. Because the screen refresh that was happening after the widget load was looking to me like as if Selenium script was trying to click on the option but app was failing to do the needful.

Solution:

Explicit wait: How?

  1. First i used it on that culprit widget, making sure that the script will not do anything untill this widget shows up
    WebDriverWait wait=new WebDriverWait(d,20);
    accountsWidget = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(string)));
    
  2. Then i used it on the menu option itself, so that once the mouse hover is done, the script waits for couple of seconds so that the list is displayed completely, before selecting the option inside it
    //locate "Payment" options in the Menu
     paymentsMenuOption = d.findElement(By.xpath(string));
    
     //performing mouse hover action on Payment option and then clicking on it
     Actions action = new Actions(d);
     action.moveToElement(paymentsMenuOption);
     action.click().build().perform();
    
     //Locate the Import Profile Option now, which gets displayed in Payments list.
     //use explicit wait again so that script waits till the payment list is fully visible
     importProfileSubMenuOption = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(string)));
    
     //Performing mouse hover action on Import Profile and then clicking
     action.moveToElement(importProfileSubMenuOption);
     action.click().build().perform();
    

 

 

 

 

 

Leave a comment