qatechcode.files.wordpress.com€¦  · web viewinstall java and configure the java in environment...

30
1. Install java and configure the java in environment variable 2. Download android sdk from this “http://developer.android.com/sdk/index.html#top” and install 3. Download Eclipse Luna or Kepler and configure in your machine Note: Don’t use Eclipse Helios. 4. Add the Adt Pluign to the Eclipse . Open Eclipse>Install New Software 5. Enter this Url https://dl-ssl.google.com/android/eclipse/ and click add 6. Select the developer tool Check box. 7. complete the installation. 8. Open Eclipse and Go to Preference tab >> Select android >>configure the location of sdk and click ok 9. Create Android_home in Environment Variable E:\AccelQ\QA Folder\ mobile Automation\adt-bundle-windows-x86_64-20140702\sdk 10. Add these location to the pathE:\AccelQ\QA Folder\mobile Automation\adt-bundle-windows-x86_64-20140702\sdk\tools; 11. Open CMD and type ANDROID and enter

Upload: others

Post on 23-Aug-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

1. Install java and configure the java in environment variable2. Download android sdk from this “http://developer.android.com/sdk/index.html#top” and install3. Download Eclipse Luna or Kepler and configure in your machine Note: Don’t use Eclipse Helios.4. Add the Adt Pluign to the Eclipse . Open Eclipse>Install New Software

5. Enter this Url https://dl-ssl.google.com/android/eclipse/ and click add6. Select the developer tool Check box.7. complete the installation.8. Open Eclipse and Go to Preference tab >> Select android >>configure the location of sdk and

click ok9. Create Android_home in Environment Variable E:\AccelQ\QA Folder\mobile Automation\adt-

bundle-windows-x86_64-20140702\sdk10. Add these location to the pathE:\AccelQ\QA Folder\mobile Automation\adt-bundle-windows-

x86_64-20140702\sdk\tools;11. Open CMD and type ANDROID and enter

Page 2: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

12. You will find the window open

13. Install all the Android api versions14. Open your real device >>setting>>About phone and check the android version thrice in the

about phone to see the developer option15. Switch to the developer option and enable the Usb Debugging16. Connect your phone and make sure it is detected in ddms tool(pdanet software)

Page 3: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

17. Click the highlighted icon (open perspective) in picture

18. Click DDms in the picture

19. If you not find the ddms Download (pdanet software) and install

Page 4: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

20. Install Nodejs in your machine. https://nodejs.org/en/21. Download and install Microsoft dot net framework 4.522. Downlaod Apium from the http://appium.io and select Download Appium >>

Appiumforwindows.zip23. Open the appium and click start check the server log24. Open the url http://appium.io/downloads.html and download java jar file25. Click the io.appium https://search.maven.org/#search|ga|1|g%3Aio.appium%20a%3Ajava-

client26. Save the jar file 27. Selct no reset in the appium android setup

More information to work on appium

Android Automation using Appium in Windows

So here I am – back with a new post and full of energy and ready to share my knowledge in Appium –

this time on a different OS and an environment platform- Android Automation in Windows!!

Before we begin learning anything new here , let’s retouch some concepts of Appium which makes it a

popular cross-platform test automation tool for automating native, hybrid and mobile web apps and is

widely used for testing on simulators, emulators and real devices.

Page 5: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

Advantages of using Appium :

It uses vendor-provided automation frameworks –o For iOS – Apple’s UIAutomation;o Android 4.2+ – Google’s UiAutomator;o Android 2.3+ – Google’s Instrumentation It uses WebDriver API (i.e ‘Selenium WebDriver‘) which provides a client-server protocol (known as

the JSON Wire Protocol) thus making it possible to write the test code in any language. Appium is a server which is written in node.js and can be built and installed from source or directly

