Search code examples
javawinappdriver

WinAppDriver how to define a WindowsElement in Java without getting java.lang.ClassCastException


I want to define a WindowsElement so I can reuse it , but if I run it , it throws java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to io.appium.java_client.windows.WindowsElement

I went though some WinAppDriver bugs ,but all were closed without any useful information.

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();

    }

Solution

  • Change the element type to a WebElement had solved my issue .

    @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();
    
        }