This article will show you how to run first automation script for testing android mobile app using appium server.
Before that, we need to create a new maven project and update the pom file with below mentioned Selenium dependency. I am assuming you are familiar with Java project creation in Eclipse. Let me include a brief step by step guide to creating a project.
- Open Eclipse ( I’m using : Neon.1a Release (4.6.1) )
- Click on File > new > Maven Project
- Click on Next > Click on Next >
Where Artifact Id is your project name, Group Id is your project path folder structure
- Click on Finish
Once the project is created, copy the below dependencies in the pom.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.1</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.1.1</version> <scope>test</scope> </dependency> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>3.0.0</version> <scope>test</scope> </dependency> </dependencies> |
Just we created the maven project with updated pom.xml file and we need to launch some tools.
- UiAutomator viewer — Used to find out android mobile app elements (id, classname, XPath).
- Appium server — inform the appium server which kind of session we are interested in (via Desiredcapabilities ).
- Real Device / Emulator.
UiAutomator viewer: Which is used to find out android mobile app elements like id. Classname, XPath.
If you want to open the UiAutomator viewer, Go through this path
Ex: C:\Users\akumar\AppData\Local\Android\sdk\tools
- Connect your android device to PC via USB cable
- Enable your device in “Debugging mode”.
- Click on uiautomatorviewer > Click on this image button to refresh the device screen in Automator viewer.
- Make sure before click on above image button, we need to launch our android mobile with our app (Which we want to test the app).
Ex; Here I’ m using the “ dialer “ android app for making a call to someone
- After that, we need to click on image button which I have marked as red color.
- clicking on the each node gives you properties of UI element in the lower panel
By the above Image will show you how to take android mobile app elements ( id, text, classname ).
- text attribute can be used as “name”
- a resource-id attribute can be used as “id”
- class attribute can be used as “className”
- a content-desc attribute can be used as “AccessibilityId”
If you want XPath, you can write manual XPath using below syntax
Locating Android App Element By XPath:
- XPath using class and text attribute:
Ex: Take Calculator android app:
Syntax: //class_name[@Attribute=’Value’]
driver.findElement(By.xpath(“//android.widget.Button[@text=’5′]”))
IMP :- I will show you one example about Dialer app testing, just copy the code and paste it inside your java class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.Test; import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; public class MakingACall { AndroidDriver<MobileElement> driver; @Test public void call() throws MalformedURLException { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "Moto E (2nd Generation)"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("platformVersion", "5.1"); capabilities.setCapability("appPackage", "com.android.dialer"); capabilities.setCapability("appActivity", "com.android.dialer.DialtactsActivity"); driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS); driver.findElement(By.id("floating_action_button")).click(); driver.findElement(By.id("one")).click(); driver.findElement(By.id("two")).click(); driver.findElement(By.id("three")).click(); driver.findElement(By.id("four")).click(); driver.findElement(By.id("five")).click(); driver.findElement(By.id("six")).click(); driver.findElement(By.id("seven")).click(); driver.findElement(By.id("eight")).click(); driver.findElement(By.id("nine")).click(); driver.findElement(By.id("zero")).click(); driver.findElement(By.id("dialpad_floating_action_button")).click(); driver.findElement(By.id("select_account_list_item_view")).click(); driver.quit(); System.out.println("Success"); } } |
Then Start appium server, below steps, show you how to start appium server on command prompt
- Navigate to C: -> Program Files -> Appium -> node_modules folder.
Ex: C:\Program Files (x86)\Appium\node_modules
- Press and hold Ctrl + Shift Keys of keyboard and press mouse right click.
- Select “Open command Window here” from the context menu.
- It will open a command prompt with navigation to given node_modules folder path.
- Type command ” node appium ” In command prompt and press enter then appium server will start.
Note: All set for running our script, make sure before running our script we need to launch first appium server.
Thank you for reading this article!