gpt4 book ai didi

android - Appium+Selenium Android : ListView item not clicked without Thread. 休眠

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:03 24 4
gpt4 key购买 nike

我讨厌在测试中使用“ sleep 者”(Thread.sleep(millis)),但如果没有 sleep 者,一些测试就会失败。

我的 Android 应用程序中有一个 ListView,我想点击列表中的第一项(在我们的例子中是沙特阿拉伯)。

Countries ListView

 public AndroidDriver androidDriver;
...
androidDriver = new AndroidDriver(serverAddress, capabilities);
androidDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driverWait = new WebDriverWait(androidDriver, 30);
// at this moment everything is initialized and working properly,
// Appium server is up and running
driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.###.debug:id/countries_list_view")));
WebElement countriesList = driver.findElement(By.id("com.###.debug:id/countries_list_view"));
List<WebElement> countries = countriesList.findElements(By.id("com.###.debug:id/list_item_container"));
WebElement country = countries.get(0);
// country isn't null, and it corresponds to a real ListView row
driverWait.until(ExpectedConditions.elementToBeClickable(country));
Thread.sleep(1000); // <---- country isn't clicked without this
country.click();

Calabash/Cucumber 测试中存在同样的问题(需要显式等待)。

我已经尝试了不同的方式来等待应该被点击的元素

  • driverWait.until(ExpectedConditions.visibilityOfElementLocated(By));
  • driverWait.until(ExpectedConditions.visibilityOf(WebElement));
  • driverWait.until(ExpectedConditions.elementToBeClickable(By));
  • driverWait.until(ExpectedConditions.presenceOfElementLocated(By));

而且没有一个在工作。当我尝试点击 ListView 第一项时,ListView 存在并且所有列表元素都在屏幕上。

我试图通过使用 Appium Inspector 获取列表行 XPath 来找到 ListView 第一行。结果是一样的 - 没有 Thread.sleep 就不会点击 View 。

在测试中使用 Thread.sleep 是非常糟糕的做法,并且会使我的测试不稳定。在这种情况下,我不能依赖测试结果,因为即使应用程序正常运行,它们也可能会失败。有一篇关于在 Selenium 测试中使用“等待”和“ sleep ”的文章 (Selenium WebDriver wait)。

  1. 如何在测试中解决此类问题?
  2. Thread.sleep 调用在自动化领域的频率如何? (我主要是 Android 开发人员,在移动自动化方面经验不足)。

更新:我尽量不混淆隐式和显式等待,如 JeffC提到了,但没有帮助。

这是我的测试:

@Test
public void selectCountryLanguageAndStartApplication() throws Exception {
countryLanguagePage.loaded();
countryLanguagePage.selectFirstCountry();
countryLanguagePage.pleaseSelectCountryTextIsHidden();
countryLanguagePage.startClick();
}
...
/**
* Verify the page has loaded
*/
public static void loaded() {
driver.findElement(By.id("com.###.debug:id/countries_list_view"));
}

我正在验证是否在每次测试中都加载了页面。如果我只使用隐式等待——测试有时会失败;如果我只使用显式等待 - 这是一样的,测试有时会失败。

我在 Appium tutorial 中找到了他们使用 implicit结合明确的1 , 2 .根据 docs 看起来很奇怪.

工作解决方案:我修改了一点loaded方法

public static void loaded() {
driver.findElement(By.id("com.###.debug:id/countries_list_view"));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

sleep 带来了测试的“稳定性”,我可以找到元素并在明确等待或不等待它们的情况下按下它们。

这是否意味着我应该在新的 Activity 启动时添加“ sleep ”(对我来说唯一可行的解​​决方案)?或者我正在以错误的方式等待 Activity 初始化?

最佳答案

你可以试试这个:

  WebDriverWait wait = new WebDriverWait(driver, 30); 
wait.until(ExpectedConditions.elementToBeClickable(By));

while(!driver.findElement(By).isDisplayed())
{
//Thread.sleep(10);
}

我认为 Thread.sleep 调用很好用(如果您的测试涉及计时,这几乎是必需的)但在大多数情况下,有更好的方法来处理它们将用于的大多数事情。

希望对您有所帮助,

利亚姆

关于android - Appium+Selenium Android : ListView item not clicked without Thread. 休眠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33611579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com