WinAppDriver如何在Java中定义WindowsElement而不获取java.lang.ClassCastException

时间:2020-02-22 11:29:40

标签: java winappdriver

我想定义一个WindowsElement以便重新使用它,但是如果我运行它,它将抛出java.lang.ClassCastException:org.openqa.selenium.remote.RemoteWebElement无法转换为io.appium.java_client.windows。 WindowsElement

我遇到了一些WinAppDriver错误,但所有这些错误均被关闭,没有任何有用的信息。

public class WinAppDriverWaitsExample {

    private static WindowsDriver<WebElement> driver = null;
    private static WebElement alarm = null;

    @BeforeClass
    public static void setup() {
        try {       
            DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("app", "Microsoft.WindowsAlarms_8wekyb3d8bbwe!App");             

            driver = new WindowsDriver<WebElement>(new URL("http://127.0.0.1:4723"), capabilities);
            driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);

            //To make sure app is launched
            alarm = driver.findElementByName("Alarm tab");
            Assert.assertNotNull(alarm);

        }catch(Exception e){
            e.printStackTrace();
        } finally {
        }
    }

    @Test
    public void timer() {
        System.out.println("Start and stop the alarm clock");
        //It is AutomationID
         driver.findElementByAccessibilityId("TimerPivotItem").click();
        WindowsElement  startBtn= (WindowsElement) driver.findElementByName("Start");
         WindowsElement  pasueBtn=(WindowsElement) driver.findElementByName("Pause");
         startBtn.click();
         pasueBtn.click();

    }

1 个答案:

答案 0 :(得分:0)

将元素类型更改为WebElement已解决了我的问题。

@Test
    public void timer() {
        System.out.println("Start and stop the alarm clock");
        //It is AutomationID
         driver.findElementByAccessibilityId("TimerPivotItem").click();
         WebElement  startBtn= driver.findElementByName("Start");
         WebElement  pasueBtn=driver.findElementByName("Pause");
         startBtn.click();
         pasueBtn.click();

    }