This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
21 / 49
Sep 2017

I'm trying to swipe right to left from the first content(there is no content at left hand side) So far I'm using below, but it's not working.

public void swipeRightToLeft() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap<String, Double> swipeObject = new HashMap<String, Double>();
        swipeObject.put("startX", 0.9);
        swipeObject.put("startY", 0.5);
        swipeObject.put("endX", 0.01);
        swipeObject.put("endY", 0.5);
        swipeObject.put("duration", 3.0);
        js.executeScript("mobile: swipe", swipeObject);
}

public void swipeLeftToRight() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap<String, Double> swipeObject = new HashMap<String, Double>();
        swipeObject.put("startX", 0.01);
        swipeObject.put("startY", 0.5);
        swipeObject.put("endX", 0.9);
        swipeObject.put("endY", 0.6);
        swipeObject.put("duration", 3.0);
        js.executeScript("mobile: swipe", swipeObject);
}

Error log: org.openqa.selenium.WebDriverException: Not yet implemented. Please help us: http://appium.io/get-involved.html18 (WARNING: The server did not provide any stacktrace information)

  • created

    Jan '17
  • last reply

    Feb '19
  • 48

    replies

  • 12.6k

    views

  • 15

    users

  • 11

    likes

  • 11

    links

Frequent Posters

There are 48 replies with an estimated read time of 5 minutes.

Those coordinates depend on the size of your android. If you want to continue to use percentage you can use the following method:

public static void swipeHorizontal(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.height * anchorPercentage);
    int startPoint = (int) (size.width * startPercentage);
    int endPoint = (int) (size.width * finalPercentage);
    new TouchAction(driver).press(startPoint, anchor).waitAction(duration).moveTo(endPoint, anchor).release().perform();

    //In documentation they mention moveTo coordinates are relative to initial ones, but thats not happening. When it does we need to use the function below
    //new TouchAction(driver).press(startPoint, anchor).waitAction(duration).moveTo(endPoint-startPoint,0).release().perform();
}

And calling it with the value from your topic would be:

swipeHorizontal(driver,0.9,0.01,0.5,3000);

Notice the comments about absolute/relative values

and i suggest to get coordinates directly from element you trying to scroll. not the whole screen.

As you said, it is the same format... You just exchange height for width

public static void swipeVertical(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.width * anchorPercentage);
    int startPoint = (int) (size.height * startPercentage);
    int endPoint = (int) (size.height * finalPercentage);
    new TouchAction(driver).press(anchor, startPoint).waitAction(duration).moveTo(anchor, endPoint).release().perform();

    //In documentation they mention moveTo coordinates are relative to initial ones, but thats not happening. When it does we need to use the function below
    //new TouchAction(driver).press(anchor, startPoint).waitAction(duration).moveTo(0,endPoint-startPoint).release().perform();
}

swipeVertical(driver,0.9,0.1,0.5,3000);

Never tried. I think it works only with automator2. But check this test testToastMSGIsDisplayed() in

@Telmo_Cardoso, Could you please tell me how to hide the android keyboard.

I have tried this one: driver.hideKeyboard() but it's not working.

Currently I'm using: driver.navigate().back(); but I think it's not the proper way.

could you please tell me the better way i'm using java-client 4.1.2

New "issues" should be open in new topics. What happens when you run driver.hideKeyboard() ? Post the logs in a new topic. Thanks

@Telmo_Cardoso in my case, I need to swipe a row to left to view hidden button on right side of it.

I tried your solution but instead of press event, I see tap event happening on row.

I'm not sure what may be going wrong, even I tried longPress instead of press but no luck yet.

Can you please refer to below method and review if it has some issue ?

WebElement rowToSlide = driver.findElement(By.xpath("//XCUIElementTypeApplication[1]//XCUIElementTypeTable[1]/XCUIElementTypeCell[1]"));
Dimension size = rowToSlide.getSize();

            	TouchAction swipe = new TouchAction(driver).press(rowToSlide, size.width / 2 + 50 , size.height / 2).waitAction(1000)
            	            .moveTo(rowToSlide, 100, size.height / 2).release().perform();
7 months later

One more thing, you can enable touch and pointer option for observing touch action, From android mobile Settings > Developer option > Input > Enable Pointer location and Show touches, observe the touch action and change the start point / end point / time duration from swipeVertical(driver,0.9,0.1,0.5,3000); as you need.

Here’s how I placed it. I just copied the code above.

I called the method to my test.java class:
Capture

with Regards to Settings, I don’t have also the 'Enable Pointer location and Show touches ’ option. I’m currently using Samsung Galaxy S8. Thank you. @Al_Imran

Method is ok, Please check again, Settings > Enable the Developer options > Scroll down the page > Input > Enable Pointer location and Show touches (check mark box)

