Swipe Gesture Implementation for Adding Item to Cart in Mobile App - Notes By ShariqSP

Swipe Gesture Implementation for Adding Item to Cart in Mobile App

This code demonstrates how to automate the process of adding an item to a cart in a mobile application using Appium. It includes a swipe gesture to scroll the screen, leveraging the Selenium W3C actions API. The example showcases modern Appium practices with detailed comments to enhance clarity.

Code

              
              package addItemtoCartbySwipeGesture;
              
              import java.net.MalformedURLException;
              import java.net.URL;
              import java.time.Duration;
              import java.util.Collections;
              
              import org.openqa.selenium.interactions.Sequence;
              import org.openqa.selenium.WebDriver;
              import org.openqa.selenium.WebElement;
              import org.openqa.selenium.interactions.PointerInput;
              import org.openqa.selenium.remote.DesiredCapabilities;
              import org.openqa.selenium.support.ui.ExpectedConditions;
              import org.openqa.selenium.support.ui.WebDriverWait;
              
              import io.appium.java_client.AppiumBy;
              import io.appium.java_client.android.AndroidDriver;
              
              public class AddItemToCart {
              
                  public static void swipe(AndroidDriver driver) {
                      // Swipe the screen upwards
                      PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
                      Sequence swipe = new Sequence(finger, 0);
              
                      swipe.addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), 500, 1000));
                      swipe.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
                      swipe.addAction(finger.createPointerMove(Duration.ofSeconds(1), PointerInput.Origin.viewport(), 500, 500));
                      swipe.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
              
                      driver.perform(Collections.singletonList(swipe));
                  }
              
                  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:appWaitActivity", "*");
                      caps.setCapability("appium:automationName", "UiAutomator2");
                      caps.setCapability("appium:noReset", true);
                      caps.setCapability("appium:fullReset", false);
                      caps.setCapability("appium:adbExecTimeout", 60000);
                      caps.setCapability("appium:showLog", true);
              
                      // Initialize Android Driver
                      AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), caps);
                      driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3000));
              
                      // 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 user details
                      driver.findElement(AppiumBy.xpath("//android.widget.EditText[@content-desc=\"test-First Name\"]")).sendKeys("Shariq");
                      driver.findElement(AppiumBy.xpath("//android.widget.EditText[@content-desc=\"test-Last Name\"]")).sendKeys("SP");
                      driver.findElement(AppiumBy.accessibilityId("test-Zip/Postal Code")).sendKeys("560001");
                      driver.findElement(AppiumBy.xpath("//android.widget.TextView[@text=\"CONTINUE\"]")).click();
              
                      // Perform swipes
                      swipe(driver);
                      Thread.sleep(1000);
                      swipe(driver);
                      Thread.sleep(1000);
                      swipe(driver);
              
                      // Finish the order
                      WebElement finish = driver.findElement(AppiumBy.accessibilityId("test-FINISH"));
                      finish.click();
              
                      // Close the session
                      driver.terminateApp("com.swaglabsmobileapp");
                  }
              }
              
                

Explanation

  • Swipe Method: A reusable `swipe` method is created using the W3C actions API. It simulates an upward swipe on the screen by defining start and end coordinates.
  • Login: The script logs into the application using pre-defined credentials.
  • Add to Cart: It selects an item to add to the cart by interacting with the respective element.
  • Checkout: The script navigates to the checkout page and fills out user details such as name and ZIP code.
  • Multiple Swipes: The swipe method is called thrice to simulate scrolling through a long page.
  • Order Completion: After filling out the details, the "Finish" button is clicked to complete the order.

Key Features

The script demonstrates efficient use of the Appium W3C actions API for complex gestures like swiping and ensures seamless interaction with app elements.