from npm. There are client libraries (in Java, Ruby, Python, PHP, JavaScript, and C#) which can be used instead of

regular WebDriver client. Last but not the least – it is Open-Source.

Appium on Windows (Requirements) :

Following things will be required as we work on appium android automation on windows platform –

1. Appium.exe

2. Android SDK API >= 17

3. Java JDK

4. Apache Ant

5. Eclipse

6. Android Emulator or an Android Device

7. Selenium Server (formerly the Selenium RC Server) version (download link)

8. Appium Client Library jar file (according to your language choice) (download link)

9. ‘.apk‘ file of the app on which tests will be executed.

We would be using java-client here for client library as we discuss further.

Page 6: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

Pre – Conditions –

[i] Set Environment Variables –

Make sure you set up environment variables correctly before executing any script. All these variables

should be set under ‘System variables’ not in ‘User variables’.

Depending on the location where you have jdk, android-sdk and ant installed in your machine, path

should be specified . For example ,in my machine , path is specified as –

JAVA_HOME –o C:\Program Files\Java\jdk1.8.0_25 ANDROID_HOME –o D:\Android_Development\android_sdk ANT_HOME –o C:\apache-ant-1.9.4 PATH variable (would contain System32 , platform-tools , tools ,ant bin and jdk bin set) –o C:\Windows\System32;D:\Android_Development\android_sdk\platform-tools;%JAVA_HOME%\bin;D:\

Android_Development\android_sdk\tools;%ANT_HOME%\bin;

[ii] USB Debugging ‘ON’ –

Make sure the device which you are connecting to your PC to run tests on has Developer

Options enabled and USB debugging option is checked there as shown below –

Page 7: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

Executing Basic Script –

FIRST STEP : Creating a new project

[1] Create a new java project in Eclipse :

Go to ‘File’ -> ‘New’ -> ‘Java Project’

Page 10: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

[1] You will need to export required selenium jar files in your project.

Selenium Server (formerly the Selenium RC Server) version here. Appium Client Library jar file (according to your language choice) from here.

[2] Import all these jar files in your project.

Right click on your project -> Properties. Go to Libraries section -> Add External JARs (Note: I am using java client here) Select both jar files which needs to be added -> click on ‘Open’ -> then tap ‘OK’.

THIRD STEP : Download Appium

Download Appium for Windows zip file here and extract the zip file contents. After Appium is

downloaded , you will find an “Appium.exe” application inside extracted ‘Appium’ folder.

FOURTH STEP : Understanding Appium Parameters

1. Launch Appium.exe and you would see following buttons –

1. Android button

2. Settings button

Page 11: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

3. Developer Settings

4. About

5. Inspector (magnifying glass icon)

6. Launch/Stop button

7. Delete (trash icon)

2. Hover over gear icon and you would see “General

Settings”. Tap on it. Some important points to note here –

1. You would see “Server Address” (IP address on which you want the Appium server to run) and

“Port” (port on which the Appium server will listen for WebDriver commands) as it shows

“127.0.0.1” (localhost) and “4723” respectively here –

2. “Check for Updates” is to check regular Appium updates.

3. “Pre-Launch Application” is to prelaunch the application before any commands start executing.

4. “Override Existing Session” can be checked to override any existing Appium sessions(if any).

Page 12: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

3. Now hover over android icon and you would see “Android Settings. Tap on it and you will see a

window opens up with too many fields.

4. Under “Application” section , some fields which

we can consider here –

1. “Application Path” – You should check this option and can tap on ‘Choose’ and give the path of

your ‘.apk’ file downloaded which you want to test.

2. “Package” – This is the package name of your test application (ex – com.example.testApp)

3. “Launch Activity” – Activity name for the Android activity which needs to be launched from your

package.

5. Under “Capabilities” section –

1. “Platform Name” – mobile platform.

2. “Automation Name” – automation tool (Android or Selendroid).

3. “Platform Version” – mobile platform version.

4. “Device Name” – name of the device you would be testing on.

Page 13: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

6. Hover on the magnifying glass and you can all the elements accessible on the screen. Appium should

be running for inspector to work. An example screenshot of “Settings” section of an android device

can be viewed in Inspector as –

7. Through appium inspector – you can view name , value , label , xpath of different elements on the

screen. Given below are some examples how to use elements from the screen using xpath ,

tagName, name, attribute etc –

1. //webdriver declaration2. public WebDriver driver ; 3.4.5. //using xpath6. driver.findElement(By.xpath("//window[1]/scrollview[1]/button[1]"));7.8.9. //using name10. driver.findElement(By.name("Go"));11.12.13. //using attribute –14. WebElement x = driver.findElement( By.xpath("//window[1]/tableview[1]/cell[2]/text[1]");15. x.getAttribute("name"); //get name of the element16.17.18. //using tagName19. driver.findElement(By.tagName("button"));20.21.22. //sendkeys can be used for giving some input in any field

Page 14: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

23. WebElement login_email = driver.findElement(By.xpath("//window[1]/scrollview[1]/textfield[1]"));

24. login_email.click();25. login_email.sendKeys("[email protected]");26.27.28. //provide delay between two event clicks29. Thread.sleep(time in milliseconds);

Note – You can run your test scripts either in simulator or on real device. Refer about Appium

parameters detailed information here.

FIFTH STEP : Writing Script

Now we will have a look at one sample script.

Following desired capabilities need to be taken care of while writing script for android automation

(Desired capabilities are a set of keys to communicate with Appium server to tell the server what kind of

automation session you are interested in) –

platformName (mobile platform) platformVersion (mobile platform version) deviceName (name of device on which you are running test) app (path to your ‘.apk’ file) appPackage (app package) appActivity (main activity class)

1. package com.smriti.firsttest;2.3. import java.net.URL;4. import org.openqa.selenium.By;

Page 15: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

5. import org.openqa.selenium.WebDriver;6. import org.openqa.selenium.remote.DesiredCapabilities;7. import org.openqa.selenium.remote.RemoteWebDriver;8.9. public class FirstTestCases {10.11. // webdriver declaration12. public WebDriver driver;13.14. public static void main(String[] args) throws Exception {15.16. // creating a class object17. FirstTestCases testcases = new FirstTestCases();18.19. // call launch app method20. testcases.launchApp();21.22. // call add name method23. testcases.addName();24. }25.26. public void launchApp() throws Exception {27.28. System.out.println("Launching App");29. DesiredCapabilities capabilities = new DesiredCapabilities();30. capabilities.setCapability("platformName", "Android");31. capabilities.setCapability("platformVersion", "4.3");32. capabilities.setCapability("deviceName", "S3");33. capabilities.setCapability("app", "D:\\Apps\\TestApp.apk");34. capabilities.setCapability("app-package", "com.example.testApp");35. capabilities.setCapability("app-activity", ".SplashActivity");36. driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),37. capabilities);38. Thread.sleep(20000); // delay of 20s39. System.out.println("App Launched");40. }41.42. public void addName() throws InterruptedException {43.44. System.out.println("Adding Name");45. String myname = "Smriti";46. driver.findElement(By.name("Add")).click();47. driver.findElement(By.xpath("//textfield[1]")).sendKeys(myname);

Page 16: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

48. driver.findElement(By.name("Save")).click();49. Thread.sleep(5000); // 5s delay50.51. // write all your tests here52.53. }54. }

SIXTH STEP : Run Script

After writing the test script , follow these steps to run your test –

1. Open Appium.exe.

2. Go to ‘General Settings’ and check that ‘Server Address’ and ‘Port’ should be set as “127.0.0.1” and

“4723” respectively.

3. Go to ‘Android Settings’ add your ‘.apk’ file path(path where you downloaded the apk file in your

machine) under “Application Path”. You will notice that it takes up ‘Package’ and ‘Launch Activity’

paths automatically when you enter the apk file path.

4. Under ‘Capabilities’ section –

1. Make sure ‘Platform Name’ and ‘Automation Name’ are set to ‘Android’ and ‘Appium’

respectively considering you aren’t using older android versions. If yes , chose ‘Selendroid’.

2. Select ‘PlatformVersion’ based on mobile platform version you are working on.

3. Select checkbox next to Device Name and mention your device name there say “S3” if my

Samsung S3 device name is S3.

5. Now trigger Appium server by tapping on launch button.

6. Now go to Eclipse and run your project as a ‘Java Application’ –

Page 17: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

7. Once Appium server is triggered and your test app

launches , you can tap on Appium Inspector and view all accessible elements on any active screen

and write test cases accordingly and execute.

There you go running your first test script in Android platform using Appium in Windows

So this was all about Android Mobile Automation using Appium tool on Windows platform. Stay tuned

for more posts!!

iOS Mobile Automation using AppiumThis time I started with a new tool which I had heard about earlier but didn’t start working on it.

Earlier I had worked for some time on UI Automation Library using Instruments tool for iOS

automation.

Appium – an open source, cross-platform test automation tool which can be used for native,

hybrid and mobile web apps. It requires :

Mac OS X 10.7 or higher, 10.8.4 recommended XCode >= 4.6.3

It drives various native automation frameworks and provides an API based on Selenium’s

WebDriver JSON wire protocol. It drives Apple’s UIAutomation library for iOS support.

 

Page 18: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

Appium can be useful for automation as :

We can write tests with Selenium WebDriver API and language specific client libraries with any of the WebDriver compatible language say Java , Objective-C , Javascript , PHP , Python , C# , Ruby , Clojure or Perl.

Any testing framework can be used.

So I thought of trying this tool out with Java language in Eclipse as I have been using the same

for my Android automation as well and I chose TestNG framework. Testing framework will be

required if you want any way to log your results for your purpose otherwise you can even skip

this.

 

To start with , you will be need :

Selenium Server in order to run either Selenium RC style scripts or Remote Selenium Webdriver ones. Language specific client drivers TestNG framework (optional) Appium app to launch appium server Eclipse

How   to   use   Appium   –

Appium uses ‘Appium Inspector’ to view the elements similar to what we have as ‘uiautomatorviewer’ tool to view UI components in Android in UI Automator. You can access elements using name , xpath or tagName as visible through appium inspector.

Appium Inspector can be launched by clicking on the blue ‘i’ button besides ‘Launch’ button from appium app.

Through appium inspector – you can view name , value , label , xpath of different elements on the screen.

Some examples how to use elements from the screen using xpath , tagName, name, attribute is

here –

 

1. //webdriver declaration2. public WebDriver driver ; 3.

Page 19: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

4. //using xpath5. driver.findElement(By.xpath("//window[1]/scrollview[1]/button[1]"));6.

7. //using name8. driver.findElement(By.name("Go"));9.

10. //using attribute –11. WebElement x = driver.findElement(By.xpath("//window[1]/tableview[1]/cell[2]/text[1]"));12. x.getAttribute("name"); //get name of the element13.

14. //using tagName15. driver.findElement(By.tagName("button"));16.

17. //sendkeys can be used for giving some input in any field18. WebElement login_email =

driver.findElement(By.xpath("//window[1]/scrollview[1]/textfield[1]"));19. login_email.click();20. login_email.sendKeys("[email protected]");21.

22. //provide delay between two event clicks23. Thread.sleep(time in milliseconds);

 

NOTE   – To run automation scripts for iOS , you need a Mac machine. It can’t be done on

Windows as Xcode   needs to be present for Appium to work. 

You will not be able to test iOS apps on a locally hosted server, because Apple’s

instruments binary, which Appium uses to launch the iOS simulator by default uses the

currently-selected Xcode, and the highest iOS SDK installed with that version of Xcode.

 

 

Executing  Basic  Script – 

Page 20: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

FIRST   STEP : Creating   a   new   project

[1] Create a new java project in Eclipse :

Go to ‘File’ -> ‘New’ -> ‘Java Project’

 

 

[2] Add project name and click ‘Next’ –

 

Page 21: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

[3] Click ‘Finish’ –

 

[4] Expand your project. Right click on src folder -> ‘New’ -> ‘Class’ –

 

[5] Enter ‘Name’ for class. Also mention ‘Package’ and then click ‘Finish’ –

Page 22: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

 

[6] You will see something like –

 

 

SECOND   STEP   :   Adding   required   jar   files

[1] You will need to export required selenium jar files and install your testing framework (if

required) in your project.

For that , download :

Page 23: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

i) Selenium Server (formerly the Selenium RC Server) version and

ii) Required client driver (according to your language choice) under – Selenium Client &