I have tried this but no effect takes place
I have 1 question how u get the co-ordinates
0.9,0.1,0.5,3000

And also when I tried this

Dimension dim= SwipeHomeButton.getSize();
int height= dim.getHeight();
int width = dim.getWidth();
int y = (int)(height*.20);
int startx=(int)(width*.75);
int endx=(int)(width*.35);
driver.swipe(startx, y, endx, y,500);
new TouchAction(driver).press(y, startx).waitAction(5000).moveTo(y, endx).release().perform();
It pulled from the top on my android phone
It looks like it is not able to get the proper co-ordinates

Can u plzz help me with this

15 days later

Hi,

Here is detailed video on how you can swipe from left to right, right to left, from top to bottom and from bottom to top using Appium.

Thanks,
Anuja

20 days later

Hi

I am new to Appium and struggling on calling a swipe method from left to right.

As per you video, driver is not having method called swipe in its intelligence.

I have tried using calling TouchAction class as well but while giving driver as argument it is
throwing the below error all the time. I am Struck and not able to move further.

Please help here

image

Still No progress. I am getting below error.

Below is my Capability SetUp. Please let me know If any where am doing wrong.

public class ExampleHorizontalSwipe {
//AndroidDriver driver;
WebDriver driver;

@Test(priority=0)
public void setUp() throws MalformedURLException
{
	
	
	DesiredCapabilities capabilities = new DesiredCapabilities();
	
	capabilities.setCapability("deviceName", "NexusTest");
	capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
	capabilities.setCapability(CapabilityType.VERSION, "5.0.1");
	capabilities.setCapability("platformName", "Android");
	capabilities.setCapability("...");
	capabilities.setCapability("appActivity", "...");
	driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

// driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
// driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}

Hi After using the below method, console show script pass successfully but execution was not happening from left to right for a specific element. please help where am i missing.

public class ExampleHorizontalSwipe {
AndroidDriver driver = null;
}

TouchAction act = new TouchAction(driver);
act.longPress(200, 180, Duration.ofSeconds(2)).moveTo(600, 180).release().perform();

Hi All, Finally I could able to resolve the issue of swiping for

Android
Hybrid App
Samsung 8 Device

before start of swiping use the switching to the Native context, after doing the swipe action

         //Find swipe start and end point from screen's.
	//Find startx point which is at right side of screen.
	int startx = (int) (size.width * 0.70);

	//Find endx point which is at left side of screen.
	int endx = (int) (size.width * 0.30);

	//Find vertical point where you wants to swipe. It is in middle of screen height.
	int starty = size.height / 2;

	System.out.println("startx = " + startx + " ,endx = " + endx + " , starty = " + starty);

	//Swipe from Right to Left.
	if(swipingDirection == Swipe.Left)
	((AndroidDriver<WebElement>)getInstance()).swipe(startx, starty, endx, starty, 2000);

	//Swipe from Left to Right.
	if(swipingDirection == Swipe.Right)
	((AndroidDriver<WebElement>)getInstance()).swipe(endx, starty, startx, starty, 2000);

switch back to WebContext to continue working with elements. Hope this solution helps

Sorry Kumar, I was missed you request.

You have done great !!! anyways for your information please find the below code which i have used to swipe left to right

AndroidDriver driver = null;

TouchAction act = new TouchAction(driver);
act.longPress(200, 180, Duration.ofSeconds(3)).moveTo(790, 180).release().perform();.

17 days later
16 days later

@Telmo_Cardoso
This solution not working, while I’m using java-client 5.0.4. Its showing error at duration and suggested change it to Duration.ofDays(duration), I have tried it but not working. could you please help for solving this problem.

That was for an old client… in 5.0.4 I use:

Duration.ofMillis(duration)  

with duration being in miliseconds (2000, for instance).

If you using latest java client v6 beta then it has changed again.

Thanks it is working cool :slight_smile: @Telmo_Cardoso

For java-client 5.x.x new solution is:

public static void swipeHorizontal(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
        Dimension size = driver.manage().window().getSize();
        int anchor = (int) (size.height * anchorPercentage);
        int startPoint = (int) (size.width * startPercentage);
        int endPoint = (int) (size.width * finalPercentage);
        new TouchAction(driver).press(startPoint, anchor).waitAction(Duration.ofMillis(duration)).moveTo(endPoint, anchor).release().perform();
    }


    public static void swipeVertical(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
        Dimension size = driver.manage().window().getSize();
        int anchor = (int) (size.width * anchorPercentage);
        int startPoint = (int) (size.height * startPercentage);
        int endPoint = (int) (size.height * finalPercentage);
        new TouchAction(driver).press(anchor, startPoint).waitAction(Duration.ofMillis(duration)).moveTo(anchor, endPoint).release().perform();
    }