0

I have created a utility class where I have:

public void waitForScreenToLoad(AndroidDriver  lDriver, WebElement element, int seconds){
    WebDriverWait wait = new WebDriverWait(lDriver,seconds);
    wait.until(ExpectedConditions.visibilityOf(element));
}

and my main class of sign in has:

@Test (priority = 0)
public void SignIn() throws InterruptedException, IOException {
    Thread.sleep(8000);
    MobileElement ele = (MobileElement) driver.findElementByAccessibilityId("abcde");
    MobileElement sign = (MobileElement) driver.findElementByAccessibilityId("Sign in");
    sign.click();
    // Thread.sleep(8000);
    waitForScreenToLoad(driver, ele, 120);
    captureScreenshot(driver,folder_name,df);   
    Thread.sleep(2000);
}

They are both in the same package. Element ele is present on both pages before and after sign in. The wait does not work but, if I use Thread.sleep it works and I am able to take a screenshot.

Can anybody tell me what is wrong with my code? or if using Thread.sleep so frequently is ok to make it work?

Hassan Radi
  • 936
  • 10
  • 22
Manisha
  • 1
  • 3
  • 1
    WebElement's are unique for different pages. You found `ele` on the page before login. Then you login (change/reload page). And `ele` has become **stale**. Therefore you cannot Wait for it this way. You should use another expected condition `visibilityOfElementLocated(By locator)` – Alexey Dolgopolov Mar 28 '18 at 13:31
  • IMO, you have put a rug over the actual issue by mentioning **not working** instead of actual **stale**. Update the question with the complete error stack trace for a proper analysis. – undetected Selenium Mar 28 '18 at 13:35
  • what you mean by not working? `Thread.sleep(x)` will always wait for x seconds no matter what. On the other hand, explicit wait will only wait if the element is not visible (or whatever other condition you desire to happen). If the element is already visible in the beginning, explicit wait won't wait any second. – JACK ZHANG Mar 30 '18 at 00:28
  • guys, thread.sleep works, but still as it is not a best practice i wanted to use webdriver explicit wait – Manisha Mar 30 '18 at 09:10

1 Answers1

1

ele has become stale after clicking and reloading page. Try to use another expected condition.

public void waitForScreenToLoad(AndroidDriver  lDriver, By locator, int seconds)
        {

                WebDriverWait wait = new WebDriverWait(lDriver,seconds);
                wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
          }


@Test (priority = 0)
  public void SignIn() throws InterruptedException, IOException {
            Thread.sleep(8000);
            //MobileElement ele = (MobileElement) driver.findElementByAccessibilityId("abcde");
            By ele_locator = By.AccessibilityId("abcde");
            MobileElement sign = (MobileElement) driver.findElementByAccessibilityId("Sign in");
            sign.click();
           // Thread.sleep(8000);
            waitForScreenToLoad(driver, ele_locator, 120);
            captureScreenshot(driver,folder_name,df);   
            Thread.sleep(2000);
  • Hi Alexey i tried using this but is hows error "The method AccessibilityId(String) is undefined for the type By" , – Manisha Mar 30 '18 at 09:25
  • ok changing it too By ele_locator = MobileBy.AccessibilityId("PB SMB "); removes the error but still the screenshot does not happen at the desired time. Webdriver wait still not working – Manisha Mar 30 '18 at 09:46