WebDriver Language Bindings.

Selenium server and client drivers can be downloaded from here

– http://docs.seleniumhq.org/download/   .

 

[2] Import all these jar files in your project.

Right click on your project -> Properties -> Libraries -> Add External JARs. Add all three

selenium jar files here.

 

Optional – If you want to use TestNG  :  Go to Help -> install new software -> use

http://beust.com/eclipse/ :

 

Page 24: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

THIRD   STEP   –

Download Appium app depending on your platform either Mac/Win

–https://bitbucket.org/appium/appium.app/downloads/  

You can use  ‘IP Address’ as ‘127.0.0.1’ and port ‘4723’.

 

FOURTH   STEP   –

To run scripts on real device , you will need “UDID” (Device ID) and “Bundle ID” (Application

Bundle ID) of your app :

To run scripts on simulator , you will need “.app” and “Bundle ID” of your app (Force device

also can be left unchecked)  :

Page 25: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

NOTE : You can run your test scripts either in simulator or on real device but I would

recommend using iOS simulator as it responds faster than real device. Desired capabilties need

to be updated depending on whether you run on simulator or real device.

(Desired capabilities are a set of keys to communicate with Appium server to tell the server what

kind of automation session you are interested in).

To Run on Simulator   –

1. public WebDriver driver = null;2. DesiredCapabilities capabilities = new DesiredCapabilities();3. capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");4. capabilities.setCapability(CapabilityType.VERSION, "6.1");5. capabilities.setCapability(CapabilityType.PLATFORM, "Mac");6. capabilities.setCapability("device", "iPhone");7. capabilities.setCapability("app","/Users/abc/Desktop/MyiOSApp.app");8. driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

 

