Full Automation scripts using appium - Notes By ShariqSP
Login script
Login Automation Script
This section outlines the functionality of the login Java program, which uses the Appium framework to automate the login process for a mobile application. Below are the key details:
Key Features
- Framework: Appium with the UiAutomator2 driver for Android automation.
- Platform: Android 7.1.1 on a device named "Mi Max 2".
- Target Application: "Swag Labs Mobile App".
- Functional Flow:
- Launch the app.
- Fill in the login credentials using accessibility IDs.
- Submit the login form.
- Terminate the app session.
Code
package login;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
public class login {
public static void main(String[] args) throws MalformedURLException {
// 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));
System.out.println("App launched successfully!");
// Add test steps here, if required
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();
// Close the session
driver.terminateApp("com.swaglabsmobileapp");
}
}
Explanation of Key Code Sections
- Desired Capabilities: These are used to specify the device and app details required for the Appium server to initialize the Android driver.
- Driver Initialization: The
AndroidDriveris initialized with the specified URL pointing to the Appium server running on localhost. - Implicit Wait: A timeout is set to allow elements to be located before throwing an exception.
- Login Steps: The script locates the username, password, and login button using accessibility IDs and performs the login action.
- App Termination: Ends the session gracefully by terminating the app.
Example Output
When the script is executed successfully, the following message will be displayed on the console:
App launched successfully!
Logout script
Logout Automation Script
This section explains the logout automation script, which automates the process of logging out from a mobile application using Appium and Java. The program utilizes Appium's AndroidDriver and UiAutomator2 to interact with the app's UI elements.
Key Features
- Framework: Appium with the UiAutomator2 driver for Android automation.
- Platform: Android 7.1.1 on a device named "Mi Max 2".
- Application: Swag Labs Mobile App.
- Flow:
- Launch the app.
- Log in using valid credentials.
- Open the menu and select the "Logout" option.
- Terminate the app session.
Code
package logout;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
public class logout {
public static void main(String[] args) throws MalformedURLException {
// 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));
System.out.println("App launched successfully!");
// Perform login steps
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();
// Navigate to menu and logout
driver.findElement(AppiumBy.xpath("//android.view.ViewGroup[@content-desc='test-Menu']/android.view.ViewGroup/android.widget.ImageView")).click();
driver.findElement(AppiumBy.xpath("//android.widget.TextView[@text='LOGOUT']")).click();
// Close the session
driver.terminateApp("com.swaglabsmobileapp");
}
}
Explanation of Code Sections
- Desired Capabilities: These settings specify the testing platform (Android), device details, app package, and other parameters required by the Appium server.
- Driver Initialization: The
AndroidDriveris initialized using the Appium server URL and the desired capabilities. The driver is used to interact with the mobile application. - Implicit Wait: A timeout of 3 seconds is set to allow elements to load before throwing an exception.
- Login Steps:
- Username and password are entered into the respective fields identified by their accessibility IDs (
test-Usernameandtest-Password). - The login button (
test-LOGIN) is clicked to proceed.
- Username and password are entered into the respective fields identified by their accessibility IDs (
- Logout Steps:
- The menu is opened by locating an element via an XPath expression.
- The "Logout" option is clicked, identified by its visible text.
- Session Termination: The
terminateAppmethod closes the app gracefully to end the test session.
Output
If the script executes successfully, the following actions will occur:
- The Swag Labs Mobile App is launched.
- User logs in using the provided credentials.
- The app navigates to the menu and logs the user out.
- The app session is terminated.
Console Output: App launched successfully!
Add Item to Cart
Add Item to Cart Automation Script
This section describes the addItemToCart script, which automates the process of logging in, adding an item to the cart, and proceeding through the checkout flow in a mobile application using Appium and Java. The script also highlights challenges like handling invisible elements, requiring advanced gestures or JavaScript executors for successful execution.
Key Features
- Framework: Appium with UiAutomator2 for Android automation.
- Platform: Android 7.1.1 on a device named "Mi Max 2".
- Application: Swag Labs Mobile App.
- Flow:
- Launch the app.
- Log in using valid credentials.
- Add an item to the cart.
- Navigate to the cart and proceed to checkout.
- Fill in the required user information and complete the checkout process.
Code
package addItemToCart;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
public class addItemToCart {
public static void main(String[] args) throws MalformedURLException {
// 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 first name
driver.findElement(AppiumBy.xpath("//android.widget.EditText[@content-desc=\"test-First Name\"]")).sendKeys("Shariq");
// Enter last name
driver.findElement(AppiumBy.xpath("//android.widget.EditText[@content-desc=\"test-Last Name\"]")).sendKeys("SP");
// Enter ZIP code
driver.findElement(AppiumBy.accessibilityId("test-Zip/Postal Code")).sendKeys("560001");
// Click on continue (requires gesture handling if element is not visible)
driver.findElement(AppiumBy.xpath("//android.widget.TextView[@text=\"CONTINUE\"]")).click();
// Complete the checkout
driver.findElement(AppiumBy.accessibilityId("test-FINISH")).click();
// Close the session
driver.terminateApp("com.swaglabsmobileapp");
}
}
Explanation of Code Sections
- Desired Capabilities: Specifies platform details, app information, and configurations like device name, automation engine, and timeouts.
- Driver Initialization: Initializes the AndroidDriver to interact with the mobile app.
- Login Steps:
- Username and password are entered using
AppiumBy.accessibilityId. - The "Login" button is clicked to proceed.
- Username and password are entered using
- Add Item to Cart:
- Identifies the "Add to Cart" button using XPath and clicks it to add the first item to the cart.
- Cart Navigation and Checkout:
- Opens the cart using an XPath locator for the cart button.
- Proceeds to the checkout screen by clicking the "Checkout" button.
- User Information:
- Fills out fields for first name, last name, and ZIP code.
- Handles potential issues with invisible elements during scrolling, which can be resolved with gestures or JavaScript executors.
- Complete Checkout: The "Finish" button is clicked to finalize the order.
- Session Termination: The
terminateAppmethod closes the app session.
Challenges and Improvements
The script includes a note about handling elements not visible on the screen. Advanced gesture controls or JavaScript executors can be used to scroll and make such elements interactable. This topic will be explored in the next section on gestures.
Output
If executed successfully, the script will:
- Log the user into the app.
- Add the first item to the cart.
- Proceed through the checkout process.
- Complete the order and terminate the session.