0% found this document useful (0 votes)
12 views12 pages

Appium Setup and Automation Guide

some appium notes

Uploaded by

cat20.nov
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

Appium Setup and Automation Guide

some appium notes

Uploaded by

cat20.nov
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Appium is like client server app where server is written on node js and client is code

Server will process this code and perform actions on phone

Downlaod appium- appium -g install (or google)


sudo npm install -g appium
sudo npm install -g npm@11.1.0

To start appium server:


Cmd- appium

Zhprofile
export JAVA_HOME=/Library/Java/JavaVirtualMachines/[Link]/Contents/Home
export ANDROID_HOME=/users/devanshimehta/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/platform-tools
export PATH=$PATH:$ANDROID_HOME/build-tools
export PATH=$PATH:$ANDROID_HOME/build-tools/36.0.0-rc4
export PATH=$PATH:$ANDROID_HOME/emulator
export ANDROID_SDK_ROOT=$ANDROID_HOME
export PATH=$ANDROID_HOME:$PATH

UIAutomator:
1) Check driver list—appium driver list (in cmd)
2) Appium driver install uiautomator2

Code:
Appium code will send to server and server further interacts with mob device
Give all capabilities ( app info, platform , device etc) In UiAutomator2

public class BaseTest {

public AndroidDriver driver;


public AppiumDriverLocalService as;

@BeforeClass
public void settings() throws Exception
{
as= new AppiumServiceBuilder().withAppiumJS(new
File("//usr//local//lib//node_modules//appium//build//lib//[Link]"))
.withIPAddress("[Link]").usingPort(4723).build(
);
[Link]();

UiAutomator2Options options= new UiAutomator2Options();


[Link]("samsung SM-M336BU");
[Link]("appium:appPackage",
"[Link]");
[Link]("appium:appActivity",
"[Link]" );
[Link]("appium:platformVersion", "13.0");
driver= new AndroidDriver(new
URI("[Link]
}

@AfterClass
public void tearDown()
{
[Link]();
[Link]();
}
}

To automatically start/stop server:

Use AppiumServiceBuilder
Give path of node module
[Link] fle is responsible to trigger/start appium server

It requires info like IP address and port

public AppiumDriverLocalService as;


as= new AppiumServiceBuilder().withAppiumJS(new
File("//usr//local//lib//node_modules//appium//build//lib//[Link]"))
.withIPAddress("[Link]").usingPort(4723).build(
);
[Link]();

APPIUM INSPECTOR:
Download online
{
"appium:deviceName": "samsung SM-M336BU",
"platformName": "Android",
"appium:platfromVersion": "13",
"appium:appPackage": "[Link]",
"appium:appActivity": "[Link]",
"appium:automationName": "UIAutomator2"
}

Before inspecting start appium server from cmd


Device also has tp be connected in Android studio

Pgm1:

Base test:
public class BaseTest {

public AndroidDriver driver;


public AppiumDriverLocalService as;
@BeforeClass
public void settings() throws Exception
{
as= new AppiumServiceBuilder().withAppiumJS(new
File("//usr//local//lib//node_modules//appium//build//lib//[Link]"))
.withIPAddress("[Link]").usingPort(4723).build(
);
[Link]();

UiAutomator2Options options= new UiAutomator2Options();


[Link]("samsung SM-M336BU");
[Link]("appium:appPackage",
"[Link]");
[Link]("appium:appActivity",
"[Link]" );
[Link]("appium:platformVersion", "13.0");

driver= new AndroidDriver(new


URI("[Link]
}

@AfterClass
public void tearDown()
{
[Link]();
[Link]();
}

Login into fab:


package tests;

import [Link];
import [Link];
import [Link];

import [Link].java_client.AppiumBy;
import [Link].java_client.[Link];
import [Link].java_client.[Link];

public class Appium1 extends BaseTest{

@Test

public void runAppium() throws Exception, URISyntaxException


{
[Link](4000);

[Link]([Link]("//[Link][@resource-
id='[Link]:id/
permission_allow_button']")).click();
[Link](1000);

[Link]([Link]("//[Link][@resource-
id=\"[Link]:id/environmentButton\"]")).click();
[Link](1000);

[Link]([Link]("[Link]")).click();
[Link](1000);
[Link]([Link]("new
UiSelector().text(\"UAT4\")")).click();
[Link](1000);
[Link](new KeyEvent().withKey([Link]));
[Link](1000);
[Link]([Link]("new
UiSelector().resourceId(\"[Link]:id/btnAlreadyCusto
mer\")")).click();
//settings(); will run as beforeclass given
[Link](1000);

[Link]([Link]("//[Link][@text='Enter
here']")).sendKeys("4174781");
[Link](1000);

[Link]([Link]("[Link]")).click();
[Link](1000);

[Link]([Link]("//[Link][@resource-
id='[Link]:id/btnContinue']")).click();

[Link](5000);

}
}

Mac:
If error comes android home not found:
Means eclipse is not able to identify android path even if set in env

Rclick on test pgm file- run- run config


Testng tab- go to your file pgm – env
Set android_home and give path of android home sdk

To directly go to a page:
1) App package- global proj name
2) App activity – different pages

To find out activity


- Use the Activity class
- Adb devices in terminal
- Go to emulator and keep the page ready
- Then run adb shell dumpsys window | grep -E
mCurrentFocus’
- Before slash is package

Activity ac= new Activity (packagename, activityname);


[Link] (ac);
This is depreciated now
Use javascript and mobile:startActivity

To download apk in phone-


1. First open emu
2. Thjen on terminal go to platform-tool path
cd /Users/devanshimehta/Library/Android/sdk/platform-tools
3. Type the path wherever apk is there
adb install /Users/devanshimehta/Downloads/[Link]

now in script use


[Link]("//Users//devanshimehta//Documents//Eclipse1//
AppiumTest//src//test//java//resources//[Link]");

To hide keybpoard from screen if it is obstructing elements


[Link]();
Scrolling in Appium
 Touch Action – Appium provides TouchAction API that allows you to
perform a series of actions like tap, touch, press, long press, scroll,
and pinch. You can perform complex multi-touch actions one after
the other using this API.
 UiScrollable – This is a UI Automator API provided by Appium to
automate scrolling gestures. You can use it to scroll to an element
under the parent object without the need to specify the direction of
the scroll.
 JavaScript Executor – Appium can execute JS scripts to imitate
scroll gestures and interact with the desired element.

Swiping
This is an XCUITest driver specific method that is similar to scrolling (for
reference, see [Link]
This method has the same API as Scrolling, just replace "mobile: scroll"
with "mobile: swipe"
Automating Sliders
iOS

 Java

// java

// slider values can be string representations of numbers between 0 and 1

// e.g., "0.1" is 10%, "1.0" is 100%

WebElement slider = [Link]([Link]("//window[1]/slider[1]"));

[Link]("0.1");

native android app: only app


hybrid android app: app + browser (within app)

By default the driver wont work directly on web (native) because it is only for android.
So we need to switch the context of driver.

To find elements within web, use same locators like web (from chrome and inspect for
that website)
If the mobile has opens in chrome browser, it needs chrome driver as selenium

Presskey events- go through


Gestures- go through

To go back in android
[Link](new KeyEvent().withKey([Link]));

iPhone
iphone setup works only on mac

download appium server


install xcode to compile app on iphone (from app store)
launch iphone simulator
setup appium maven proj with appium client jars
desired capabilities to invoke app on iphone

Android- emulator
Ios- simulator

Appium sets webdriver agent in phone and using this app it will automate
This is like middle man – webdriver agent
This will get installed before execution begins. So set some timeout

Ios automator- xcuitest


Appium driver install xcuitest

XCUITestOptions opt= new XCUITestOptions();

[Link](“iphone 13 pro”); //give name

[Link](“//give path of app which is compiled and wherever it is stored/[Link]”);


//compiled app path will be in xcode
[Link](“15.5”);

[Link]([Link](20));

Public IOSDriver driver;


driver= new IOSDriver(new URI("[Link]

In Appium Inspector:

App: give path of app


automationName : XCUITest
deviceName: iphone 12 pro
platformVersion: 15.5
platformName: iOS

Locators:
Xpath, classname, AppiumBy. iosclasschain, AppiumBy. iosprodicatestring

Iosclasschain is very similar to xpath. Ios is not in xml hence xpath is very slow in ios
Appium will convert native code to xml in background for xpath. So very slow
**/tagname[`attribute == ‘value’ `]

Iospredicatestring
“attribute==’value’ AND attribute2==’value’ “
Can use multiple attributes
“attribute BEGINSWITH ‘value’ “
attribute BEGINSWITH [c] ‘value’ “ (case sentitive)
“attribute ENDSWITH ‘value’ “

-************************************************************************
If the app is directly .app then no need to execute it in xcode
Place that app under resources package in project

Direct opening app in ios

to invoke existing app (eg gallery)

//use bundleid to identify app in ios (package name in android)


//to get bundle id- ask developer
Or get .app file

For default ios app like photos- google bundleid


Map <String,String> param= new HashMap<String,String>();
[Link](“bundleID”, “[Link]”);
[Link](“mobile:launchApp”, param);

[Link]() – to go back

to connect real device and invoke:


[Link](“Android Device”);
this will trigger connected andr device

for ios:
go through doc
use brew (first install brew) – for libimobiledevice
ios realtime device can be connected from 3rd party- libimobiledevice

Framework:
When in eclipse you write package name as
[Link] (. Means new folder in file structure)

Pom:
@AndroidFindBy(id=”name”)
Private WebElement web;

In pom class create constructor to identify driver


Globally in this class- AndroidDriver driver;
Public pomclass (AndroidDriver driver)
{
[Link]=driver;
PgaeFactory .initElements(new AppiumFieldDecorator(driver), this);
}

PgaeFactory- this step is needed to contrsuct the entire finEleemnt (by.()) sentence
@AndroidFindBy(id=”name”)
Private WebElement web;

In your base class wherever you’ve defined driver – create a class of pom
pomclass pm= new pomclass(driver) ;

Utils:
All actions like long press, scrolling etc can be in one class-

Pomclass extends utils

In utils there is no driver info-


So in pomclass construvtor call super(driver);
Encapsulation
When we use pom, we are hiding implementation details.
In pom class, we should use generic methods like
Setname
Setgender

And in test- only call this method for that page and pass param
Eg: [Link](“abc”);

That’s why the findby is private webelement so that test class directly doesn’t use it

Eg: [Link]()
Instead of directly using findby object- we should use methods like

[Link](“abc”);

don’t use locators directly – use emthods instead and call in methods
that’s why locators in pom are private and method is public
In ios
IOSDriver
In actions file- give ios actions
@iOSXCUITFindBy

AppiumDriver is parent of AndroidDriver and iOSDriver


This class can be used for common utils of android and ios (just like Exception)

appiumutils Common utils

AndroidActions iosactions Platform specific utils

Page object class

Pom page 1 Pom page 2 Pom page 1 Pom page 2

ios test
Android test

ios base
Android base
Test Config (start server,
Capabilities, invoke app)

To directly open any pge:


Use packagename and activityname

You can use this in beforemethod in test class


So that before eveyr test, the app comes to home page again

In before class
Invoking app, installing – once before all test in class

To get that- go to that screen


And in trminal and hit grep mfocus command

((JavascriptExecutor)driver).exeuteScript(“mobile: startActivity”,
[Link](“intent”,”packagename/activityname”);

Home screen- mainactivty (could be splash also)

To read json
Download [Link] jar in maven
Jackson-databind- it’s a Mapper- read json and write in hashmap- which can be used in data provider

Copy paste utility code

String file= [Link] (new File (path pf jspn)),StandardCharSets.UTF_8);


ObjectMapper mp= new ObjectMapper();
List<HashMap<String,String>> data= [Link](file, new
TypeReference<List<HashMap<String,String>>>()
{});

This [Link] method creates as many hashmaps as there are arrays in json (data sets)

List will look like


{. {firstenrty hashmap}, {secondentry hashmap}. }

In dataprovider
List<HashMap<String,String>> data= getJsonData ([Link](filepath));
Return new Object [][] { {[Link](0)}, {[Link](1)}. }

Syso ([Link](“[Link]”));
This returns current working dir

Ypu can use this to get path of files


[Link](“[Link]” + “//src//test//etc”);
To include properties file:
Create a package resource- create file with .properties
Then load in class using Properties [Link]
[Link](“name of property”)

For Extent Report


Use 2 classes- ExtentReport and ExtentSparkReporter

ExtentSparkReporter esp= new ExtentSparkReporter (“path of where reports should be


saved”);

[Link]().setReportName(“name of report”);

[Link]().setDocumentTitle(“name of title- tab name”);

ExtentReports ex= new ExtentReports();


Attacj the report created with esp to this class
Imagine esp as helper class to help config the reports

[Link](esp);

[Link](“Tester”,”Devanshi”);

In your test method:


[Link](“Test name”);

In framework- write and include it all in Listeners

You might also like