To Run on Real Device   –

1. public WebDriver driver = null;2. DesiredCapabilities capabilities = new DesiredCapabilities();

Page 26: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

3. capabilities.setCapability("device", "iPhone");4. capabilities.setCapability("udid", "1234567890abcdef");5. capabilities.setCapability("bundleid", "com.example.appiumiphonetest");6. capabilities.setCapability("ipa", "MyiOSApp.ipa");7. driver = new RemoteWebDriver( new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

 

There are two ways to get the “.app” of your application :

[1] You can build your app using xcode. Not down the build path of your application – that

would be the location where your ‘.app’ file will be created.

Ex – I ran my build on iPhone simulator 4-inch , 7.0.3. My ‘.app’ was generated at the following

path :

‘/Users/Library/Application Support/iPhone Simulator/7.0.3/Applications/’.

[2] If your app is in store :

– Download app from itunes store. Then open that file in finder.

– There will be one plist file -> you can get bundle id from there.

BASIC SCRIPT –

1. package com.smriti.firstest;2.

3. import java.io.File;4. import java.net.URL;5. import java.util.List;6.

7. import org.openqa.selenium.By;8. import org.openqa.selenium.WebDriver;9. import org.openqa.selenium.WebElement;10. import org.openqa.selenium.remote.CapabilityType;11. import org.openqa.selenium.remote.DesiredCapabilities;12. import org.openqa.selenium.remote.RemoteWebDriver;13. import org.testng.Assert;14. import org.testng.annotations.AfterMethod;

Page 27: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

15. import org.testng.annotations.BeforeMethod;16. import org.testng.annotations.Test;17.

18. public class FirstTest {19.

20. public WebDriver driver = null;21.

22. @BeforeMethod23. public void setUp() throws Exception {24.

25. // set up appium26.

27. File appDir = new File("/Users/abc/Desktop/smriti/iPhoneSimulator");28.

29. File app = new File(appDir, "XYZ.app");30.

31. DesiredCapabilities capabilities = new DesiredCapabilities();32.

33. capabilities.setCapability(CapabilityType.BROWSER_NAME, "");34.

35. capabilities.setCapability(CapabilityType.VERSION, "7.1");36.

37. capabilities.setCapability(CapabilityType.PLATFORM, "Mac");38.

39. capabilities.setCapability("app", app.getAbsolutePath());40.

41. driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),42. capabilities);43.

44. System.out.println("App launched");45.

46. }47.

48. @AfterMethod49. public void tearDown() throws Exception {50. driver.quit();51. }52.

53. @Test54. public void testCases() throws InterruptedException {55.

56. String myname = "Smriti";57.

Page 28: qatechcode.files.wordpress.com€¦  · Web viewInstall java and configure the java in environment variable. Download android sdk from this “€ and install

58. driver.findElement(By.name("Add")).click();59. driver.findElement(By.xpath("//textfield[1]")).sendKeys(myname);60. driver.findElement(By.name("Save")).click();61.

62. Thread.sleep(5000);63.

64. // write all your tests here65.

66. }67. }

I copied my ‘.app’ file from that location to a folder in my local here – ‘/Users/abc/Desktop/smriti/iPhoneSimulator. That is what it shows in code :

1. File appDir = new File("/Users/abc/Desktop/smriti/iPhoneSimulator");2. File app = new File(appDir, "XYZ.app");

@BeforeMethod is for appium setup and app launch. @Test is for writing all test cases after app launch . @AfterMethod is after test cases are complete.

 

I have started using appium for native mobile iOS apps automation. Let’s see how it goes