Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Best Practices In Appium

DZone 's Guide to

Best Practices In Appium

A guide to using Appium.

· Performance Zone ·
Free Resource

Many times, in Appium we face uncertain issues with the AVD Manager, ADB, Emulators, uiautomatorviewer and with MobileWebScript execution.

Common Reasons for Errors

  • Configuration Errors
  • Environment Variable
  • Installation Setup
  • Browser Driver version and Binary Path

Project Setup:

1) JDK - http://www.oracle.com/technetwork/java/javase/downloads/index.html

Set JAVA_HOME Variable In Windows

2) Eclipse - http://www.eclipse.org/downloads/

3) Android SDK - https://developer.android.com/studio/index.html

Set ANDROID_HOME and Path environment variables.

User Variables - ANDROID_HOME - C:\Users\<username>\AppData\Local\Android\sdk

C:\Users\<username>\AppData\Local\Android\sdk\tools

C:\Users\<username>\AppData\Local\Android\sdk\platform-tools

4) Selenium WebDriver Jar - Selenium Java , Java Client

5) Appium for Windows

1)    Create a New Maven Project

File -> New -> Maven Project

Image title

2)    Click on Next -> Next -> Next and Click on Finish

Image title

3)    Add Below Dependencies on POM.xml

<dependencies>
    <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>

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

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson ///   2.8.2  -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>  

  </dependencies>

4)    Create a Test for Calculator — Native Application

public class CalculatorDemo {
AndroidDriver<WebElement> driver;

@BeforeTest
public void beforeTest() throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "9");
cap.setCapability("deviceName", "emulator-5554");
cap.setCapability("appPackage", "com.android.calculator2");
cap.setCapability("appActivity", "com.android.calculator2.Calculator");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void login() {

driver.findElementByXPath("//android.widget.Button[@text='5']").click();
System.out.println("Clicked on 5");

driver.findElementByXPath("//android.widget.Button[contains(@resource-id,'com.android.calculator2:id/op_mul')]").click();;
System.out.println("Clicked on X");

driver.findElementByXPath("//android.widget.Button[@text='6' and @index='5']").click();
System.out.println("Clicked on 6");

driver.findElementByXPath("//android.widget.Button[@content-desc='equals']").click();
System.out.println("Clicked on =");

WebElement results=driver.findElementById("com.android.calculator2:id/result");
System.out.println("Multiplication Result is : "+results.getText());

assert results.getText().equals("30"):"Actual value is : "+results.getText()+
" did not match with expected value: 30";
}

@AfterTest
public void afterTest() {
driver.closeApp();
}
}

5)The Result Will Display Like Below:

Image title

6)    Create New Test for Mobile Web Application


public class LoginHerokuApp {
AndroidDriver<WebElement> driver;

@BeforeTest
public void beforeTest() throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9");
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
cap.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.CHROME);

driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
}

@Test
public void appLogin() {
driver.get("https://the-internet.herokuapp.com/login");
System.out.println("Website Opened");

driver.findElementById("username").sendKeys("tomsmith");
System.out.println("Entered Username");

driver.findElementByName("password").sendKeys("SuperSecretPassword!");
System.out.println("Entered Password");

driver.findElementByClassName("radius").click();
System.out.println("Clicked on Login button");
}

@AfterTest
public void afterTest() {
driver.close();
}

}

7)    The Result Will Display Like Below:

Image title

8)    Create New test for Hybrid Application:

@Test
public void demoLongPress() {

driver.findElement(MobileBy.AccessibilityId("Views")).click();
System.out.println("Clicked on Views Tab");

driver.findElement(MobileBy.AccessibilityId("Expandable Lists")).click();
System.out.println("Clicked on Expandable Lists Tab");

driver.findElement(MobileBy.AccessibilityId("1. Custom Adapter")).click();
System.out.println("Clicked on 1. Custom Adapter");

TouchAction action = new TouchAction(driver);
WebElement element1 = driver.findElement(MobileBy.AndroidUIAutomator("text(\"People Names\")"));
action.longPress(element1).perform();
}

Errors in Appium and Solutions

1)    Error getting while trying to launch AVD manager

Solution: Install Intel Haxm to run AVD.

Steps to install HAXM:

  • Go to http://software.intel.com/en-us/android.
  • Select the Intel HAXM installer package for your platform.
  • Extract the installer folder and follow installation instructions for your platform.

2)    When we are trying to capture the screenshot in uiautomatorviewer it shows error like ‘Unexpected error while obtaining UI Hierarchy’.

Solution :

  • Open a Command prompt and execute below command.
    • adb kill-server
    • adb start-server

3)    If your working with MobileWeb application and your not able to execute your test on chrome browser.
Solution:

This error is because of the chromedriver version.
Follow the below steps:
1)    Download chromedriver 2.43
2)    Extract ‘chromedriver_win32.zip’ folder
3)    Open Appium Server – Set Chromedriver Binary Path – C:\Appium\chromedriver_win32\chromedriver.exe path.

Topics:
appium ,appium testng ,mobile application testing ,selenium ,android testing ,open source test tools

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}