【问题标题】:How can we put wait if application doesn't support implicit wait or explicit wait? I'm using Thread.sleep() in mycase如果应用程序不支持隐式等待或显式等待,我们如何等待?我在 mycase 中使用 Thread.sleep()
【发布时间】:2019-07-14 04:12:48
【问题描述】:

我正在安卓设备上执行我的脚本。如果应用程序不支持隐式等待或显式等待,我该如何等待?我在我的情况下使用Thread.sleep()。谁能给我一个解决方案?

【问题讨论】:

  • 我在安卓设备上执行脚本。我尝试在不同的条件下使用 webdriver wait 但是对于我的应用程序显式等待和隐式等待不起作用。谁能帮帮我?
  • 任何应用程序或站点都不支持显式和隐式等待,这些是 Selenium 功能。发布不适合您的代码、相关的 html 和完整的错误消息。

标签: selenium appium-desktop


【解决方案1】:

显式等待不是应用程序的功能,它是Selenium 的功能,在Appium 的底层

因此,您通常应该能够使用 WebDriverWait 和 ExpectedConditions 类,有关概念和代码 sn-ps 的更多信息,请参阅 How to use Selenium to test web applications using AJAX technology:

WebDriverWait wait = new WebDriverWait(driver, 10);
MobileElement company = (MobileElement) wait
        .until(ExpectedConditions
                .elementToBeClickable(By
                        .id("usernameTextField")));

带有必要的import statements的完整代码以防万一:

package com.example;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.URL;


public class AppiumTest {

    public static void main(String[] args) throws Exception {

        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setCapability(MobileCapabilityType.APP, "http://d242m5chux1g9j.cloudfront.net/eribank.apk");
        dc.setCapability("platformName", "Android");
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
        dc.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.experitest.ExperiBank");
        dc.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, ".LoginActivity");
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
        dc.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 300);

        AndroidDriver driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), dc);
        WebDriverWait wait = new WebDriverWait(driver, 10);
        MobileElement company = (MobileElement) wait
                .until(ExpectedConditions
                        .elementToBeClickable(By
                                .id("usernameTextField")));
        company.sendKeys("company");

        driver.quit();
    }
}

【讨论】:

  • 感谢您的回答。我知道​​它是硒功能。但它不适用于我在真正的 android 设备上的应用程序。 Webdriver 等等我已经输入了我的代码。它不工作。 IT 不会等待并简单地抛出异常。元素不在此页面上。(即使 Xpath 也是正确的)
【解决方案2】:

这是一项框架功能,与您运行它的应用程序无关。等待需要你告诉它“它应该等待多长时间?”以毫秒为单位,它应该等待什么?

#Time input
WebDriverWait wait = new WebDriverWait(driver, 50000);
#What to wait for
wait.until(ExpectedConditions.elementToBeClickable(By.id("usernameTextField")));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2020-01-20
    • 2017-07-26
    相关资源
    最近更新 更多