Appium Tutorial
Create First Appium Test
How To Create First Test Using Appium ?
To create a test in appium, follow these steps :
Create a Maven Project.
Add dependencies in pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>AndroidProject</name>
<url>http://maven.apache.org</url>
<groupId>org.example</groupId>
<artifactId>AndroidProject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.4.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
</dependencies>
</project>
Create a package in src/test/java as 'appiumexamples'.
Create a testng class within this package.
To inspect a web element :
Launch the emulator.
Go to the desired mobile page.
Now, start Appium Server.
Click on Search icon.
It will open a session window.
Add Desired capabilites as shown in the image :
Start session.
A window will be opened that contains the mobile screen view of the emulator.
Right-click on this mobile screen view and click on Inspect Element.
Locate the webelement.
To know the udid of device :
Run command 'adb devices' on command prompt.
It will show a list of attached emulator.
The integers written next to emulator is its udid.
Add below lines of code in testng class :
package appiumexamples;
import org.testng.annotations.Test;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.testng.annotations.BeforeClass;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.FindBy;
import org.testng.annotations.AfterClass;
public class FirstAppiumTest {
AndroidDriver<MobileElement> driver;
@FindBy(xpath = "//*[@text='AUTHENTICATE']")
WebElement authenticationButton;
@Test
public void firstAppiumTest() {
authenticationButton.click();
driver.findElementByAccessibilityId("Start").click();
}
@BeforeClass
public void startSession() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Pixel 3 API 28"); // device Name
caps.setCapability("udid", 5554); // DeviceId from "adb devices" command
caps.setCapability("platformName", "Android");
caps.setCapability("skipUnlock", "true");
caps.setCapability("app","C:\\Users\\Dell\\appiumProject\\AndroidAppium\\src\\main\\resources\\apk\\cleardil_sdk_android_example_1.0.0.apk");caps.setCapability("noReset", "false");
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
}
@AfterClass
public void closeSession() {
driver.closeApp();
}
}
Launch the emulator.
Start the Appium Server.
Run as 'TestNG Test'.