Browser testing on Android can become tedious if you do not have proper tools to run tests. This blog shows some basic tests to start browser testing on android devices (locally and on cloud). Note that these tests are not running on an app, but on a browser in Android. The most popular tool for running mobile tests is Appium which can be controlled by selenium-webdriver in javascript.

The runner can be anything from selenium-webdriver or wrappers like webdriver.io or protractor.

Below are few examples of how you can achieve testing of mobile site on laptop and an android device.

Android Device

  • Download Appium. You will have to set PATH for the appium binary to run it from anywhere. There is a very handy tool, appium-doctor which you can use to check if all dependencies for Appium are installed in your machine.

  • Once you have Appium installed, run it on your machine. By default, Appium runs at port 4723.

  • Use following code to run the test. Notice that the test is not running an APK file in the device. It is just opening a site and running a test on a browser.

  • Important - The tests run only on real devices. The reason is that these tests need Chrome version >= 55 which are not available on Android Emulators as of May 2017.

Using webdriverio wrapper:

var webdriverio = require('webdriverio');
var options = {
    port: 4723,
    desiredCapabilities: {
        'browserName': 'chrome',
        'appiumVersion': '1.5.3',
        'deviceName': '007886d5600d492f',
        'device-orientation': 'portrait',
        'platformVersion': '4.4',
        'platformName': 'android'
    }
};
webdriverio
    .remote(options)
    .init()
    .url('https://www.google.com')
    .setValue('#lst-ib', "Cheese")
    .click('[name="btnG"]')
    .end();

Testing on cloud platform e.g. TestObject.

Please note that you will need a access key from TestObject. You can get a trial access key for limited time

  export TESTOBJECT_KEY=<testobject key>
  export TESTOBJECT_DEVICE = <test object device>

var webdriver = require('selenium-webdriver'),
    testObjectId =  process.env.TESTOBJECT_KEY,
    deviceId = process.env.TESTOBJECT_DEVICE,
    builder, driver;

builder = new webdriver.Builder()
    .withCapabilities({
        'testobject_appium_version': '1.6.4',
        'testobject_api_key' :process.env.TESTOBJECT_KEY,
        'testobject_device':process.env.TESTOBJECT_DEVICE,
        'browserName': 'chrome'
    })
    .usingServer("https://app.testobject.com/api/appium/wd/hub");
driver = builder.build();

driver.get("https://www.walmart.com");

driver.getTitle().then(function (title) {
    console.log("title is: " + title);
});

driver.quit();

 

Desktop

Desktop version of same test needs Selenium standalone server. So download latest selenium standalone server from here

var webdriverio = require('webdriverio');
var options = {
    desiredCapabilities: {
        'browserName': 'chrome'
    }
};
webdriverio
    .remote(options)
    .init()
    .url('https://www.google.com')
    .setValue('#lst-ib', "Cheese")
    .click('[name="btnG"]')
    .pause(2000)
    .end();

 

Testing on saucelabs - Desktop.

export SAUCE_USERNAME=<sauce username>
export SAUCE_ACCESS_KEY=<sauce access key>
var webdriver = require('selenium-webdriver'),
    builder, driver;


builder = new webdriver.Builder().
withCapabilities({
    'browserName': 'chrome',
    'username': process.env.SAUCE_USERNAME,
    'accessKey': process.env.SAUCE_ACCESS_KEY


}).
usingServer("http://" + process.env.SAUCE_USERNAME + ":" + process.env.SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub");

driver = builder.build();

driver.get("https://www.google.com");


driver.getTitle().then(function (title) {
    console.log("title is: " + title);
});

driver.quit();

 

Check out the repo for some webdriverio tests