Create Appium Script for Android Real Device’s Chrome Browser

In this post, we are going to create our first Appium script for android device 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

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, "appium");
            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 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 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 Appium

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "appium");

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. You will get this uniqueID after running the “adb devices” command in command Prompt.

capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "4100b79b459381f7");

4. As we want to open chrome browser on android, so we have mentioned browser name as chrome.

capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");

5. Here, we have to 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.