How to run mobile testing using Appium in IntellIj
Appium is an open-source, cross platform tool for automated testing of native, mobile web, and hybrid applications on iOS, Android, and Windows desktop platform.
Supports automation using multiple programming languages like Java, PHP, Perl, Python, etc. So, users can use any programming language they are comfortable with and write automated scripts.
Why appium ?
- Open Source
- Cross Platform : By using appium, we can test applications, whether they are native, hybrid, or web apps, android applications, or iOS. Appium can also be installed on Windows, Mac, and Linux.
- No Source Code : Most applications to test applications such as Selendroid or Robotium need access to the source-code / library of our application, in Appium we don’t need to do this, Appium can test applications only by using the application’s apk alone and applications that are already in the market
- No Reinstallation : In several other automation testing applications, every time you start testing the application it will reinstall the application, but not in appium, every time you run appium you don’t need to reinstall the application again
- Framework Support : Appium gives us the freedom to use whatever programming language we want to use when creating automation scripts
- Community : Appium has a fairly large discussion community and is quite active compared to other application testing applications,
so it can help us when there are difficulties in using Appium
How Appium Work ?
The Appium client will try to communicate by sending a request using the JSON wire protocol to appium server.
The Appium server receives the request from appium client, and will forward the server request to the device through appium driver.
Then the device will respond to the server request by sending server response to Appium Server. Then this server response will be forwarded by the appium server to the appium client.
Appium Drivers
So how do we create automation testing for mobile using Appium?
Well you need to prepare this first :
- Install Jdk : https://www.oracle.com/java/technologies/javase-downloads.html
- Add System Variable :
Name : ANDROID_HOME
Value : Path to your sdk folder (C:\Users\your computer username\
AppData\Local\Android\Sdk) - Emulator : can used genimotion or download emulator in android studio
- IntellIj
- Node.js
Install Appium
Appium can be installed in two ways :
- Via NPM
- Download and Install node.js & NPM : https://nodejs.org/en/download/
https://phoenixnap.com/kb/install-node-js-npm-on-windows (tutorial reference) - Open command prompt as an administrator. Then type this command :
npm install -g appium - Check whether your appium has been installed succesfully by type this command : appium -v. Then it will show the version of appium
- To start appium, you can type command : appium. Then it will start the
appium on 0.0.0.0 : 4723
or if you want to run appium in certain ip and port, you can type command appium -a IPAddress -p Port
- By downloading Appium Desktop
Open Appium Official Website (http://appium.io/downloads.html)
Choose Appium Desktop Apps. Then you will redirect to github page. Choose version of appium and download the file .exe
Install file appium.exe you just downloaded. After installation is complete, run the appium desktop
Create Mobile Automation Testing
First create new project in intellij, pop up “New Project ” will be shown. Click on maven, choose project sdk, and then click checklist on Create from archetype
Click button Add Archetype
Add archetype of serenity-cucumber, you can find it in this link : https://mvnrepository.com/artifact/net.serenity-bdd/serenity-archetypes. Click on the serenity-cucumber version (in here I choose version 2.0.81)
Input GroupId, ArtifactId, and Version in the pop up Add Archetype in the IntellIj with data GroupId, ArtifactId, and Version of serenity-cucumber archetype
And then click Ok. In the box list of archetype, archetype of serenity-cucumber will be shown. Click on the serenity-cucumber archetype and then click Next
In the next step, input Project Name, choose Location of project to be saved, Project GroupId, ArtifactId, and Version, and then click Next.
Make sure that groupId, artifactId, version, archetypeGroupId, archetypeArtifactId, archetypeVersion are already correct, then click Finish
Then your project will be like picture below
Next we want to make automation testing on The App apps.
Download apk of TheApp aps : https://github.com/cloudgrey-io/the-app/releases/download/v1.5.0/TheApp-v1.5.0.apk (https://appiumpro.com/editions/18-using-espresso-with-appium)
Test scenario that we want to run is verify user can login in TheApp. Therefore, first we create new feature file named LoginTheApp.feature. In the LoginTheApp.feature we add Scenario : Verify user can login in TheApp.
Map the scenario line to a Step Definition class named DefinitionSteps
You can hover to one of scenario lines, if yellow light bulp appears, click on the light bulp and then choose Create all step definitions
Then a small pop up “Choose step definition file” will appear and select DefinitionSteps
Then it will add method step definition for each line of scenario
Next for method step userOpenApplication() we want to wait until TheApp application is show in the emulator before execute the next step of scenario. To do that we must give implicit wait to layout of home in TheApp to be visible. How to do that?
First create folder named apps inside the root folder (AppiumTraining)
Then place the apk that was downloaded earlier into the apps folder
Start your emulator. Then open Appium Desktop and then click button Start Server
Then it will show the log of Appium Server. Click on menu File -> New Session Window…
Next click on tab Automatic Server
On tab Desired Capabilities, add :
- app : path of apk The Apps in the folder apps
- platformName : Android
- deviceName : Name of your device ( (you can get this by type “adb devices” in command prompt)
- uuid : your emulator uuid (you can get this by type “adb devices” in command prompt)
- automationName : UiAutomator2 (Appium driver that we use in this tutorial)
Click Start Session
Then Appium will try to run TheApp in the emulator
If this screen is displayed, click button continue
And if pop up like picture below is displayed, click OK.
If screen that displayed in Appium is the pop up not the home page of TheApp, click on Refresh Source & Screenshot
Then it will capture the page that your emulator is currently displaying
Because we want to give implicit wait for layout of home page to be visible, then in appium we inspect to layout of the homepage and get the class name
Next we create java class named LoginTheAppPage in the folder pages. Add extends to PageObject.
Create variable WebElementFacade of the home page layout
And add method to give implicit wait for element of home page layout to be visible
Next add new Step class named LoginTheAppSteps under folder serenity. Then create method to call method waitUntilHomePageAppear() in the LoginTheAppPage.
Next call method waitForHomePageVisible in the method userOpenApplication() in DefinitionSteps
Next we inspect element of Login Screen, input username, input password, button login, and text verify in Appium.
After that create variable WebElementFacade of each element and action for that element (clickLoginScreen, inputUsername, inputPassword, clickButtonLogin, getTextWithUsernameInSecretArea).
Then create new method in LoginTheAppSteps to call each method action of the element, and call it in DefinitionSteps.
Note : add depedency hamcrest so you can use equalTo when validate in assertThat()
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
Next we create java class AppiumDriver to set up Appium capabilities to run the Appium Server
Create package driver under test-> java -> org.appium.training. Then add new java class named AppiumDriver
In the method newDriver(), we set up capabilities of Appium with capabilityName same as capabilities that we set when open Appium Inspect Element earlier. After set up the capabilities then we call the Appium Server by create instance of Webdriver and set url with url of the Appium Server using ip and port where our Appium Server is running.
To tell the our Cucumber to user Appium Driver, then we need to add properties in serenity.properties
In the DefinitionTestSuite, change features to path of LoginTheApp.feature, add plugin = “json:target/destination/cucumber.json”, and add tags with value tag @LoginTheApp in the LoginTheApp.feature
Run the project !!
If the two of picture below is appear, click button Continue and button Ok.
Then you will see in appium will run our automation test scenario in our emulator