Writing Basic Tests
Step 1: Download the Sample App
To test the above configuration, download the sample app from the following URL:
Sauce Labs Sample AppInstalling and Launching Application in Appium Inspector Tool
Using JSON Configuration
Below is the JSON configuration to install and launch the application in Appium Inspector Tool:
{
"platformName": "Android",
"appium:platformVersion": "7.1.1",
"appium:deviceName": "Mi Max 2",
"appium:app": "C:\\Users\\Dell\\Downloads\\Android.SauceLabs.Mobile.Sample.app.2.7.1.apk",
"appium:appWaitActivity": "*",
"appium:automationName": "UiAutomator2",
"appium:noReset": false,
"appium:fullReset": false,
"appium:adbExecTimeout": 60000,
"appium:showLog": true
}
Steps to Use:
- Start the Appium server with the default configuration (
http://127.0.0.1:4723). - Connect your Android device and verify it is recognized by running the
adb devicescommand. - Paste the above JSON configuration into the "Desired Capabilities" section in Appium Inspector.
- Click on "Start Session" in Appium Inspector.
The Appium Inspector tool will install the application if it is not already installed and launch it on the connected Android device.
Using Selenium Script
The following Selenium script demonstrates how to install and launch the application programmatically:
package mobileAutoTesting;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
public class InstallAndLaunchApp {
public static void main(String[] args) {
// 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:app", "C:\\Users\\Dell\\Downloads\\Android.SauceLabs.Mobile.Sample.app.2.7.1.apk");
caps.setCapability("appium:appWaitActivity", "*");
caps.setCapability("appium:automationName", "UiAutomator2");
caps.setCapability("appium:noReset", false);
caps.setCapability("appium:fullReset", false);
caps.setCapability("appium:adbExecTimeout", 60000);
caps.setCapability("appium:showLog", true);
try {
// Initialize Android Driver
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), caps);
System.out.println("App installed and launched successfully!");
// Perform any desired actions on the app (if required)
// Close the session
driver.quit();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
Steps to Execute:
- Ensure Appium Server is running at
http://127.0.0.1:4723. - Verify the device connection using
adb devices. - Place the APK file at the specified path (
C:\Users\Dell\Downloads\Android.SauceLabs.Mobile.Sample.app.2.7.1.apk). - Run the script using a Java IDE like IntelliJ IDEA or Eclipse.
This script will programmatically install the application and launch it on the device.
Retrieving Application Package Information
Getting the Currently Focused App's Package and Activity
To find the package name and activity of the currently focused app, you can use the following command:
Windows Command:
adb shell dumpsys window | find "mCurrentFocus"
Linux/Mac Command:
adb shell dumpsys window | grep "mCurrentFocus"
Steps:
- Ensure the device is connected and recognized by running:
adb devices
- Launch the app you want to inspect on the device.
- Run the above command based on your operating system.
Output will look like:
mCurrentFocus=Window{d0abc123 u0 com.example.myapp/.MainActivity}
- Package Name:
com.example.myapp - Main Activity:
.MainActivity
Listing All Installed Apps
To get a list of all installed applications:
Command:
adb shell pm list packages
Example output:
package:com.example.myapp
package:com.android.settings
package:com.google.android.apps.maps
Getting Current App Package
To find the package of the app currently running in the foreground:
Windows Command:
adb shell dumpsys activity activities | find "mResumedActivity"
Linux/Mac Command:
adb shell dumpsys activity activities | grep "mResumedActivity"
Steps:
- Launch the desired app on the device.
- Run the appropriate command based on your operating system.
Example output:
mResumedActivity: ActivityRecord{abcd123 u0 com.example.myapp/.MainActivity}
- Package Name:
com.example.myapp - Activity:
.MainActivity
Platform-Specific Notes
- Windows: Ensure the Android SDK is installed and
adbis added to your PATH. Use Command Prompt or PowerShell. - Linux: Use the terminal and make sure
adbis installed via a package manager or the Android SDK. - Mac: Use the terminal, and install
adbusing tools like Homebrew or directly from the Android SDK.
Additional Filtering
- Windows: Use
findfor specific filtering, e.g.:adb shell pm list packages | find "example"
- Linux/Mac: Use
grepfor filtering, e.g.:adb shell pm list packages | grep "example"
How to Launch an App in Appium Inspector and Using a JSON if app is installed
SConfigure JSON in Appium Inspector
To launch the app in Appium Inspector, use the following JSON configuration:
{
"platformName": "Android",
"appium:platformVersion": "7.1.1",
"appium:deviceName": "Mi Max 2",
"appium:appPackage": "com.swaglabsmobileapp",
"appium:appActivity": ".MainActivity",
"appium:appWaitActivity": "*",
"appium:automationName": "UiAutomator2",
"appium:noReset": true,
"appium:fullReset": false,
"appium:adbExecTimeout": 60000,
"appium:showLog": true
}
- Open Appium Inspector.
- Click on "Start Session" and paste the above JSON configuration.
- Ensure your device is connected and Appium Server is running.
- Click "Start Session" to launch the app on your device for inspection.
Launch the App Using a Selenium Script if App is Installed
The following Java script demonstrates how to launch the app programmatically:
package mobileAutoTesting;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
public class LaunchApp {
public static void main(String[] args) {
// 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);
try {
// Initialize Android Driver
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), caps);
System.out.println("App launched successfully!");
// Add test steps here, if required
// Close the session
driver.quit();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}