Create Appium script on android mobile web

In this post, we are going to explain you the 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 executing Appium script. You can read that post from this link.

First of all, create a new project in Eclipse and include following mentioned jars in your project.

Pre-requisite

    • Add Google Play Store on Genymotion emulator so that you can install applications available on play store. As of now, just install chrome browser. Refer this video.
    • Appium server should be started.

AppiumServer

    • Emulator should be connected and opened.

Emulator-cmd

Scenario

Here is the scenario that we are going to cover :

1. Launch chrome browser on Genymotion emulator and then open facebook.com into it.
2. Enter invalid username and password
3. Print error message
4. Close browser.

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class FacebookTest {

    WebDriver driver;

    @Before()
     public void setupstart() throws MalformedURLException
     {
            DesiredCapabilities capabilities = DesiredCapabilities.android();
            capabilities.setCapability("automationName", "Appium");
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("deviceName", "192.168.215.101:5555");
            capabilities.setCapability("browserName", "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("automationName", "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("platformName", "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 ID after running the “adb devices” command. To know more about how to run this command. Refer our previous post.

capabilities.setCapability("deviceName", "192.168.215.101:5555");

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

capabilities.setCapability("browserName", "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 really like the information provided above, please don’t forget to leave the comment. You can also like us on Facebook.

Comments are closed.