Mobile Automation

prerequisites

1. Java JDK
2. Appium studio UI
3. Android studio 3.1/3.2(if you want to use UI Automator for inspecting elements)
Note:C:\Users\testname\AppData\Local\Android\Sdk\tools\bin , you will get UI Automator


Connecting Mobile device and validating

Mobile Device name: DEFGHIKJLM
1. connect mobile device with proper cable
2. Enable developer option on, wake up option on, debugging mode on  in the mobile
3. adb devices

Connecting mobile on wifi (Mobile and laptop should be on Same Wifi)

adb -s DEFGHIKJLM tcpip 5555
After above, disconnect mobile and enter the  below command
adb connect  abc.def.g.h:5555

Where adc.def.g.h is the ip address of mobile connected on Wifi

adb devices
you will see abc.def.g.h as the device id

To kill adb server
adb kill-server

To start adb server
adb start-server

Maven Dependency

<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>7.3.0</version>
</dependency>

Important info

There are three ways of starting appium server
1. From Appium IO desktop

2. From Node js
     a. install nodejs
     b. npm -g appium (this install appium)
     c. from the command prompt enter 'appium' which starts appium server from command                prompt

3.  Using below program(which uses nodejs library

    AppiumDriverLocalService service  = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
.usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))
.withAppiumJS(new File("C:\\Users\\<<Name>>\\AppData\\Local\\Programs\\Appium\\resources\\app\\node_modules\\appium\\build\\lib\\main.js"))
.withLogFile(new File (System.getProperty("user.dir")+"\\src\\test\\resources\\logfile.log")).withArgument(GeneralServerFlag.LOCAL_TIMEZONE));
service.start();
                service.end();

Inspecting Elements in Mobile

1. chrome://inspect/#devices for Browser application in chrome(device need to be connected physically)
2. Till Adnroid 3.5, we have UI Automater to insepct(Device need to be connected physically)
C:\Users\<<Name>>\AppData\Local\Android\Sdk\tools\bin(uiautomaterviewer.bat)
3. Using Appium IO desktop desired capabilities
a. start inspection session in appium(Search Icon)
b. Minimum capability
   platformName   android
   deviceName     Motorola
   appPackage
   appActivity

Below are some other capabilities that can be used when apk file is in your system and load it directly into mobile device from system


File app = new File(System.getProperty("user.dir") + "\\src\\test\\resources\\test.apk");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "android");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.2");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "android");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());

we can get appPackage, appActivity using below trick(Here i have taken dialer example on mobile)
open the Dialer on Mobile
Now Issue below commands

   1. adb devices
   2. adb shell
   3. dumpsys window windows | grep -E 'mCurrentFocus'(this gives package info of application that in focus in mobile
    example:(dialer) mCurrentFocus=Window{257c55 u0 com.google.android.dialer/com.google.android.dialer.extensions.GoogleDialtactsActivity}

From the above first part before / is appPackage, after / is activity name

ie. appPackage=com.google.android.dialer
     appActivity=com.google.android.dialer.extensions.GoogleDialtactsActivity

4. Now restart the server which will start the application in mobile with Dialer application by default

Note:
After Android 7.0 version , default app activity cannot be found from the above command. To find default app activity follow the below
1. adb shell
2. cmd package resolve-activity --brief <apppackagename>
example: cmd package resolve-activity --brief test.app

Running Appium Server programmatically

1st Way(Long Code)
AppiumDriverLocalService service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
.usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))
.withAppiumJS(new File(
"C:\\Users\\guestuser\\AppData\\Local\\Programs\\Appium\\resources\\app\\node_modules\\appium\\build\\lib\\main.js"))
.withLogFile(new File(System.getProperty("user.dir") + "\\src\\test\\resources\\logfile.log"))
.withArgument(GeneralServerFlag.LOCAL_TIMEZONE));
service.start();

service.stop()

2ndWay(Short Code)
AppiumServiceBuilder serviceBuilder = new AppiumServiceBuilder().usingPort(4723);
AppiumDriverLocalService server = AppiumDriverLocalService.buildService(serviceBuilder);
server.start();


Note:
For android 8 and above 
developer option enable
debugging enable
Disable permission monitoring enable(it worked for Oppo A3s for apppackage and app activity)

Common Issues:

When running programmatically,
1. 0.0.0.0:4723 already in Use
 
 Solution: netstat -ano|findstr "PID :4723"
taskkill /pid 24548 /f
24548 is listening port

Dependencies

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



-<dependency>

<groupId>io.appium</groupId>

<artifactId>java-client</artifactId>

<version>7.6.0</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.squareup.okhttp/okhttp -->



-<dependency>

<groupId>com.squareup.okhttp3</groupId>

<artifactId>okhttp</artifactId>

<version>3.10.0</version>

</dependency>


-<dependency>

<groupId>com.squareup.okio</groupId>

<artifactId>okio</artifactId>

<version>1.14.1</version>

</dependency>

<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->



-<dependency>

<groupId>net.sourceforge.tess4j</groupId>

<artifactId>tess4j</artifactId>

<version>4.4.1</version>

</dependency>

Example1: Opening browser 

cap.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "android");
//cap.setCapability(MobileCapabilityType.NO_RESET,true);
cap.setCapability("chromedriverExecutable","D:\\resources\\chromedriver.exe");

Running Mobile Automation Scripts in AWS Device Form services


Credentials for AWS:

    1. URL: <<Device Form Url>>
    2. Account ID: <<Enter Account Id>>
    3. UN: <<user id>>
    4. PW: <<password>>
1. Choose applicaiton
    -Take the latest APK file and Upload, Name the Run(Example 24-12-2018_AndroidRun_1)

2. Configure
    -Take the latest Code from source tree
    -make sure you have testng.xml of the framework   
    -Run following Terminal command from POM.xml location in the local project to create a zip file.       This zip file will be created in target folder
     mvn -f pom.xml clean package -DskipTests
    -Now select type of test as "Appium Java Testng Test"
    -Now upload this zip file in AWS
    -Select  An customized YML file  for AWS to run the scripts.  example value
     "Default_yamlfile.yml" from drop down

3. select devices
   -threee devices samsung, motorola and pixel
 
4. Speicify device state
   -Skipt this step. just click on next with what ever state displayed

5. Reveiw and start run
 
6. Once run completed, collect the customer Artifacts from the AWS.

Limitations for AWS:
1. At a time only 5 devices will be run
2. Max time allocated for a device is 2.5 hours


Comments

Popular Posts