Autoplay



Transcripted Summary

The next TouchAction that we will use is Swipe.

In this example, we will try to swipe with Android and Appium.

Here, I also prepared a test case with a swipe or Android TouchAction.

This is the setup, and this is the swipe_test, and here I am also clicking on "Views", and after that, I will add my code for swipe.


package Android;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidTouchAction;
import io.appium.java_client.touch.offset.ElementOption;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
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 Swipe_Android_Test {

    public AppiumDriver 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 swipe_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();
        }
    }
}

Then we have the AfterTest.

So let's open the application APIDemos and "Views".

We have something here called the "Gallery" and inside the "Gallery", we have 1. Photos or 2. People.



Here, I will click on Photos and then I want to swipe - like a scroll but horizontal.



So, I need to do this action with Appium and TouchAction in this project.

We already have "Views", so we need to find the "Gallery".

I will copy this code for the views and then I will add it for gallery with accessibility id "Gallery".

Then I need to tap on this, so I can copy the code from the previous tap and click on the gallery element.


    @Test
    public void swipe_test() {
        AndroidElement views = (AndroidElement)
                driver.findElementByAccessibilityId("Views");

        //Tap
        actions = new AndroidTouchAction(driver);
        actions.tap(ElementOption.element(views)).perform();

        AndroidElement gallery = (AndroidElement)
                driver.findElementByAccessibilityId("Gallery");
        actions.tap(ElementOption.element(gallery)).perform();
    }

Then I can copy both of these lines and just change the accessibility id.

After I click on the gallery, I need to click on "1. Photos".

So let's add this one.

There's a space, so let's try first with space and if not, we can remove it. We'll call this one photo and then I need to click on photo.

After I click on photo, I need to find the first picture element.

So from here, I need to open the inspection session with the Appium server and Appium Inspector and open the application and then try to find the inspector in this one.

I click on Android and open the application in the inspection session with the Appium server and then I will try to find picture number 1 and then after that, I need to move starting from picture number 1, so picture 1 will be the starting point.

So, here I will open the application from the Appium inspection session, and then I will start to look at the element.

I will click on "Views" on the device and then "Gallery" and then "1. Photos" and then I just need to refresh the view to get the updated screenshots from the device.



I need to click on this first image and then I will try to find anything to identify the element.

We have the "xpath" here but it's not recommended because it can lead to fragile tests.

I can find something like the class name and I have a find by class name, so I can use this class name and get the index of zero from this class since this class name will be for all of the images in this slider.

So from AndroidElement, I can add pic1 and driver.findElement(By.className()) - you need to make sure it's findElement not findElements - and the class name will be android.widget.ImageView.

Then I need to get the size, but in this case, we need to use findElements because all of the elements will be with the same class, so we need to change it to findElements and get the index.

Since these are elements in an array - so an ArrayList - will return a lot of elements in this array.

I need picture number 0, which is the first picture element, and we need to cast it to an AndroidElement.

So, we have the driver, then findElements, because most of the images will be with this class, and then we click on this one.

Then after that, with actions, I need to press on ElementOption.element() with element pic1, and then I need .waitAction().

Then I need to moveTo() - and here I will use PointOption.point() and then you need to specify the X offset and Y offset as integers.

So for example, I can say, -20 for the xOffset, and here I can say 210 for the yOffset, and then after that, I will .release() and .perform()to leave it after I did the action.


    @Test
    public void swipe_test() {
        AndroidElement views = (AndroidElement)
                driver.findElementByAccessibilityId("Views");

        //Tap
        actions = new AndroidTouchAction(driver);
        actions.tap(ElementOption.element(views)).perform();

        AndroidElement gallery = (AndroidElement)
                driver.findElementByAccessibilityId("Gallery");
        actions.tap(ElementOption.element(gallery)).perform();

        AndroidElement photo = (AndroidElement)
                driver.findElementByAccessibilityId("1. Photos");
        actions.tap(ElementOption.element(photo)).perform();

        AndroidElement pic1 =
                (AndroidElement) driver.findElements(By.className("android.widget.ImageView")).get(0);

        actions.press(ElementOption.element(pic1))
                .waitAction()
                .moveTo(PointOption.point(-20, 210))
                .release()
                .perform();
    }

Here is the code for using swipe and Appium, so I will click on pic1 and try to press and moveTo, and then release the button from this one.

Let's run our script and check what will be displayed.

I will close this inspection session, the application has already closed, and now we will install the application again because every time we are installing the apk and not using the one that exists on the application.

Then I will open the test and try to check if the test is passed or we have a problem to fix.

The application is open, I click on "Views", click on "Gallery", and then here we have a problem because an element could not be located - I think this is because "1.Photos" needs a space.



Let's add this space, and then we will run again to check the test.

The application is open, click on "Views", click on "Gallery", click on "1. Photos", and then we clicked on the picture and it moved a little bit, so we can change the value, but we did it with a swipe, so this is the way to do the swipe with Android and TouchAction with Appium.



Resources



Quiz

The quiz for this chapter can be found in Chapter 5.10

© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR