1

My Appium Java code:

package Generic;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.remote.MobileCapabilityType;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;

public class AppiumSelenium {

    //AndroidDriver Ad=new AndroidDriver();
    public static void main(String arg[]) throws MalformedURLException, InterruptedException {

DesiredCapabilities dc= new DesiredCapabilities();

        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");

        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");

        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1.1");

        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");

        dc.setCapability(MobileCapabilityType.APP, "/home/connexis/appium/AppiumMobile/src/main/java/Apps/ApiDemos.apk");

        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver<WebElement> driver= new AndroidDriver<WebElement>(url,dc);

        driver.findElementsById("android:id/text1").get(1).click();

        WebElement info=driver.findElementsById("android:id/text1").get(1);


        int x1=info.getLocation().getX();
        int y1=info.getLocation().getY();

        //we need end coornidates a.longPress(x).moveTo(startX, endY).release().perform();too hence , lets use some formulat to geterate


        int x2=x1 /2;
        int y2=y1 /2;






        TouchAction a = new TouchAction((MobileDriver) driver);
        -------------------------------------------------------

        a.longPress(info).moveTo(x1, y2).perform().release();






        /*
        List<WebElement> val=driver.findElementsById("android:id/text1");
        //it should retrive all the objects means 30 or 40 whatever present in that app

        for(int i=0;i<val.size();i++){

            System.out.println(val.get(i).getText());

        }
        */

        Thread.sleep(9000);

        driver.quit();

    }

}

Environment Set :

 Selenium server standalone 2.53
>     java-client 5.0.4
>     java-client 6.1.0

Error logs

Oct 04, 2018 3:07:53 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0 INFO: Detected dialect: W3C Exception in thread "main" java.lang.NoSuchMethodError: io.appium.java_client.TouchAction.longPress(Lorg/openqa/selenium/WebElement;)Lio/appium/java_client/TouchAction; at Generic.AppiumSelenium.main(AppiumSelenium.java:52)

2 Answers 2

2

If you look at release version https://github.com/appium/java-client/releases/tag/v6.0.0-BETA1 the usage of TouchActions are changed and they accept io.appium.java_client.touch.ActionOptions and sublasses were added

Please refer the unit tests of Appium for TouchActions class here

Sign up to request clarification or add additional context in comments.

Comments

0

In your code you cannot pass webelement in longpress(). LongPress() takes point option as its parameter. Modify your code as following:

   new TouchAction(driver).longPress(PointOption.point(x1, y1)).moveTo(PointOption.point(x2, y2)).perform().release();

If you want to vertical scroll you can use do it as follow:

 public static void swipe(int startX, int endX, int startY, int endY) {
        try {
            new TouchAction(Init.driver).press(PointOption.point(startX, startY)).waitAction(WaitOptions.waitOptions(ofSeconds(1)))
                    .moveTo(PointOption.point(endX, endY)).release().perform();              
        } catch (Exception e) {
            System.out.println("unable to swipe");
        }
    }

4 Comments

thank you but I want to vertical scrolling using these object but that is not working
i have updated my answer. Can you please check if it is working?
hii Suban dhyako ''waitOptions(ofsecond(1)))'' display error in 'ofsecond(1)'
please use ofSeconds. Don't use ofsecond, ofSecond or ofseconds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.