0% found this document useful (0 votes)
26 views

Lab 5

Uploaded by

zannatulmaoameem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lab 5

Uploaded by

zannatulmaoameem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Bangladesh University of Professionals (BUP)

Department of Information and Communication Technology


ICT 4102 : Software Testing and Maintainance Lab
Lab No - 05

Title: Mobile application Testing using Appium.


Appium
Appium is a popular open-source framework used for automated mobile app testing. It allows developers to
automate the testing of native or hybrid iOS and Android applications. Appium doesn’t work alone. It runs the
test cases using the WebDriver interface.

Features of Appium:

• Like Selenium, Appium allows QAs to create test scripts in multiple programming languages – Java,
JavaScript, PHP, Ruby, Python, and C#.
• Appium is especially favored for being a flexible, cross-platform framework that testers can use to create
test scripts applicable to multiple platforms (Windows, iOS, and Android) – using the same API.
• Essentially, Appium users can reuse their source code for Android and iOS, thus reducing the time and
effort of building tests. Unlike other automation tools, Appium doesn’t require you to modify the app’s
source code to enable automation.
• Supports the automation of native, hybrid, and mobile web applications, accommodating a wide range
of app types.Integrates with cross-platform mobile app development frameworks like React Native,
Xamarin, and Flutter.
• Provides built-in tools like Appium Desktop and Appium Inspector to inspect app elements and generate
test scripts

Objective:
To set up android studio and appium for mobile app testing.

