Skip to content
Advertisement

Eclipse-Selenium-Alternative to Thread.sleep()

I am relatively new to the Test Automation and in particular to the Selenium. I am using Selenium Web-driver, Eclipse. One of the biggest problems I am experiencing is that our scripts are crashing due to the Internet speed or server performance. I currently use Thread.sleep() to solve the problem. However it is not a secret that Thread.sleep() enforces waiting time till the timer is completed. I tried to find an optimised solution in example for the script below. Having looked through the posts on Stackoverflow I found solution which is provided below in the for() loop. It was suggested to use:

JavaScript

to ensure that action (click on the button) is completed, and if not, so to wait a little bit and to try again during the next iteration.
In my case below there would be 6 attempts as per loop. However the code brings up an error:

“Failure: Cannot invoke “Object.toString()” because the return value of “org.openqa.selenium.JavascriptExecutor.executeScript(String, Object[])” is null”

The code is:

JavaScript

Can somebody kindly give me a suggestion what I am doing wrong and how it would be possible to overcome using Thread.sleep();

Advertisement

Answer

Since clicking the button_Sign is closing some pop-up we can do some code like the following.

JavaScript

Methods I have used here are:

JavaScript

Here you can pass the XPath locator of button_Sign element as xpath1 and the XPath locator of pop-up that should close by clicking on button_Sign as xpath2.
Also, accordingly to Java conventions you should name the button_Sign as buttonSign. I would suggest naming it as signBtn or signButton.
Also, you dont have to return Booleans here, so you can simplify these methods making them void.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement