我创建了一个实用工具类,我有:
public void waitForScreenToLoad(AndroidDriver lDriver, WebElement element, int seconds){
WebDriverWait wait = new WebDriverWait(lDriver,seconds);
wait.until(ExpectedConditions.visibilityOf(element));
}
我的主要登录类有:
@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);
}
它们都在同一个包装中。登录前后两个页面上都会显示元素ele
。等待不起作用,但是,如果我使用Thread.sleep
,它可以正常工作,我可以截取屏幕截图。
有人能告诉我我的代码有什么问题吗?或者如果频繁使用Thread.sleep
可以使它工作吗?
答案 0 :(得分:1)
ele
已变为陈旧。
尝试使用其他预期条件。
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);