Prerequisites:
1. Install Java Development Kit (JDK)
2. Install Node.js (https://nodejs.org/en/download/prebuilt-installer )
3. Android Setup:
• Install Android Studio: Download and install Android Studio.
4. Install Appium: Download and install Appium using npm (Node Package Manager).

sh
npm install -g appium@latest
for uninstall : npm un -g apppium
5. Verify appium is available on the system : appium -v which appium where appium
6. Run command appium to get information on our installed appium (CTRL + C to quit)
7. Install appium doctor (optional) npm install @appium/doctor --location=global
appium-doctor –help
appium driver list
appium driver install uiautomator2
appium driver list –updates
8. run appium server appium
appium –allow-cors

1|Page
--allow-cors: This is a flag that tells the Appium server to enable CORS - Cross-Origin Resource Sharing (CORS)
enabled
You might need to use this command if you're using a testing framework that runs on a different domain than the
Appium server.
For example, if you're using the Appium Inspector to record and edit test scripts, you'll need to start the Appium server
with CORS enabled if the Inspector is running on a different domain
This works on Appium 2.x versions

Check missing dependencies on our system


Step 1 : Run command appium-doctor --android
Step 2 : Take a note of missing necessary dependencies on our system

Setup Java JDK


Common Steps (Windows and Mac OS)
Step 1 : Check Java JDK is present java -version javac -version
Step 2 : If not download and install java (https://www.oracle.com/java/technologies/downloads/#java23 )

Install appium android drivers


Step 1 : Run command to see available drivers appium driver list (works on appium 2.0 or later)
Step 2 : Install Appium driver for Android appium driver install uiautomator2
Step 3 : To check for updates in existing drivers appium driver list --updates
Step 4 : To update existing drivers appium driver update uiautomator2

OPTION 2: Download and Install Android Studio


Step 1 - Download Android Studio https://developer.android.com/studio
Step 2 - Install on the system
Step 3 - Start Android Studio
Step 4 - Go to "Tools" -> "SDK Manager" (or click on the SDK Manager icon in the toolbar)
Step 5 - Select as per your testing needs
• SDK Platforms: Select the desired Android API levels you want to target for your tests. You can choose
multiple versions
• SDK Tools: Choose the platform-tools, build-tools, emulators etc compatible with your targeted API level(s)
Step 6 - Click Apply and wait for setup to complete
Step 7 - Set the folder having platform-tools, build-tools as ANDROID_HOME and update Path env variable
% ANDROID_HOME%\bin
% ANDROID_HOME%\build-tools
% ANDROID_HOME%\platform-tools

Step 8 - Can run command appium-doctor --android to check all requirements for Android Testing are completed

How to create and start Android Virtual Device (emulator) with Android Studio GUI App
Step 1 - Open Android Studio
Step 2 - Go to "Tools" -> "AVD Manager" (or click the AVD Manager icon in the toolbar)
Step 3 - Click "Create Virtual Device" and choose a desired device configuration (phone or tablet) with the required
Android API level
Step 4 - Click "Next" and follow the steps to define the AVD configuration (name, RAM, storage etc.)
Step 5 - Click "Finish" to create the emulator
Step 6 - Launch the emulator from the AVD Manager
Step 7 - Check the emulator is available in Device Manager
(Device manager will show all connected devices, virtual and physical)

2|Page
Here are some sample APK files that we can use for learning test automation
Can get some sample APK files from appium
https://github.com/appium/appium/tree/master/packages/appium/sample-code/apps
This contains both Android (.apk files) and iOS (.app files)

__________________________________________________________
Can search for APK files for apps like Calculator

3|Page
https://en.uptodown.com/android/search

__________________________________________________________
Browserstack https://github.com/browserstack/app-testing-examples/blob/master/app-debug.apk

__________________________________________________________
SauceLabs demo Apps
https://github.com/saucelabs/sample-app-mobile/releases

4|Page
Includes both android and ios apps

__________________________________________________________
TestProject demo app
https://github.com/testproject-io/android-demo-app

__________________________________________________________
Katalon Studio Sample apk files
https://github.com/katalon-studio-samples/katalon-mobile-automation/tree/master/Data%20Files

Note:
Appium Desktop has been deprecated and unsupported since Appium 2.0. It is no longer recommended for use due to known
security vulnerabilities and incompatibility with later versions of Appium.
We can use Appium Inspector

Setup and Start Appium Inspector

Step 1 : Download and install Appium Inspector https://github.com/appium/appium-inspector

OR

Use web version: https://inspector.appiumpro.com/

5|Page
Step 2 : Start Appium Inspector

Step 3 : Now click on Start Session


As we have not yet started appium server you will get error message

Step 4 : Start appium server by running command appium

If you are using the web version of Appium Inspector start appium using command appium --allow-cors

Step 5 : Note the host and port of appium server and provide the same in Appium Inspector

Step 6 : Now again click on Start Session on Appium Inspector


This time you should see the error message for desired capabilities and not for appium server

6|Page
Step 7 : Add the Desired Capabilities as per the mobile device or emulator connected to the system

deviceName - can get using command adb devices

platformName - can get using command appium driver list

platformVersion - can get using command adb shell getprop ro.build.version.release

If you have multiple devices connected first get the device id using adb devices then run command

adb -s DEVICE_ID shell getprop ro.build.version.release

app - provide the location of APK file

adb shell getprop ro.build.version.sdk

This command will provide the API level (e.g., “30” for Android 11, “29” for Android 10, etc.)

To use Emulator with Appium Inspector

Step 1 - Start appium appium --allow-cors

Step 2 - Start Emulator or Connect Physical Device

emulator -list-avds

emulator -avd AvdName

To start AVD in a new state (w/o maintaining the old state)


emulator -avd avd_name -no-snapshot-load

Step 3 - Run command adb devices to see the device id. Can use this for deviceName

Step 4 - Get the Android version of your device or emulator

adb shell getprop ro.build.version.release android version

If you have multiple devices connected first get the device id using adb devices then run command

adb -s DEVICE_ID shell getprop ro.build.version.release

adb shell getprop ro.build.version.sdk api level

Step 5 - Check the appium drivers available appium driver list

Step 6 - Get location for a demo APK file (for android)

Step 7 - Add all details in the Desired Capabilities of Appium Inspector

7|Page
{

"appium:automationName": "UiAutomator2",

"appium:platformName": "Android",

"appium:platformVersion": "14",

"appium:deviceName": "AVRK023C30029824",

"appium:app": "J:/backup download/ApiDemos-debug.apk"

Step 8 - Start Session

If you don’t have APK file and want to use an existing app on the device can use appPackage and appActivity

adb shell dumpsys window displays | grep -E ‘mCurrentFocus’

adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'”

8|Page
Step 3: Create TestNG Test Class
▪ Setup Your Test Class:

package mobileApp;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.appium.java_client.AppiumBy;
import io.appium.java_client.remote.options.BaseOptions;
import io.appium.java_client.android.AndroidDriver;
//import io.appium.java_client.MobileElement;
//import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class appTest{


private AndroidDriver driver;
private URL url;

@BeforeClass
public void setUp() throws Exception {
// caps= new DesiredCapabilities();
BaseOptions options = new BaseOptions()
.amend("appium:automationName", "UiAutomator2")
.amend("appium:platformName", "Android")
.amend("appium:platformVersion", "14")
.amend("appium:deviceName", "emulator-5554")
.amend("appium:app", "J:/backup download/ApiDemos-debug.apk")
.amend("appium:newCommandTimeout", 3600)
.amend("appium:connectHardwareKeyboard", true);

url = new URL("http://127.0.0.1:4723");


driver = new AndroidDriver(url, options);
}

@Test
public void sampleTest() {

driver.findElement(AppiumBy.accessibilityId("Graphics")).click();
driver.findElement(AppiumBy.accessibilityId("CameraPreview")).click();
driver.findElement(AppiumBy.accessibilityId("Accessibility")).click();
driver.executeScript("mobile: pressKey", Map.of("keycode", 4));
driver.findElement(AppiumBy.accessibilityId("OS")).click();

@AfterClass
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}

9|Page
Step 4: Configure Appium in POM File
Inside pom.xml:

This file defines the browsers to be used for the tests.


<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>appiumTest</groupId>
<artifactId>mobileApp</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>

<!-- https://mvnrepository.com/artifact/io.appium/java-client -->


<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->


<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.26.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->


<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>

Step 5: Execute Tests


• Execute test as testNG test

10 | P a g e

You might also like