Saturday, 18 June 2016

Appium - Code for Automate the Calculator App in Android Mobile


Please follow below steps to Automate the mobile application in Appium


Emulator Setup for Android Mobile -> Setup




The First Appium code for Automate the calculator App



import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL; 
import java.util.concurrent.TimeUnit; 

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By; 
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.remote.CapabilityType; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest; 
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class Mobile 

{ 

WebDriver driver;

@BeforeTest 

public void setUp() throws MalformedURLException

{

// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();

// Set android deviceName desired capability. Set your device name. 
capabilities.setCapability("deviceName", "sdk"); 
// Set BROWSER_NAME desired capability. It's Android in our case here.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android"); 
// Set android VERSION desired capability. Set your mobile device's OS version.
capabilities.setCapability(CapabilityType.VERSION, "4.4.4"); 
// Set android platformName desired capability. It's Android in our case here. 
capabilities.setCapability("platformName", "Android");
// Set android appPackage desired capability. It is 
// com.android.calculator2 for calculator application. 
// Set your application's appPackage if you are using any other app.

capabilities.setCapability("appPackage", "com.android.calculator2"); 

// Set android appActivity desired capability. It is
// com.android.calculator2.Calculator for calculator application.
// Set your application's appPackage if you are using any other app. 

capabilities.setCapability("appActivity", "com.android.calculator2.Calculator"); 

// Created object of RemoteWebDriver will all set capabilities. 
// Set appium server address and port number in URL string.
// It will launch calculator app in android device. 

driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 
}

@Test
public void Sum() throws IOException 

{ 
System.out.println("Appium");


driver.findElements(By.xpath("//android.widget.Button")).get(0).click();
// driver.findElement(By.name("CLR")).click(); 
// Click on number 2 button. 
driver.findElement(By.name("6")).click(); 
// Click on + button. 
driver.findElement(By.name("+")).click(); 
// Click on number 5 button. 
driver.findElement(By.name("5")).click();
// Click on = button. 
driver.findElement(By.name("=")).click(); 
// Get result from result text box. 
String result = driver.findElement(By.className("android.widget.EditText")).getText(); 
System.out.println("Number sum result is : " + result);

File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File(System.getProperty("user.dir")+"\\ScreenShots.png"));


}

@AfterTest public void End() { driver.quit(); } 

}

How to find app Launcher activity name in Appium



We have two options for find app activity name

1) By using Command Prompt
2) Manifest Info
3) Hierarchy Viewer


1) By using Command Prompt

Please find below steps to find app activity name using command prompt.


-> app APK package name. EX : com.android.calculator2

-> Launch the Android Emulator and wait till starts properly.

-> Goto command prompt and goto E:\android-sdks\platform-tools and verify the device connected properly (not offline). by using below command

E:\SDK\platform-tools>adb devices

List of devices attached
emulator-5554   device

-> Open the reuired app and keep on active in Emulator

-> GOTO and command prompt and use below commands

E:\android-sdks\platform-tools>adb logcat >log.txt

-> Now its generated log text file. Here you can search by app Package name (com.android.calculator2) then we will have log like

I/ActivityManager(  878): Displayed com.android.calculator2/.Calculator: +7s332ms


Here com.android.calculator2.Calculator is app activity name.



2) By using AndroidManifest.xml Info



http://software-testing-tutorials-automation.blogspot.in/2015/10/find-launcher-activity-and-package-name.html


-> Right click on .apk file of APK Info app(or any other .apk file for which you wants to know package and activity name).

-> Select Open with -> WinRAR archiver option from right click context menu.

-> It will open .apk file in WinRAR window and you can see AndroidManifest.xml.

-> Open AndroidManifest.xml file in notepad. Now find keyword "p a c k a g e" In notepad.

-> Package name for my APK Info app is : com.intelloware.apkinfo Find keyword "a c t i v i t y" In notepad. It will show you activity name of your app.


-> Activity name for my APK Info app is display: .MainActivity. Here you need to append package name ahead of activity name so full activity name is : com.intelloware.apkinfo.MainActivity



3) Hierarchy Viewer 


-> Launch the Android Emulator and wait till starts properly.

-> Launch the appium and start the server.

-> Open the Hierarchy viewer from below path. Android Emulator should be starts properly, its should not be closed.

C:\android-sdks\tools\hierarchyviewer.bat


-> Then goto page in emulator which activity needs to be record.

-> Then automatically we can see the package name and activity name in Hierarchy viewer

we can see in bold color

com.android.calculator2/com.android.calculator2.calculator

Here,

package Name = com.android.calculator2
Activity Name = com.android.calculator2.calculator

How to find APK Package using UI Automator Viewer


We can identify the Package name by using below two methods


1) UI Automator viewer
2) Hierarchy Viewer


1) UI Automator viewer

Please find below steps to find APK package using UI Automator viewer


-> Open the reuired app and keep on active in Emulator

-> Launch the uiautomatorviewer.bat file from below mentioned path

E:\android-sdks\tools\uiautomatorviewer.bat

-> Click "Device Screenshot ("Uiautomater Dump") in uiautomatorviewer then its taking screenshot and paste into uiautomatorviewer.

-> Now we can inspect the element.

-> Right hand side we can see the option like "package" for APK package name.




2) Hierarchy Viewer


-> Launch the Android Emulator and wait till starts properly.

-> Launch the appium and start the server.

-> Open the Hierarchy viewer from below path. Android Emulator should be starts properly, its should not be closed.

C:\android-sdks\tools\hierarchyviewer.bat


-> Then goto page in emulator which activity needs to be record.

-> Then automatically we can see the package name and activity name in Hierarchy viewer

we can see in bold color

com.android.calculator2/com.android.calculator2.calculator

Here,

package Name = com.android.calculator2
Activity Name = com.android.calculator2.calculator



How to inspect element in mobile app for appium mobile automation testing (By using Emulator)



We can inspect the element in mobile app by using uiautomatorviewer.bat file from Android SDK folder.


-> Open the required app and keep on active in Emulator

-> Launch the uiautomatorviewer.bat file from below mentioned path

E:\android-sdks\tools\uiautomatorviewer.bat

-> Click "Device Screenshot ("Uiautomater Dump") in uiautomatorviewer then its automatically taking screenshot and paste into uiautomatorviewer.

-> Now we can inspect the element from Uiautomationviewer.

UnInstall .apk file into emulator for mobile automation testing in Appium





we have two ways to Uninstall .apk file into emulator

1) By using setting in emulator
2) By using Command Prompt




1) By using setting in emulator (Like Android Phone)

-> Goto setting in emulator
-> Goto App
-> Select required app and uninstall




2) By using Command Prompt

-> Download required .pdk file

-> Launch the Android Emulator and wait till starts properly.

-> Goto command prompt and goto E:\android-sdks\platform-tools and verify the device connected properly (not offline). by using below command

E:\SDK\platform-tools>adb devices

List of devices attached
emulator-5554   device

-> Run below command for install required .apk file in command prompt.

E:\SDK\platform-tools>adb uninstall Calender.apk

Success

-> We will get "Success" message if apk uninstalled in emulator.

-> Goto emulator and verify the calender app is uninstalled or not.

Install .apk file into emulator for mobile automation testing in Appium




Please follow below steps for install .apk file into emulator


-> Download required .pdk file

-> Launch the Android Emulator and wait till starts properly.

-> Goto command prompt and goto E:\android-sdks\platform-tools and verify the device connected properly (not offline). by using below command

E:\android-sdks\platform-tools>adb devices

List of devices attached
emulator-5554   device

-> copy and paste required .apk file into below mentioned path
E:\android-sdks\platform-tools

-> Run below command for install required .apk file in command prompt.


E:\android-sdks\platform-tools>adb install Calender.apk

1910 KB/s (93651283 bytes in 431.338s)
        pkg: /data/local/tmp/Calender.apk
Success


-> We will get "Success" message if apk installed in emulator.

-> Goto emulator and verify the calender app is installed or not.

Appium Setup for Mobile Automation Testing





The below software needs to be install in our machine


1) Download ADT Plug in (Eclipse)
2) Download and Set SDK location
3) Microsoft .Net Framework
4) Appium
5) Node JS



1) ADT Plugin (in Eclipse)


Use following steps to install ADT Plugin

-> Goto Eclipse
-> Goto Help -> Install New Software
-> Here click "ADD" button
-> Then Name = ADT (Any Name)
-> URL = https://dl-ssl.google.com/android/eclipse/
-> Select all software and finish the installation

Refer this link for install ADT plugin -> http://software-testing-tutorials-automation.blogspot.in/2015/09/steps-to-install-eclipse-adt-plugin-to.html


2) Download and Set SDK Location in Eclipse

After install the ADT plugin in eclipse then we set the SDK location in Eclipse

-> once restart the Eclipse after install ADT plugin then its ask confirmation for install SDK.

-> select first option for "install SDK".

-> select first checkbox for install latest SDK

-> select second checkbox and give the path of SDK

-> click yes for send the statistics to the google and click proceed. its take some time to install SDK.



3) Microsoft .Net Framework


Download Microsoft .Net Framework by using this link -> https://www.microsoft.com/en-in/download/details.aspx?id=17851

Refer this link to download -> http://software-testing-tutorials-automation.blogspot.in/2015/09/install-other-required-software-for.html



4) Appium


Download Appium by using this link -> http://appium.io/

Refer this link to download -> http://software-testing-tutorials-automation.blogspot.in/2015/09/how-to-download-and-install-appium-in.html



5) Node JS

 Download Node JS by using this link -> https://nodejs.org/en/download/

Refer this link to download -> http://software-testing-tutorials-automation.blogspot.in/2015/09/install-other-required-software-for.html



6) Download below required jar files and configured with eclipse


gson-2.5
java-client-3.3.0



Launch Android Emulator


Pre-requiste

1) Installed SDK
2) Installed required pakages in Android SDK Manager



2) Installed required pakages in Android SDK Manager

-> Open the Android SDK Manager (Eclipse-> Window-> Android SDK Manager

-> Update Proxy settings in Android SDK Manager (Tools-> Options)

-> Refresh (Android SDK Manager-> Pakages->Reload)

-> Its list out the available pakages. Select required pakage and install