Add Item With Scroll Gesture Program using UI Selector - Notes By ShariqSP

Understanding the AddItemWithScrollGesture Program

This program demonstrates how to use Appium for automating an Android application workflow. It includes login, adding an item to the cart, performing a checkout, and utilizing scroll gestures to locate elements on the screen that are not immediately visible. The program is written in Java and uses the Appium Java Client (version 8.6.0).

Key Features

  • Scroll to an Element: The scrollToElement method performs a scroll gesture using Appium's mobile: scrollGesture command to locate elements dynamically.
  • Swipe Gesture: The swipe method utilizes the mobile: swipeGesture command to simulate swiping in a specified direction.
  • End-to-End Workflow: The program automates login, cart addition, checkout, and completion of a purchase flow.
  • Custom Gestures: Uses Appium's ability to execute gestures for a smoother user experience.

Program Code

            
            package addItemWithScrollGesture;
            
            import java.net.MalformedURLException;
            import java.net.URL;
            import java.time.Duration;
            
            import org.openqa.selenium.WebElement;
            import org.openqa.selenium.remote.DesiredCapabilities;
            
            import io.appium.java_client.AppiumBy;
            import io.appium.java_client.android.AndroidDriver;
            
            public class AddItemWithScrollGesture {
            
                public static void scrollToElement(AndroidDriver driver, String textToScrollTo) {
                    boolean isElementFound = false;
                    while (!isElementFound) {
                        try {
                            // Try finding the element
                            WebElement element = driver.findElement(AppiumBy.androidUIAutomator(
                                    "new UiSelector().text(\"" + textToScrollTo + "\")"));
                            if (element.isDisplayed()) {
                                isElementFound = true;
                            }
                        } catch (Exception e) {
                            // Perform a scroll gesture if the element is not found
                            driver.executeScript("mobile: scrollGesture", java.util.Map.of(
                                    "left", 0,
                                    "top", 500,
                                    "width", 1080,
                                    "height", 1000,
                                    "direction", "down",
                                    "percent", 0.75
                            ));
                        }
                    }
                }
            
                public static void swipe(AndroidDriver driver, String direction) {
                    driver.executeScript("mobile: swipeGesture", java.util.Map.of(
                            "left", 500,
                            "top", 1000,
                            "width", 500,
                            "height", 600,
                            "direction", direction,
                            "percent", 0.75
                    ));
                }
            
                public static void main(String[] args) throws MalformedURLException, InterruptedException {
                    // Set Desired Capabilities
                    DesiredCapabilities caps = new DesiredCapabilities();
                    caps.setCapability("platformName", "Android");
                    caps.setCapability("appium:platformVersion", "7.1.1");
                    caps.setCapability("appium:deviceName", "Mi Max 2");
                    caps.setCapability("appium:appPackage", "com.swaglabsmobileapp");
                    caps.setCapability("appium:appActivity", ".MainActivity");
                    caps.setCapability("appium:automationName", "UiAutomator2");
            
                    // Initialize Android Driver
                    AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), caps);
                    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
            
                    // Login script
                    driver.findElement(AppiumBy.accessibilityId("test-Username")).sendKeys("standard_user");
                    driver.findElement(AppiumBy.accessibilityId("test-Password")).sendKeys("secret_sauce");
                    driver.findElement(AppiumBy.accessibilityId("test-LOGIN")).click();
            
                    // Add item to cart
                    driver.findElement(AppiumBy.xpath("(//android.widget.TextView[@text=\"ADD TO CART\"])[1]")).click();
            
                    // Open cart
                    driver.findElement(AppiumBy.xpath("//android.view.ViewGroup[@content-desc=\"test-Cart\"]/android.view.ViewGroup/android.view.ViewGroup")).click();
            
                    // Click on checkout
                    driver.findElement(AppiumBy.xpath("//android.widget.TextView[@text=\"CHECKOUT\"]")).click();
            
                    // Enter first name as Shariq
                    driver.findElement(AppiumBy.xpath("//android.widget.EditText[@content-desc=\"test-First Name\"]")).sendKeys("Shariq");
            
                    // Enter last name as SP
                    driver.findElement(AppiumBy.xpath("//android.widget.EditText[@content-desc=\"test-Last Name\"]")).sendKeys("SP");
            
                    // Enter zip code as 560001
                    driver.findElement(AppiumBy.accessibilityId("test-Zip/Postal Code")).sendKeys("560001");
            
                    // Click on continue  
                    driver.findElement(AppiumBy.xpath("//android.widget.TextView[@text='CONTINUE']")).click();
            
                    // Perform a Scroll gesture to find "FINISH" button
                    scrollToElement(driver, "FINISH");
            
                    // Click on Finish
                    WebElement finish = driver.findElement(AppiumBy.xpath("//android.widget.TextView[@text='FINISH']"));
                    finish.click();
            
                    // Close the session
                    driver.terminateApp("com.swaglabsmobileapp");
                }
            }
            
                

How It Works

The program uses the Appium library to interact with the Android device. The following steps summarize its functionality:

  1. Sets up device and app configuration using DesiredCapabilities.
  2. Initializes an AndroidDriver instance to interact with the app.
  3. Performs login by sending credentials and clicking the login button.
  4. Adds an item to the cart and navigates to the checkout page.
  5. Fills in user details (first name, last name, and zip code) and clicks "CONTINUE".
  6. Uses the scrollToElement method to scroll and locate the "FINISH" button.
  7. Completes the purchase by clicking "FINISH".