1

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();

pringi
  • 3,987
  • 5
  • 35
  • 45
Vladislav
  • 121
  • 1
  • 9
  • 1
    [How to wait until an element is present in Selenium?](https://stackoverflow.com/a/20903328/1518100) – Lei Yang Feb 28 '22 at 14:40
  • 1
    I there some other, more clean identification for the success of click on `button_Sign` element? Can you wait for some element appearance of disappearance instead of `toString().equalsIgnoreCase("complete")`? Why don't you use a regular selenium `.click()` or even actions click methods there? – Prophet Feb 28 '22 at 15:00
  • Thank you for the answer. Yes, I used "button_Sign.click();" as you suggested. It used to work, but at some point started to generate an error, so, I suggested an alternative solution: "jse.executeScript("arguments[0].click();", button_Sign)" – Vladislav Feb 28 '22 at 15:21
  • 1
    You didn't answer my first and main question: what is the indication of `button_Sign` element click success? That the click is worked or not? Some UI changes? Also please tag me so I could know you answered me. – Prophet Feb 28 '22 at 15:56
  • @Prophet , Hello, thank you for the prompt answer again., button_Sign element click success: It's a window that pops up to confirm that the user wants to complete the action. When button Sign is clicked the pop is disappearing. – Vladislav Feb 28 '22 at 16:10

2 Answers2

1

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.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • 1
    I'm happy I could help, you are welcome. As about naming - I understand – Prophet Mar 01 '22 at 16:07
  • Thank you very much for your time, effort and explanations. Your code is working absolutely fine for me. Regarding button_Sign format (or lack of format to be more precise) I completely agree. But it was not me who suggested this name I just continue to maintain the old code. Your help was really invaluable. Thank you again. – Vladislav Mar 01 '22 at 16:09
  • You are right. I upvoted. Is it only -1, 0, 1 points? – Vladislav Mar 01 '22 at 20:51
  • 1
    Upvote gives +10, accept +15, downvote -2 – Prophet Mar 01 '22 at 21:32
  • I see. Thank you. Will it be possible to ask you questions about Selenium in the future? – Vladislav Mar 02 '22 at 10:30
  • 1
    Sure. You can ask new questions and tag me in a comment – Prophet Mar 02 '22 at 10:32
0

You can use Explicit wait with any element not appeared and mentioned needed time for it , for Ex:

WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("ElemnetNotExist")));
  • @Prophet Hello, I remember you kindly helped me before. I have a new question https://stackoverflow.com/questions/72144555/eclipse-selenium-extract-the-value-from-the-table. If you could have a look please. Of course if you have time. Thank you in advance. – Vladislav May 06 '22 at 16:32