UiObject clickAndWaitForNewWindow加载时间太长

时间:2014-10-05 06:05:53

标签: android uiautomator

在我们的uiautomator测试用例中,我们使用UiObject.clickAndWaitForNewWindow来触发任何点击操作。然而,有时只需要设备响应/加载页面就需要很长时间。因此,在页面完全加载之前,任何后续分析/测试都会失败,因为预期的小部件尚未准备就绪。这使我们的一些测试用例以不确定的方式失败。大部分时间只是重新运行他们通过的测试用例。

有没有办法确保UiObject.clickAndWaitForNewWindow在继续进行下一次验证之前完全加载预期的页面?我们可以在UiObject.clickAndWaitForNewWindow之后插入一个睡眠语句,它可以作为双刃剑使用,但这并没有真正解决这个问题。

UiObject.clickAndWaitForNewWindow();
// how to wait until the page is loaded completely
new UiSelector(...); // search for widgets that are supposed to be on the newly loaded page

2 个答案:

答案 0 :(得分:0)

这里的一种方法是在这里添加一个while循环。

假设您在一个窗口中点击某个元素然后出现一个新屏幕,并且您想要在新窗口中对某个元素(比如菜单)执行操作,并且该元素定义为 -

//say you tapped on some element(say abc) on the first window
abc.click();

//now you want to wait till the element (say menu) appears on the next screen which is decl as -
UiObject menu =  new UiObject(new UiSelector().resourceId("com.example.test:id/elmnt"));

//add a while loop to check if that element exists and add a print statement just to keep printing till that element appears on the screen-
while (!menu.exists()) {

    System.out.println("Menu will appear in some time");

}

System.out.println("Menu appeared");
//carry other operations here

答案 1 :(得分:0)

我正在使用

UiDevice.waitForWindowUpdate(package, timeout);

到目前为止它工作正常。有了这个,你不需要任何元素的id。我认为也可以选择不超时。