In this demo, we will learn how to use mobile gestures with Android and iOS.
We will start with Android examples like drag and drop, scroll down, and swipe.
Let's start and check our project.
Our first demo is a scroll with Android. So, we will try to scroll down with Appium, and here I've already prepared the application by adding desired capabilities, adding the teardown, and also adding the application that we will use during our session.
package Android;
import io.appium.java_client.android.AndroidDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class Scroll_Android_Test {
public AndroidDriver driver;
@BeforeTest
public void setUp() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "9.0");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("app", System.getProperty("user.dir") + "/apps/ApiDemos.apk");
driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
}
@AfterTest
public void tearDown() {
if (null != driver) {
driver.quit();
}
}
}
Also here, I'll add one test for scroll down.
So, we need to add @Test
and public void scroll_test()
.
But first, let's start and check how we can scroll in our application.
We will open our API Demos app, and I need to click on "Views" and in "Views", I want to scroll down until I can click on "Lists" because if we open "Views" for the first time, "Lists" is not displayed.
So, I need first to scroll down until I find "Lists" and then click on "Lists" to display this screen.
This is what we will try to use with Appium.
Here, we will try to find "Views" first, and then we can start using it.
We'll need to first click on "Views" and then we can scroll and click on "Lists".
In the code, we can just state that we have an AndroidElement
and we'll create an element so we can use it in the next steps.
So this is called views
for driver.findElementByAccessibilityId()
, because, in the previous demo, we found that we have the accessibility ids with these texts.
For example, this accessibility id is "text", "preference", "OS", and "NFS", so we will use accessibility id for Views, or if we already have an inspection session open, I will click on "Views" and here I will find the accessibility id is "Views".
So I will copy this and then I will add the semi-colon because I will not create any actions.
Here, I have an error that driver.findElementByAccessibilityID
should return a WebElement
but here I am defining it as AndroidElement
so we will just cast this element.
So I will cast it to an AndroidElement and that's it.
import io.appium.java_client.android.AndroidDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class Scroll_Android_Test {
public AndroidDriver driver;
@BeforeTest
public void setUp() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "9.0");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("app", System.getProperty("user.dir") + "/apps/ApiDemos.apk");
driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
}
@Test
public void scroll_test() {
AndroidElement views =
(AndroidElement) driver.findElementByAccessibilityId("Views");
}
@AfterTest
public void tearDown() {
if (null != driver) {
driver.quit();
}
}
}
So, here I added an AndroidElement views
and then I can continue.
The first action that I will use is Tap.
I want to single tap an element and to be able to use it, we need to initialize Android touch actions.
So I will add public AndroidTouchAction
- it's from java_client from Android and here I can name it actions
.
Then in scroll_test()
I will say actions = new AndroidTouchAction()
, and here, we are passing what will perform the actions on the elements, so the driver
and then I will add the semicolon.
Then we will type actions.
and here we have longPress
, moveTo
, waitAction
, perform
, release
, tap
, and press
.
I need to tap on a specific element so I will select .tap()
and then I will pass ElementOptions.element()
because I need to tap on an element, and then here I need to specify the WebElement views
and then I will add .perform()
.
So here is the description of what we're doing - I am selecting the action tap
from AndroidTouchAction
and with this ElementOption
, I am selecting the element
, which is views
with the accessibility ID "Views", and then I will click and perform.
import io.appium.java_client.android.AndroidDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class Scroll_Android_Test {
public AndroidDriver driver;
public AndroidTouchAction actions;
@BeforeTest
public void setUp() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "9.0");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("app", System.getProperty("user.dir") + "/apps/ApiDemos.apk");
driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
}
@Test
public void scroll_test() {
AndroidElement views =
(AndroidElement) driver.findElementByAccessibilityId("Views");
// Tap
actions = new AndroidTouchAction(driver);
actions.tap(ElementOption.element(views)).perform();
}
@AfterTest
public void tearDown() {
if (null != driver) {
driver.quit();
}
}
}
So, this is the first one and let's just start and verify that our code is working and then we can go and continue with our tests.
We will run this script and check what happened during our test.
I will close this inspection session to be able to view my server and now the application is closed to be able to download it from the beginning and install it and then run our test script.
Here, we are using the first action from the TouchAction for Android and here the application is opened and I clicked on "Views" and the test script is passed.
Then, after I click on "Views", I need to scroll down, so I need to add the logic or the code for scroll down.
Scroll down usually works as a vertical scroll down from top to down or vice versa, and we usually use the height of the device and dimensions.
We will work with the dimensions of the device to be able to start it from a starter point and then scroll until the end point, or where you wanted to finish the scrolling.
So, we need to create a custom test for scroll down.
Here in our code, we can create private void scrollDown()
and inside scrollDown
, we can add Dimension dimension = driver.manage().window().getSize()
.
We need to get the size from our mobile - we need to get the dimensions of the application.
I will start by adding the first dimension.
I will add an int scrollStart
- this the start point, which is equal to dimension.getHeight()
multiplied by 0.8
.
Then we need to cast this to be an integer because dimension
is a double and it needs to be an integer.
So this is a starter point - to get to "Views" we need to start scrolling from the "Date Widgets" point, not from the beginning, and then after that scroll until I get to "Lists".
Then second, I need scrollEnd
, so here I will add int scrollEnd = dimension.getHeight()
and this is also multiplied by 0.8 and I will cast it to an integer.
So I have the start point and the end point, and then I will start using the touch action for press
and moveTo
and release
the press using actions.
I will add actions = new AndroidTouchAction()
and then, I will pass the driver
and then I will select press
- I don't want it to longPress because longPress will be used for drag and drop - but I need the normal press
.
With press
, we'll pass PointOptions
, because I have a starter point and end point.
With PointOption.point
you need the offset of X and Y.
I can add 0
for x, and scrollStart
for y - so this is the offset and this is the point that I wanted to start from.
Then, I need to waitAction()
with WaitOptions.waitOptions()
with a Duration.ofSeconds(3)
.
And then .moveTo()
, I need to move to PointOption.point()
with the offset of 0
and then I need to end at scrollEnd
.
So, this is an action using Dimension
- so I need to start from the offset and a point on the screen, and then I will scroll, for example, for three seconds - maybe we can increase this time - and then I need to scroll down to this one, then I need just to add release()
because I'm still clicking or pressing on the button, and then after that, I will click on perform().
private void scrollDown() {
Dimension dmension = driver.manage().window().getSize();
int scrollStart = (int) (dimension.getHeight() * 0.8);
int scrollEnd = (int) (dimension.getHeight() * 0.1);
actions = new AndroidTouchAction(driver)
.press(PointOption.point(0, scrollStart))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(3)))
.moveTo(PointOption.point(0, scrollEnd))
.release()
.perform();
}
In the scroll_test()
, after I tap on "Views", I need to scroll down until I find "Lists".
So here, I will call the scrollDown
method.
Then, I need to add another element with AndroidElement
which is lists = driver.findElementByAccessibilityId()
and in this case, it will be "Lists" because we are using the accessibility ID "Lists".
This is the first one and then I can add actions.tap
with the ElementOption.element()
, pass lists
and then perform()
.
Again, I need to cast lists
to be an AndroidElement
instead of WebElement
.
So, this is my code.
@Test
public void scroll_test() {
AndroidElement views =
(AndroidElement) driver.findElementByAccessibilityId("Views");
// Tap
actions = new AndroidTouchAction(driver);
actions.tap(ElementOption.element(views)).perform();
// ScrollDown
scrollDown();
AndroidElement lists = (AndroidElement) driver.findElementByAccessibilityId("Lists");
actions.tap(ElementOption.element(lists)).perform();
}
First, I will use tap
from the TouchAction, then scroll down using a custom method with a scrollStart and scrollEnd.
Then, I define another element to be able to click on this one.
This is my code and then I will just try to run my script again and check what happened during our test.
The application will be closed and then we will continue with our test script.
The application is opened, and then here, we found a problem in our script.
Let's see what the problem is in the log.
Here we have the error, "no such element" - so what's the problem here?
We have a problem here in the scrollDown
.
We started from the Gallery, so let's open our application again and click on "Views" and "Lists".
I'm starting in the Gallery as the start point, and also for the end point I added the same value, so here for scrollEnd
, I will replace 0.8
with 0.1
since this was a typo with our script.
Let's try our script again and check what happened during our test.
This usually happens and from the Appium server, you can detect the error from the server and you can start fixing your script and running it again.
So the application is opened, I click on "Views" and from the Gallery, I start scrolling down and then click on the "Lists".
Our test script has passed now and the scroll down is working.
So, in this demo we did the scroll using TouchAction, we also clicked one action using tap
, and also we tried the press
, and the moveTo
and the waitActions
using the scrollDown
.
Quiz
The quiz for this chapter can be found in Chapter 5.10