0

我是 Windows Application Driver 的新手,我的项目需要自动化桌面应用程序,所以我决定使用 winappdriver,因为它类似于 selenium,我对使用它很有信心。

说到这个问题,只是想知道是否有一种方法可以使用winappdriver实现显式等待和隐式等待。以下是我在测试用例中使用的代码,测试失败并出现异常(NoSuchElementException),但是,如果我放置静态等待而不是显式等待,它会按预期工作。

 //Driver Setup
public class OscBase {

public static WindowsDriver<WebElement> applicaitonSession, driver = null;
public static WindowsDriver<RemoteWebElement> desktopSession = null;
public static DesiredCapabilities capabilities, cap1, cap2;
public static ProcessBuilder pBuilder;
public static Process p;

public void startDriver() {
    try {

        pBuilder = new ProcessBuilder("C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe");
        pBuilder.inheritIO();
        p = pBuilder.start();

    }

    catch (IOException e) {
        e.printStackTrace();
    }
}

public void stopDriver() {

    p.destroy();
}

public void createDesktopSession() throws MalformedURLException {

    cap1 = new DesiredCapabilities();
    cap1.setCapability("app", "Root");
    desktopSession = new WindowsDriver<RemoteWebElement>(new URL("http://localhost:4723"), cap1);
}

public void openApplication() throws InterruptedException, MalformedURLException {

    if (driver == null) {

        try {

            capabilities = new DesiredCapabilities();
            capabilities.setCapability("app",
                    "Appnamewithlocation");
            applicaitonSession = new WindowsDriver<WebElement>(new URL("http://localhost:4723"),
                    capabilities);

        } catch (Exception e) {

            System.out.println("Application opened!!!");

        } finally {

            createDesktopSession();
        }

        Thread.sleep(8000L);

        String handle = desktopSession.findElementByAccessibilityId("InstallerView5")
                .getAttribute("NativeWindowHandle");
        System.out.println(handle);
        int inthandle = Integer.parseInt(handle);
        String hexHandle = Integer.toHexString(inthandle);

        //System.out.println(hexHandle);

        cap2 = new DesiredCapabilities();
        cap2.setCapability("appTopLevelWindow", hexHandle);
        driver = new WindowsDriver<WebElement>(new URL("http://localhost:4723"), cap2);

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

    }

}


public boolean isDisplayed_SafeLoginNoBtn() {

    wait = new WebDriverWait(driver, 40);
    return wait.until(ExpectedConditions.visibilityOf(safeLoginNoBtn())).isDisplayed();

}




@Test
public void osc_Get_Data() throws InterruptedException, IOException {

    //Thread.sleep(20000);

    // Boolean value=oscLogin.safeLoginNoBtn().isDisplayed();

    try {
        Boolean value = oscLogin.isDisplayed_SafeLoginNoBtn();
        System.out.println("IS displayed========>"+value);
        if (value) {

            oscLogin.click_safeLogin();
        }
    } catch (Exception e) {

        System.out.println("Safe Login!!!!");
    }
4

1 回答 1

-1

当然是的,WebDriverWait 类可以工作。这是一个例子

WebDriverWait waitForMe = new WebDriverWait();
WebDriverWait waitForMe = new WebDriverWait(session, new TimeSpan.Fromseconds(10));
var txtLocation = session.FindElementByName("Enter a location");
waitForMe.Until(pred => txtLocation.Displayed);

我创建了一个关于使用 WinAppDriver 和 C# .Net 的 UI 自动化的详细课程。我会在几天后发布它。如果您有兴趣,请告诉我:)

于 2019-08-30T14:30:00.983 回答