Earlier we learnt how to run android automation test in real android device using appium as described in THIS POST. So I am suggesting you to read It before running appium test in AVD. Also you can run appium test on android emulators (Android virtual device). We will learn how to run first appium test in android emulator in this appium tutorial post. We will use android emulator's build in installed calculator app to create and run appium automation test. Let's start it.
PREREQUISITES :
- All previous 16 steps of appium tutorial given on this LINK and this LINK should be completed without any error.
- Android emulator should be created.
- TestNG should be installed in eclipse. View THIS POST.
1. Start Appium Node Server
Start appium node server as described in THIS POST.
2. Start Android Virtual Device (Emulator)
Once appium starts properly, You need to launch android virtual device as described in THIS POST.
3. Get Emulator Platform Version
Also you need to provide android emulator platform version. You can get it as described bellow.
- Unlock Android emulator screen.
- Go to Settings. You will find About Phone under settings.
- Go to About Phone.
- It will show you Android version as shown in bellow image. For me it is 5.1
Note down android version for your emulator.
4. Verify calculator App Is Available In Emulator
We are going to run appium test for calculator application so it should be there in emulator. Generally calculator app will be already installed in emulator. To check if it is installed or not,
- Unlock emulator.
- Verify if there is any application with name Calculator as shown in bellow image. You can download Android Calculator App from THIS PAGE if it is not available with you.
5. Get app Activity and Package Name
We need launcher activity and package name of calculator app. You can use any of the method from THIS PAGE or THIS PAGE to get activity and package name of calculator app.
Activity and package name of calculator app for me is as bellow.
Package name : com.android.calculator2
Activity name : com.android.calculator2.Calculator
Now we are ready to create and run our first appium test on android emulator for calculator application. I have prepared appium test script as bellow. I have used RemoteWebDriver of selenium webdriver to launch app with required capabilities.
Create new class file under Android package in eclipse with name SimpleEmulatorCalcTest and paste bellow given test script in it.
SimpleEmulatorCalcTest.java
package Android;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
public class SimpleEmulatorCalcTest {
WebDriver driver;
@Test
public void setUp() throws Exception {
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();
// Set android deviceName desired capability. Set it Android Emulator.
capabilities.setCapability("deviceName", "Android Emulator");
// Set browserName desired capability. It's Android in our case here.
capabilities.setCapability("browserName", "Android");
// Set android platformVersion desired capability. Set your emulator's android version.
capabilities.setCapability("platformVersion", "5.1");
// Set android platformName desired capability. It's Android in our case here.
capabilities.setCapability("platformName", "Android");
// Set android appPackage desired capability. It is com.android.calculator2 for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appPackage", "com.android.calculator2");
// Set android appActivity desired capability. It is com.android.calculator2.Calculator for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
// Created object of RemoteWebDriver will all set capabilities.
// Set appium server address and port number in URL string.
// It will launch calculator app in emulator.
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Click on CLR button.
driver.findElement(By.name("del")).click();
//OR you can use bellow given syntax to click on CLR/DEL button.
//driver.findElements(By.className("android.view.View")).get(1).findElements(By.className("android.widget.Button")).get(0).click();
// Click on number 2 button.
driver.findElement(By.name("2")).click();
// Click on + button.
driver.findElement(By.name("+")).click();
// Click on number 5 button.
driver.findElement(By.name("5")).click();
// Click on = button.
driver.findElement(By.name("=")).click();
// Get result from result text box of calc app.
String result = driver.findElement(By.className("android.widget.EditText")).getText();
System.out.println("Number sum result is : " + result);
driver.quit();
}
}
Running First Appium Test In Emulator
Now we are ready to run appium test for calculator app in emulator.
Note : Please make sure appium node server and emulator are launched and appium server is started by pressing start button.
Run test using testng and observe test execution in android emulator. It will
- Open calc app in emulator.
- Tap on calc app's buttons in this sequence -> CLR, 2, +, 5 and =.
- Get result from text area of calculator app.
- Print result in eclipse console.
This way you can run appium test in android emulator.
its superb tutorial..thanks a lot for giving such a understandable instruction..thank you
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehow we can verify the Emulator name is correct?
ReplyDeleteand for Emulator should we do any port setting?
There is no port setting for Emulator. You can give any name for emulator.
DeleteWhy do i always get error " org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Could not find a connected Android device.)", when my devices are displayed in list of connected devices? I tried both virtual device and real device connected with USB.
ReplyDeleteI always get "org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Could not find a connected Android device.)", instead i am using real device or emulator.
ReplyDeleteWhat is the method to get desired capabilities from .extrenal source and call it in our program
ReplyDelete