0

I need help about awaiting for element in some test case. In one of the tests I check the accuracy of the phone number on the dialer. The dialer opens after I press a button from our application. The problem is that to return from the dialer back to the app, I have to press the return key several times, but the number of times can vary from device to device.

So what I did is actually check to see where I am now. Take a look at the code below. The problem is that if the element is not found, then the

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

function comes into action - and then I have to wait 10 seconds for it to end.

What would you advise me to do to prevent the system from waiting this time.

Test:

@Test
    @Description("Verify that the dialer is opens on click pop up button and verify that the called number is correct")
    public void checkDialerNumbers() {
        **//For loop to to run on all the services.**
        for (int i = 0; i < topServices.getTopServiceComponents().size(); i++) {
            **//Click on the (i) service.** 
            topServices.getTopServiceComponents().get(i).click();
            **//Verify that the Service pop up opens.**
            wait.until(ExpectedConditions.visibilityOf(popUp.getClosePopUp())).isDisplayed();
            **//Open dialer according to (i) service.**
            switch (i) {
                case 0:
                    **//Open dialer**
                    topServices.getIWantToCall().click();
                    **//Wait to dialer and verify that it opens.**
                    wait.until(ExpectedConditions.visibilityOf(dialerUtils.getCallButton())).isDisplayed();
                    **//Assertion - Called number match to the Expected number.**
                    Assertions.assertEquals(topServices.getShagrirPhoneNumber(), dialerUtils.getDialedNumbersField().getText());

                    WebDriverWait w = new WebDriverWait(driver(), 3);

                    **//Here I'm creating for loop, that will check if the pop up is displayed - if displayed  > dialer closed.
                    //If displayed = making j to 10, to exit from the loop.
                    //if not displayed , so I pressing back in CATCH and going to loop again .**
                    for (int j = 0; j < 10; j++) {
                        try {
                            if (w.until(ExpectedConditions.visibilityOf(popUp.getClosePopUp())).isDisplayed()) {
                                System.out.println("Dialer closed");
                                j = 10;
                            }
                        } catch (TimeoutException TE) {
                            System.out.println("Pressing back again " + j);
                            driver().navigate().back();
                        }
                    }
                    break;
                case 1:
                    //TODO Verify numbers in this service
                    break;
                case 2:
                    //TODO Verify numbers in this service
                    break;
            }
        }
        popUp.getClosePopUp().click();
    }

Await func in driver.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Michael
  • 429
  • 5
  • 22

1 Answers1

0

Don't use it, it's bad practice to use implicitlyWait with WebDriverWait anyway. It can produce unexpected results, WebDriverWait has time span of 3 seconds but the implicit wait will make it wait 10 seconds.

You can use ExpectedConditions.presenceOfElementLocated to replace the implicit wait, create a general method to avoid writing it over and over

public WebElement waitForPresenceOfElementLocated(By locator) {
    return wait.until(ExpectedConditions.presenceOfElementLocated(locator));
}
Guy
  • 46,488
  • 10
  • 44
  • 88