Create Appium Script for Android Real Device’s Chrome Browser
In this post, we are going to create our first Appium script for Android’s chrome browser. In our previous post, we have covered the installation and configuration part that you have to do before creating Appium script. Kindly follow the links provided under Pre-requisite section.
Pre-requisite
- Download and Install Tools Required to Run Appium Script on Android Device
- Create new project in Eclipse and import Selenium Stand alone and Appium Jar file into it. Following article will help you in this regard. Setup Selenium WebDriver in Eclipse.
- Start Appium Server.
- Connect Android real device to your computer and Enable USB Debugging.
Scenario
Here is the scenario that we are going to cover :
1. Launch chrome browser on android real device and then open facebook.com into it.
2. Enter invalid username and password
3. Print error message
4. Close browser.
import org.junit.After; import org.junit.Before; import org.junit.Test; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import io.appium.java_client.remote.MobileCapabilityType; public class FacebookTest { WebDriver driver; @Before() public void setupstart() throws MalformedURLException { DesiredCapabilities capabilities = DesiredCapabilities.android(); capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2"); capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "4100b79b459381f7"); capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome"); driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); } @Test public void testApp() throws InterruptedException { //Launching Facebook.com driver.get("http://www.facebook.com/"); //Entering username driver.findElement(By.name("email")).sendKeys("contact.automateapps@gmail.com"); //Entering passsword driver.findElement(By.name("pass")).sendKeys("test1234"); //clicking on login button driver.findElement(By.name("login")).click(); //Print error message System.out.println(driver.findElement(By.xpath("//div[@id='root']//span")).getText()); } @After public void tearDown() { //closing browser driver.quit(); } }
In you noticed in the above program, Desired Capabilities are mentioned which helps Appium to understand that which browser it has to invoke and on which device etc.
Desired capabilities are basically a set of keys and values which goes to the Appium server to tell that what kind of automation session user is interested in starting up. There are various capabilities which can modify the behavior of the server during automation. You can refer the server capabilities from Appium Doc.
Below We have explained the Appium server capabilities that have been used in our script.
1. Automaton name is UiAutomator2
What is UIAutomator?
UIAutomator or UIAutomator2 are automation frameworks based on Android instrumentation and allows user to run UI tests on Android Device and Appium uses Google’s UIAutomator framework to execute commands on real devices and emulators. Or you can also say that UIAutomator is Google’s test framework for native app automation at the UI level.
Note: UIAutomator2 does not support Android versions below 5.0 and if your android device OS version is below 5.0 then you have to use UIAutomator framework. In our script, we have used UIAutomator2 as we are using Android version 9.
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
2. As we have created this script for android, so we have to tell Appium about the platform where we actually want to run our scrpt. For Iphone, it would be iOS.
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
3. We have to tell the Device name to Appium server so that it could recognize the device before launching the browser on it.
Every Android device has a unique Id and you can find this Id after running the “adb devices” command in command Prompt.
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "4100b79b459381f7");
4. Above script is created for Chrome Browser. We have to communicate the same to Appium by setting Browser Name.
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
5. And last thing is to create an object of RemoteWebDriver class and provide the IP address and port number that we have used while starting the Appium server.
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
If you find the information provided above is useful, please don’t forget to like us on Facebook, you can also leave the comment.
Comments are closed.