Java 如何在开始和测试期间使用 Appium“等待活动”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28799420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:49:59  来源:igfitidea点击:

How to "wait to activity" using Appium, on begin and during test itself?

javaandroidandroid-activityappiumui-testing

提问by Elad Benda2

I'm starting an already installed app using appium.

我正在使用 appium 启动一个已安装的应用程序。

After my driver is initialized. How do I make it poll-wait till certain activity is displayed?

在我的驱动程序初始化之后。如何让它轮询等待直到显示某些活动?

I saw only this way to wait for activity when starting up

我只看到这种在启动时等待活动的方式

cap.setCapability("app-wait-activity", "activity-to-wait-for");

cap.setCapability("app-wait-activity", "activity-to-wait-for");

Is there any other way? How do I wait to another specific activity when not initializing. Say after a button click?

有没有其他办法?未初始化时如何等待另一个特定活动。单击按钮后说?

just sleep x seconds?

只是sleep x seconds

回答by Alex

Specific activity means some specific element is being displayed. I use the following code to wait until some certain element on the screen:

特定活动意味着正在显示某些特定元素。我使用以下代码等待屏幕上的某些元素:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By
        .xpath("//android.widget.Button[contains(@text, 'Log In')]")));

or:

或者:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By
            .xpath("//android.widget.TextView[contains(@resource-id, 'action_bar_title')]")));

回答by Alex

Also you could make use of the following:

您也可以使用以下内容:

getDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

or just:

要不就:

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

or something like the following:

或类似以下内容:

Thread.sleep(5000);

回答by Gaurav

I would suggest you to use WebDriverWait. Thread.sleep() is not a good way to use in your test scripts

我建议你使用 WebDriverWait。Thread.sleep() 不是在测试脚本中使用的好方法

回答by anuja jain

WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me"))); 

If you wish to know in detail how implicit and explicit wait can be used in Appium then visit this TUTORIAL

如果您想详细了解如何在 Appium 中使用隐式和显式等待,请访问此教程

回答by Saharshdeep Singh

It can be done by different ways using element. Webdriver provide “WebDriverWait”, “ExpectedCondition” classes to implement this. ExpectedConditions class provide some set of predefine conditions to wait elements as:

它可以通过使用元素的不同方式来完成。Webdriver 提供“WebDriverWait”、“ExpectedCondition”类来实现这一点。ExpectedConditions 类为等待元素提供了一些预定义条件集:

  1. elementSelectionStateToBe: an element state is selection.
  2. elementToBeClickable: an element is present and clickable.
  3. elementToBeSelected: element is selected
  4. frameToBeAvailableAndSwitchToIt: frame is available and frame
  5. selected. invisibilityOfElementLocated: an element is invisible
  6. presenceOfAllElementsLocatedBy: present element located by.
  7. refreshed: wait for a particular condition when page refresh.
  8. textToBePresentInElement: text present on particular an element
  9. textToBePresentInElementValue: and element value present for a
    particular element. and many more You can learn more ways to implement implicit and explicit wait by going through this url: http://roadtoautomation.blogspot.in/2013/10/webdriver-implicit-and-explicit-wait.html
  1. elementSelectionStateToBe:元素状态为选择。
  2. elementToBeClickable:元素存在且可点击。
  3. elementToBeSelected:元素被选中
  4. frameToBeAvailableAndSwitchToIt:框架可用且框架
  5. 被选中。invisibilityOfElementLocated:元素不可见
  6. PresenceOfAllElementsLocatedBy:当前元素的位置。
  7. refreshed:页面刷新时等待特定条件。
  8. textToBePresentInElement:出现在特定元素上的文本
  9. textToBePresentInElementValue:和
    特定元素的元素值。还有更多您可以通过以下网址了解更多实现隐式和显式等待的方法:http: //roadtoautomation.blogspot.in/2013/10/webdriver-implicit-and-explicit-wait.html

Hope it helps...

希望能帮助到你...

回答by sameeksha sahib

long startTime = System.currentTimeMillis();
while(System.currentTimeMillis() - startTime < Time_Out)
    if (getDriver().currentActivity().equals(activity))
        break;

回答by shiv

You can use the following code to poll the current activity every second. If you want to reduce polling time you can reduce sleep time to 500 and wait*2:

您可以使用以下代码每秒轮询当前活动。如果你想减少轮询时间,你可以将睡眠时间减少到 500 并且wait*2

public void waitForActivity(String desiredActivity, int wait) throws InterruptedException
{
    int counter = 0;
    do {
        Thread.sleep(1000);
        counter++;
    } while(driver.currentActivity().contains(desiredActivity) && (counter<=wait));

    log("Activity appeared :" + driver.currentActivity(), true);
}