Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in DevOps and Agile by (47.6k points)

I'm automating the execution of test cases using Java and Selenium WebDriver. Below is the scenario:

 

There is a page named 'Products' and When I click on 'View Details' in the 'Product' page, a popup (modal-dialogue) containing the details of the items appears.

On clicking the 'Close' button in the popup, the popup closes and the page refreshes automatically (the page is just reloading, the contents remain unchanged).

 

After closing the popup, I need to click on the 'Add Item' button on the same page as part of the test. But, when WebDriver is trying to find the 'Add Item' button, and if the internet speed is good, then webdriver can find and click the element.

 

But in case the internet speed is bad or slow, then WebDriver finds the button before the page is refreshed, but right when webdriver clicks on the button, the page gets refreshed and StaleElementReferenceException occurs.

 

Even if different waits are used, all the wait conditions become true (since the contents in the page are the same before and after reloading) even before the page is reloaded and StaleElementReferenceException occurs.

The test case works perfectly if Thread.sleep(); is used before clicking on the 'Add Item' button. But that's not correct usage. Is there any other workaround for this problem?

1 Answer

0 votes
by (106k points)

There are 3 types of wait statements. The first one you can use either Implicit waits, the second is explicit waits, and the last one is fluent waits to wait until the page reloads. So, in your case, you can use implicit waits: driver.manage().timeouts().implicitlyWait() is the command. 

As you are asking to wait for the page to be reloaded before clicking on the "Add" button, for that you will need to wait for the "Add Item" element to become stale before clicking on the reloaded element.

You can use the below-mentioned code:-

driver.manage().timeouts().implicitlywait(30 timeunit.seconds).

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...