How to use Android Swipe on Eclipse Java?

I was using AppiumStudio tool to run some tests on an Android Applicative that I’ve created. The tool auto generates a Java Code and I took that code to test on Eclipse. But when I pasted the code, Java doesn’t recognize driver.swipe(); even with all imports done. Here is the code that the tool generated
package Appium;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.PerformsTouchActions;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import java.time.Duration;

//import org.openqa.selenium.support.ui.Duration;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.junit.*;
import java.net.URL;
import java.net.MalformedURLException;


public class Teste {
	private String reportDirectory = "reports";
	private String reportFormat = "xml";
	private String testName = "Cadastro_base";
	protected AndroidDriver<AndroidElement> driver = null;

	DesiredCapabilities dc = new DesiredCapabilities();
	 @Before
	 public void setUp() throws MalformedURLException {
	 dc.setCapability("reportDirectory", reportDirectory);
	dc.setCapability("reportFormat", reportFormat);
	dc.setCapability("testName", testName);
	dc.setCapability(MobileCapabilityType.UDID, "5210ce98fa7eb4b3");
	driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), dc);
		 }
	@SuppressWarnings({ "rawtypes", "deprecation" })
	@Test
	public void testCadastro_base() throws InterruptedException {
	driver.findElement(By.xpath("//*[@id='emailView']")).sendKeys("raikathekiller@mailinator.com");
	Thread.sleep(3000);
	new TouchAction(driver).press(point(500, 596)).waitAction(waitOptions(Duration.ofMillis(1000))).
	moveTo(point(518, 478)).release().perform();
	new TouchAction(driver).press(point(434, 468)).waitAction(waitOptions(Duration.ofMillis(1000))).moveTo(point(471, 312)).release().perform();
	Thread.sleep(3000);
	Thread.sleep(3000);
	driver.findElement(By.xpath("//*[@id='password_view']")).sendKeys("Smart2000");
	new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='confirm_password_view']")));
	driver.findElement(By.xpath("//*[@id='confirm_password_view']")).sendKeys("Smart2000");
	driver.findElement(By.xpath("//*[@id='name_and_surname_view']")).sendKeys("TED USP");
	driver.findElement(By.xpath("//*[@id='cpfView']")).sendKeys("41801452865");
	new TouchAction(driver).press(point(531, 465)).waitAction(waitOptions(Duration.ofMillis(1000))).moveTo(point(637, 175)).release().perform();
	Thread.sleep(3000);
	driver.findElement(By.xpath("//*[@id='dobView']")).click();
	Thread.sleep(3000);
	driver.findElement(By.xpath("//*[@text='2018']")).click();
	Thread.sleep(3000);
	driver.findElement(By.xpath("//*[@text='2020']")).click();
	Thread.sleep(3000);
	driver.findElement(By.xpath("//*[@contentDescription='13 fevereiro 2020']")).click();
	Thread.sleep(3000);
	driver.findElement(By.xpath("//*[@text='OK']")).click();
	Thread.sleep(3000);
	driver.findElement(By.xpath("//*[@id='phoneView']")).sendKeys("13982133161");
	driver.findElement(By.xpath("//*[@id='generView']")).click();
	driver.findElement(By.xpath("//*[@text='Feminino']")).click();
	TouchAction tAction=new TouchAction(driver);
	new TouchAction(driver).moveTo(584, 1013);
	driver.findElement(By.xpath("//*[@text='Cadastrar']")).click();
	//driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.consul.android.smartbeer.staging:id/register_button\")").click();
	Thread.sleep(8000);

	}

	private WaitOptions waitOptions(java.time.Duration duration) {
		// TODO Auto-generated method stub
		return null;
	}

	private PointOption point(int i, int j) {
		// TODO Auto-generated method stub
		return null;
	}

//	@After
//	public void tearDown() {
//	Thread.sleep(8000);
//	}
	}

My question is: To run the test correctly I need to Swipe between fields on the screen. How can I convert the command driver.swipe to a TouchAction or a working command that will do the touch and move action?

which Appium client version are you using (I’m assuming it’s 5 \ 6)

swipe is available in appium-client 4.1.2, you can try using this version…

or convert to TouchActions in eclipse

1 Like

Hi Tom. I tried to using Touch Action: TouchAction(driver).press(point(434, 468)).waitAction(waitOptions(Duration.ofMillis(1000))).moveTo(point(471, 312)).release().perform(); But it is returning me this

java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:882)
at io.appium.java_client.TouchAction$ActionParameter.(TouchAction.java:456)
at io.appium.java_client.TouchAction.press(TouchAction.java:68)
at Appium.Teste.testCadastro_base(Teste.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)

Hi Lucas,

I tested a code snippet on my end which appears to be working:

TouchAction ta = new TouchAction(driver);
ta.press(434, 468).waitAction(Duration.ofMillis(1000)).moveTo(471, 312).release().perform();

Remove point, and waitOptions from the line of code. Copy paste my code snippet and see if it works for you.

2 Likes

Thank you Nivi, it worked!

Hi Lucas,
I tried from my end it gives me error on “press” error i mentioned below - Error message - “The method Press(LongPressOptions) is undefined for the type TouchAction”

There are some changes in latest Appium Liibraries with regards to TouchAction , Please review the java doc and then try to match best replacement .

@kishore Is there any same Logic for HorizontalScroll or SwipeLeft

Yes please check the Appium Client library docs … they may have changed.