明确等待使用winappdriver自动执行Windows应用程序

时间:2018-12-23 08:56:18

标签: wait winappdriver

我是Windows应用程序驱动程序的新手,并且我的项目要求自动化桌面应用程序,所以我决定使用winappdriver,因为它与硒类似,我对此很有信心。

谈到这个问题, 只是想知道是否有一种方法可以使用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!!!!");
    }

1 个答案:

答案 0 :(得分:0)

当然可以,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自动化的详细课程。我将在几天后发布。让我知道您是否有兴趣:)