Appium supports both iOS and Android. Appium is HTTP server. It receives the commands from client, executes the command on Mobile device and send a HTTP response representing the result of the command execution.
I am going to use Android studio to create a virtual emulator. And Selenium WebDriver will be used to write the Client. I would prefer to use the virtual emulator instead of using real device. In actual devices you may find difficulties to connect/detect it.
Pre-requisites
- .apk file is required to install in Mobile device (Virtual emulator).
- Android Studio, Appium desktop server and intelliJ IDEA have to be installed.
Virtual emulator
This post guides you to create the Virtual emulator using Android Studio.Once you launch the emulator now It's time to install the mobile application which is going to be automated. Drag and drop the apk file and do the installation in the Mobile device.
Appium Server
Once the installation is done, Server can be started with the default Host and Port. If you want to make any changes, click the Advance button. Here I am going to use the default Host and Port.Appium Server successfully started on port 4723.
Now we have to connect the Appium server and the Virtual emulator. Click on Start Inspector Session to provide the Emulator/ App information.
You can provide the details as key value pair and save it for future use, Which saves as JSON file. Once the information given you can start the session to inspect the Mobile app elements.
- Device name can be found in Android Studio virtual emulator.
Android Studio -> Tools -> AVD manager
- Platform name and Platform version can be found in emulator info.
Android Studio -> Tools -> AVD manager -> Select Virtual emulator -> Click on edit button
- To get the information regarding appPackage and appActivity install APK application in your emulator.
- Open the APK app and select the mobile app which you have been installed. Under Activities you can find the details. This may differ application to application.
Client code
We are going to use intelliJ IDEA as the developing environment. And make sure you are creating a Maven Project So the relevant libraries can be imported as Maven dependencies in POM file. We will use Selenium WebDriver, Java, Appium and TestNG.Once you imported the maven dependencies your pom file will look like this.
<?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>appiumAndroidProject1</groupId> <artifactId>appiumAndroidProject1</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-support</artifactId> <version>3.14.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>7.0.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.3</version> <scope>test</scope> </dependency> </dependencies> </project>
Create a Java Class by giving a meaningful name, To get connect with the server and emulator we have to provide the information regarding the Virtual emulator and the Appium Server port number in Client code.
I have used @BeforeMethod annotation since I want to create the connection between the server and client before execute the command.
Elements have been found from the Appium Inspect element.
import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; 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; import java.net.MalformedURLException; import java.net.URL; public class Test001 { public AndroidDriver<MobileElement> driver; public WebDriverWait wait; @BeforeMethod public void setup() throws MalformedURLException { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("deviceName", "Nexus 5X API 25"); caps.setCapability("udid", "emulator-5554"); caps.setCapability("platformName", "Android"); caps.setCapability("platformVersion", "7.1.1"); caps.setCapability("skipUnlock", "true"); caps.setCapability("appPackage", "com.mock.app"); caps.setCapability("appActivity", "com.mock.app.ui.activities.DomainActivity"); caps.setCapability("noReset", "false"); driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps ); wait = new WebDriverWait(driver, 10); } @Test public void login() throws InterruptedException { driver.findElement(By.id("com.mock.app:id/txt_companyname")).sendKeys("comp"); driver.navigate().back(); driver.findElement(By.id("com.mock.app:id/btn_domain")).click(); wait.until(ExpectedConditions.visibilityOfElementLocated (By.id("com.mock.app:id/username"))).sendKeys("user"); driver.findElementById("com.mock.app:id/password").sendKeys("password"); driver.findElementById("com.mock.app:id/login").click(); String title = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("com.mock.app:id/mainTitle"))).getText(); Assert.assertEquals(title, "title"); System.out.println("Successfully logged in to the system!"); } @AfterMethod public void teardown(){ driver.quit(); } }When you run the test case it automatically opens the app in you emulator and start execute the commands which you have written in @Test.
Comments
Post a Comment