Home > Enterprise >  Eclipse-Selenium-Alternative to Thread.sleep()
Eclipse-Selenium-Alternative to Thread.sleep()

Time:03-02

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:

jse.executeScript("arguments[0].click();", button_Sign).toString().equalsIgnoreCase("complete");

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:

  public void SignSupportFormOnOrder() throws InterruptedException {
        JavascriptExecutor jse = (JavascriptExecutor)GlobalVariables._browser.currentDriver;
        ExplicitWait.until(ExpectedConditions.visibilityOf(Order_SignSupportingForms));
        actions.moveToElement(Order_SignSupportingForms).click().perform();
        
        //Code working but using Thread.sleep():
        //Thread.sleep(10000); //Need to look for an alternative solution
        //jse.executeScript("arguments[0].click();", button_Sign);
        /////////////////////////////////////////////////////////////////////////////////////////
        
        //try as alternative to Thread.sleep(...):
        
        for(int i=0; i < 5; i  ){   
            if(jse.executeScript("arguments[0].click();", button_Sign).toString().equalsIgnoreCase("complete"))
                break;
            else
                Thread.sleep(1000);
            }

        //Thread.sleep(10000);
    }

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

CodePudding user response:

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

public boolean clickVisibleDisappearsLoop(String xpath1, String xpath2){
    waitForElementToBeVisible(xpath1);
    int counter = 0;
    while (counter<10) {
        try {
            clickOnElement(xpath1);
            waitForElementToDisappear(xpath2,4);
            return true;
        } catch (Throwable throwable) {
            wait(400);
            counter  ;
        }
    }
    return false;
}

Methods I have used here are:


public void wait(int delay) {
    Uninterruptibles.sleepUninterruptibly(delay, TimeUnit.MILLISECONDS);
}

public boolean waitForElementToDisappear(String xpath){
    try {
        wait.until((ExpectedConditions.invisibilityOf(By.xpath(xpath))));
        return true;
    }catch (Throwable t){
        return false;
    }
}

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.

  • Related