如果应用程序不支持隐式等待或显式等待,我们该如何放置等待?我在我的情况下使用Thread.sleep()

时间:2019-07-14 04:12:48

标签: selenium appium-desktop

我正在android设备上执行脚本。如果应用程序不支持隐式等待显式等待,我该如何放置等待?我使用的是Thread.sleep()。有人可以给我解决方案吗?

2 个答案:

答案 0 :(得分:2)

“显式等待”不是应用程序的功能,而是Selenium的功能,它位于Appium的内部

enter image description here

因此,您通常应该可以使用WebDriverWaitExpectedConditions类,有关概念和代码段的更多信息,请参见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();
    }
}

答案 1 :(得分:0)

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

 { path: '**', redirectTo:"abc", pathMatch:"full" }
 window.location.href = "wrongPath2/#/"