How to create your first Android Automated test | Selenium & Appium with Java
I will use IntelliJ IDEA Software for this example, and 365Scores application.
Pre-requisites:
- Have an Android emulator with 365Scores app installed.
- Have the Appium software installed (available for Mac/PC).
Create a new maven project and update the pom file with below mentioned Selenium dependency:
Creating a Maven Project:
You can choose to create a Maven project in IntelliJ IDEA. This is the recommended way when using the Community Edition.
- Select New Project
- In the New Project window, select Maven on the left side of the screen as shown below. If the SDK is already defined in IntelliJ then it will pick up from there, else click New and select the installation folder of the desired JDK.
- Click Next
- Give a Maven GroupID, ArtifactID, and a Version for the project, or use the defaults.
- Click Next
- Enter Project name “AppiumAutomation” and Project Location “Your Desired Location”
- Click on Finish.
- Once the project is created, copy the below dependencies in the pom.xml file.
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.46.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Under the test folder in your project, create a new folder called “java”, and than create a new class called “AndroidAppium”.
Add the below content to the class file:
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
public class AndroidAppium {
WebDriver driver;
@Before
public void setUp() throws MalformedURLException {
// Created object of DesiredCapabilities class.
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName","Nexus");
cap.setCapability("platformName", "Android");
cap.setCapability("appPackage", "com.scores365");
cap.setCapability("appActivity", "com.scores365.ui.Splash");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
}
it should look like this:
What do these steps do ?
- installs the apk file (application) on the connected device,
- start the application
- inform the appium server which kind of session we are interested in (via Desired Capabilities)
- give you the driver object which is of AndroidDriver type
Once you get the driver, from there on it’s more like the Selenium usage of the appium driver and familiar method calls.
What next from here?
The next logical step from here is to create Page class files for different pages in the app and then create an according test class file. However to test the code just written, you can write a small piece of code as below:
@Test
public void firstTest() {
driver.findElement(By.id("btn_setup")).click();
}
In this test,we will click on the “Quick Setup” button and continue with the Wizard process.
That’s all for now. if you want to continue testing this app, you can use the Layout inspector (installed on the Android Studio) and identify the next element ID’s
Enjoy & Share with your friends ;)