Search code examples
seleniumappiumappium-androidjava-client

Appium code not working after updating java-client and selenium version


I have a piece of code that works fine with java-client(5.0.0-BETA6) and selenium-java version 3.3.1. But when i upgraded to java-client version 6.1.0 and selenium-java version 3.14.0, the code started throwing errors. Need help in fixing the issue.

     AndroidDriver<AndroidElement> driver=Base.capabilities();
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     TouchAction t=new TouchAction(driver);
     driver.findElementByXPath("//android.widget.TextView[@text='Views']").click();
     driver.findElementByXPath("//android.widget.TextView[@text='Date Widgets']").click();
     driver.findElementByAndroidUIAutomator("text(\"2. Inline\")").click();
     driver.findElementByXPath("//*[@content-desc='9']").click();

     t.press(driver.findElementByXPath("//*[@content-desc='15']")).waitAction(2000).moveTo(driver.findElementByXPath("//*[@content-desc='45']")).release().perform();

The error i am getting is:

  1. TouchAction is a raw type. References to generic type TouchAction should be parameterized"

  2. The method press(PointOption) in the type TouchAction is not applicable for the arguments (AndroidElement)

Any help in resolving this will be appreciated. Thanks.

The code that finally worked for me with new version is mentioned below. Had to make quite a few changes.

AndroidDriver<AndroidElement> driver = BaseNew.capabilities();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    
    TouchAction t = new TouchAction(driver);
    driver.findElementByXPath("//android.widget.TextView[@text='Views']").click();
    driver.findElementByXPath("//android.widget.TextView[@text='Date Widgets']").click();
    driver.findElementByAndroidUIAutomator("text(\"2. Inline\")").click();
    driver.findElementByXPath("//*[@content-desc='9']").click();

    t.press(ElementOption.element(driver.findElementByXPath("//*[@content-desc='15']")))
            .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(3)))
            .moveTo(ElementOption.element(driver.findElementByXPath("//*[@content-desc='45']"))).release()
            .perform();

Solution

  • With Appium java_client v6.0.0-BETA1 different Options are introduced like

    1. ElementOption(to pass element)
    2. PointOption(to pass coordinates)
    3. WaitOptions(to pass wait time)

    Old methods of TouchActions class are deprecated. You can see change log here

    import static io.appium.java_client.touch.WaitOptions.waitOptions;
    import static io.appium.java_client.touch.offset.ElementOption.element;
    import static io.appium.java_client.touch.offset.PointOption.point;
    
      // Element Usage  
        new TouchAction(driver)
                .press(element(driver.findElementById("some_element_id")))
                .waitAction(waitOptions(ofSeconds(1)))
                .release()
                .perform();
    
         // Coordinate usage
            Point point =
                driver.findElementById("some_element_id_to_get_coordinate").getLocation();
    
        new TouchAction(driver)
                .press(point(point.x + 120, point.y + 130))
                .waitAction(waitOptions(ofSeconds(1))) // here ofSeconds is a java time Duration
                .release()
                .perform();