Android Automation Script

Appium Automation Framework

Appium is an open source test automation framework for use with native, hybrid and mobile web apps. It drives iOS, Android, and Windows apps using the WebDriver protocol.

Download the Appium client software here: https://github.com/appium/appium-desktop/releases/tag/v1.10.0

Session Initialization

Now we can create our test file (name it whatever you like) and initialize the client object:

const wdio = require("webdriverio");

The next thing we need to do is to start an Appium session. We do this by defining a set of server options and Desired Capabilities, and calling wdio.remote() with them. Desired Capabilities are just a set of keys and values that get sent to the Appium server during session initialization, that tell Appium what kind of thing we want to automate. The minimum set of required capabilities for any Appium driver should include:

  • platformName: the name of the platform to automate
  • platformVersion: the version of the platform to automate
  • deviceName: the kind of device to automate
  • app: the path to the app you want to automate (but use the browserName capability instead in the case of automating a web browser)
  • automationName: the name of the driver you wish to use

So here is how we begin to construct a session in our test file:

const opts = {
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "7.1.2",
    deviceName: "Android",
    app: "/path/to/the/downloaded/ApiDemos.apk",
    automationName: "UiAutomator2"
  }
};

const client = wdio.remote(opts);

Running Test Commands

You can see that we’ve specified our Appium port and also constructed our Desired Capabilities to match our requirements (but don’t forget to replace the path with the actual download path for your system). We’ve registered this fact with webdriverio and now have a client object which will represent the connection to the Appium server. From here, we can go ahead and start the session, perform some test commands, and end the session. In our case, we will simply tap into a few menus and then back out the way we came before ending the session:

const elementId = await client.findElement("accessibility id","TextField1");
client.elementSendKeys(elementId.ELEMENT, "Hello World!"); 
const elementValue = await client.findElement("accessibility id","TextField1");
await client.getElementAttribute(elementValue.ELEMENT,"value").then((attr) => {
assert.equal(attr,"Hello World!");
});

What’s going on here is that after creating a session and launching our app, we’re instructing Appium to find an element in the app hierarchy and click on it. Specifically, webdriverio has a convention where the ~ prefix means to find an element by its “accessbility id”, which in the case of Android means an element’s “content description”. So we find and tap on these elements in order to navigate through the app’s menu system. Then we can use the back() method to trigger the Android “back” behavior and get back to where we started before ending the session.

Putting it all together, the file should look like:

const wdio = require("webdriverio");

const opts = {
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "7.1.2",
    deviceName: "Android",
    app: "/path/to/the/downloaded/ApiDemos.apk",
    automationName: "UiAutomator2"
  }
};

const client = wdio.remote(opts);

const elementId = await client.findElement("accessibility id","TextField1");    client.elementSendKeys(elementId.ELEMENT, "Hello World!");
const elementValue = await client.findElement("accessibility id","TextField1");
await client.getElementAttribute(elementValue.ELEMENT,"value").then((attr) => {
assert.equal(attr,"Hello World!");
});

You can try and run this test on your own. Simply save it and execute it using node. If everything is set up correctly, you’ll see Appium begin spitting out lots of logs and eventually the app will pop up on the screen and start behaving as if an invisible user were tapping on it!