Appium
Appium
Let’s go to Android Studio Download and install Android Studio. After that,
apply the following steps. Click the “Next” icons and finally click the
“Finish“.
When took the below screenshot the latest version was 9.0 but for this
example, we will go with Android 11. I am not using windows anymore thus
I could not update the screenshot but I wanted to mention it to guide you
correctly.
And select the required tools as shown below and click “OK.
Click OK one more time, please. ;)
Then, you need to add required Android tools and JAVA JRE paths to
your system path as shown below.
After that, check your settings and installations. Open a command prompt
window and type “sdkmanager –list” command as shown below.
and type “uiautomatorviewer” to check uiautomatorviewer is working
properly.
Then, create a sample project in Android Studio and then click the link as
shown below to install missing libraries.
After installation, click the Finish button.
After installing missing libraries you will see the device and little and sweet
android icon. :) When you click this icon, you will open the android virtual
device manager.
Let’s create a virtual device. I will also explain how to do mobile automation
with a real device too. Don’t worry. ;) Click to “+ Create a Virtual Device”
button.
Until now, we installed JAVA and Android-related libraries and did their
settings and configurations. Now, it is time to download Appium.
When the installation file downloaded, click run and start to install appium
desktop.
When installation finished, double-click the appium icon and open the
appium server as shown below.
Let’s click the “Advanced” tab and change the Server Address to
“127.0.0.1” and click Allow Session Override for override session when
there will be problems and click “Start Server”. If you will use a real device
and then use “0.0.0.0” for “Server Adress”.
Set Android and JAVA home in Appium Desktop.
Give the required permission to Appium Server.
You will see the server up and running.
Android Virtual Device and Pre-Test Settings
Before starting the tests you can directly install your apk or xapk files if you
have. In our example, first, we will install apk pure app.
After please open the apk pure app and search for any app you want to
install. In this example, I will go with the “Isin Olsun” application. My
previous company’s app. :-)
Then, I opened the APK Info app and find our application “İşin Olsun” and
click it.
Then I saw all the information about our app. When I looked at the
Activities section, I saw that our application is starting with
“com.isinolsun.app.activities.SplashActivity“. I will use this info in the
automation code’s desired capabilities section. Also, I will use app package
info which is “com.isinolsun.com“
Then, open a command prompt and write “adb devices” command to see
connected devices and get the device ID as shown below.
Go to your AVD’s setting tab and check your AVD’s Android Version as
shown below. We will use these settings in our test project.
Then, open IntelliJ IDE and create a new project as shown below. First,
select Maven and click Next button.
Then, write your project’s GroupId and ArtifactId. You can write the same
as shown below. It does not affect anything, just naming.
Then, give a name to your project.
Then, click “Enable Auto-Import” in the right bottom corner.
Our POM.xml will look like as below. You can see appium’s, selenium’s,
and TestNG’s dependencies. I used JAVA11 JDK in this example.
<?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>
<groupId>io-appium</groupId>
<artifactId>io-appium</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.5.1</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
-->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Create a new Java Class as shown below and give a name to your test
class. For now, keep it empty, after emulator settings, we will come back to
test code at the end of the article.
Now, let’s start the emulator get your device’s information from the android
studio as shown below.
After starting the application on the emulator, go to the server and click the
magnifier icon to open the inspector.
Then, start to enter the capabilities of your device as shown below in the
inspector.
{
"deviceName": "Pixel XL API 30",
"platformName": "Android",
"automationName": "UiAutomator2",
"platformVersion": "11",
"skipUnlock": "false"
}
Also, you can save these settings and use them later.
and click “Start Session” to start the inspector session to get your mobile
elements ids. Get mobile element’s id’s as shown below. We will use them
for our first mobile automation project.
After all of these steps, we need to write our test automation code.
In emulators, you may face some problems. When I tried to run my test on
the emulator XPath locators did not work. Normally, I did not want to use
XPath but in our app some for some elements I did not have other options.
Then, I tried the same test on a real device, my test worked flawlessly and
very fast. Thus, I suggest you use real devices instead of emulators. In
order to use the real device we should do the followings:
It is time to write some code for our Appium Tutorial. The test code of the
project is shown below. I added inline comments. The most critical part is
DesiredCapabilities, the rest of the code is very similar to Selenium and
TestNG test automation codes. Also, you can find this project on GitHub.
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class ioSampleTest {
public AndroidDriver<MobileElement> driver;
public WebDriverWait wait;
//Elements By
By jobsBy = By.id("com.isinolsun.app:id/rootRelativeView");
By allowWhenUsingBy =
By.id("com.android.permissioncontroller:id/permission_allow_foreground_o
nly_button");
By searchingJobBy =
By.id("com.isinolsun.app:id/bluecollar_type_button");
By animationBy = By.id("com.isinolsun.app:id/animation_view");
By toolBarTitleBy = By.id("com.isinolsun.app:id/toolbarTitle");
@BeforeMethod
public void setup() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Pixel XL API 30");
caps.setCapability("udid", "emulator-5554"); //DeviceId from "adb
devices" command
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "11.0");
caps.setCapability("skipUnlock", "true");
caps.setCapability("appPackage", "com.isinolsun.app");
caps.setCapability("appActivity",
"com.isinolsun.app.activities.SplashActivity");
caps.setCapability("noReset", "false");
driver = new AndroidDriver<MobileElement>(new
URL("http://127.0.0.1:4723/wd/hub"), caps);
wait = new WebDriverWait(driver, 10);
}
@Test
public void basicTest() throws InterruptedException {
//Click and pass Splash
wait.until(ExpectedConditions.visibilityOfElementLocated(animationBy)).clic
k();
//Click I am searching a job
wait.until(ExpectedConditions.visibilityOfElementLocated(searchingJobBy))
.click();
//Notification Allow
if
(wait.until(ExpectedConditions.visibilityOfElementLocated(allowWhenUsing
By)).isDisplayed()) {
wait.until(ExpectedConditions.visibilityOfElementLocated(allowWhenUsing
By)).click();
}
//Click Second Job
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(jobsBy)).ge
t(1).click();
//Do a simple assertion
String toolBarTitleStr =
wait.until(ExpectedConditions.visibilityOfElementLocated(toolBarTitleBy)).g
etText();
Assert.assertTrue(toolBarTitleStr.toLowerCase().contains("detay"));
}
@AfterMethod
public void teardown() {
driver.quit();
}
}
Then, run the test.
And the test should pass as shown below screenshot.