Not able to perform horizontal swipe - 1.7.2

hi guys,

i know most of them faced similar issue but Im not able to horizontal swipe, even with percentage, have tried the following

WebElement el = driver.findElement(By.xpath(“//div[@Class=‘slide’]//div[@Class=‘image’]//div[@Class=‘arrow’]”));
Point loc = el.getLocation();

String currentcontext = driver.getContext();
driver.context("NATIVE_APP");
Thread.sleep(5000);

TouchAction touchAction = new TouchAction(driver);
touchAction.press(loc.x, loc.y).moveTo(0, loc.y).release().perform();
driver.context(currentcontext);

Specs: appium client - 1.7.2
** UI automato rv1**
** java client 5.0.4**
** maven -project**

Can anyone post exact code for it, w.r.t id , xpath or text a dynamic one…please it would be of great help

Does it has to do anything with UIautomator2 im still using uiautomator1 how do i upgrade to v2

for appium 1.7.2 you have to use absolute co-ordinates.
below code works for me, hope will help u

public void swipeRightToLeft() {
Dimension size = webDriver.manage().window().getSize();
int startx = (int) (size.width *0.94);
int endx = (int) (size.width *0.24);
int starty = size.height / 2;
int finalendx = startx-endx;
int endy = 0;
(new TouchAction(webDriver)).press(point(startx,starty)).moveTo(point(-finalendx,endy)).release().perform();
}

Thanks for ur code but im getting error could u please help me out

should i declare it, even the driver im getting error.

add this in ChooseDeliveryLocationPage :

import static io.appium.java_client.touch.offset.PointOption.point;

Also refer this for dealing with touch actions related issues:

public boolean swipeImages()
{
try
{
WebElement pageIndicator = driver.findElement(page_indicator);
String pageString= pageIndicator.getAttribute(“value”);
int length = pageString.length();
String count_string= pageString.substring(length-2, length).trim();
int count = Integer.parseInt(count_string);
for (int i=1; i<=count; i++){

	  JavascriptExecutor js = (JavascriptExecutor) driver;
	  HashMap<String, String> scrollObject = new HashMap<String, String>();
	  scrollObject.put("direction", "right");
	  js.executeScript("mobile: scroll", scrollObject);
			   }
		   System.out.println("Swipe Successfully");
				}
	catch (Exception e) 
	{
		System.out.println("Image Swipe was not successful.");
	}

Thanks a lot for ur reply im not able to import…im using java-client 5.0.4

Thanks for ur reply in not able to understand what does page_indicator do i have put resource id and in text have given in getattribute im getting erro

Thanks all for ur suggestions because of u guys life becomes beautiful;) im trying to swipe from right to left

…i have a deadline to meet but because of this im stuck…after installation i need to swipe it…kindly help

Appium client - 5.0.4

Have i mentioned appium 1.7.2 and java-client 5.0.4 i do not want to upgrade it is it ok to use these versions

Hey, can you provide the UI Automator view for carousel …
Thanks!!

Hi Vinita,

thanks a lot

Hi Vinita

thanks

Hi harshitij

im getting this following error

i have upgraded to 6.1.0 java client that error went but im getting press error

Hi @Rathan_G,

Above line of code is incorrect.The constructor of TouchAction is only accept driver as parameter.

Incorrect:
TouchAction action =new TouchAction(getDriver().press(point(startx,starty))…)

Correct:
TouchAction action =new TouchAction(getDriver()).press(point(startx,starty)).moveTo.

or

TouchAction action =new TouchAction(getDriver());
action.press(point(startx,starty)).moveTo.

if u are using java client 5.0.4 you can use below code

public void swipeRightToLeft() {
Dimension size = webDriver.manage().window().getSize();
int startx = (int) (size.width *0.94);
int endx = (int) (size.width *0.24);
int starty = size.height / 2;
int finalendx = startx-endx;
int endy = 0;
(new TouchAction(webDriver)).press(startx,starty).moveTo(-finalendx,endy).release().perform();
}

Hi Tany,

im not getting error but horizontal swipe from right to left does not happen, press point works for 6.0v…kindly suggest im stuck.

appium 1.7.2
java-client : 6.0.0-BETA5

public void swipeRightToLeft() {
//getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Dimension size = getDriver().manage().window().getSize();
System.out.println(size);

int startx = (int) (size.width *0.94);
int endx = (int) (size.width *0.24);
int starty = size.height / 2;
int finalendx = startx-endx;
int endy = 0;
 
TouchAction actions = new TouchAction(getDriver());
actions.press(point(startx,starty)).waitAction(waitOptions(Duration.ofMillis(2000))).moveTo(point(finalendx, endy)).release().perform();

}

@Rathan_G if you are using appium 1.7.2 and java client 6.0.0 beta5, u have to use absolute coordinates for 1.7.2

public void swipeRightToLeft() {
//getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Dimension size = getDriver().manage().window().getSize();
System.out.println(size);

int startx = (int) (size.width *0.94);
int endx = (int) (size.width *0.24);
int starty = size.height / 2;
int finalendx = startx-endx;
int endy = 0;

TouchAction actions = new TouchAction(getDriver());
actions.press(point(startx,starty)).waitAction(waitOptions(Duration.ofMillis(2000))).moveTo(point(-finalendx,endy)).release().perform();
}

if you use above code you may need to import below lines.

import java.time.Duration;
import static io.appium.java_client.touch.offset.PointOption.point;
import static io.appium.java_client.touch.WaitOptions.waitOptions;

Thanks for ur reply i updated from 5.0.4 to 6.0 because of press(point) not being compatible if it has to be actual co-ordinates will it affect different device size while running it…can u post an actual co-ordinates code…i really need to fix it fast …really appreciate it.

does it have anything to do with ui automator2? because im using uiautomator1