Running Your First Test

Go back to tutorial

Running Your First Test (Using JavaScript)

In this section we will run a basic “Hello World” Android test. Further, we have chosen Android because it’s available on all platforms. Also we will be using the UiAutomator2 Driver, so as to ensure you’ve read through that doc and gotten your system set up appropriately. In particular, we will be using also be using JavaScript as the language so that we don’t have to deal with additional dependencies.

Indeed, you will eventually want to automate something other than Android using something other than JavaScript.

Prerequisites to Run the Test

STEP 1 – Setting up the Appium Client

In this case, we’ll use Webdriver.io as our Appium client. Thereafter we will create a directory, then run,

npm install webdriverio

STEP 2 – Session Initialization

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

// javascript

const wdio = require(“webdriverio”);

Next, we need to start an Appium session. We can 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: Name of the platform to automate
  • platformVersion: Version of the platform to automate
  • deviceName: Kind of device to automate
  • app: Path to the app you want to automate
  • automationName: Name of the driver you wish to use

STEP 3 – Running Test Commands

Once you have specified the Appium port and also constructed desired capabilities, to match requirements. Thereafter, you must register this fact with webdriverio and 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.

Example illustrating the process of running test commands

// javascript

const field = await client.$(“~TextField1”);

await field.setValue(“Hello World!”);

const value = await field.getValue();

assert.equal(value,”Hello World!”);

});

To conclude

Post creating a session and launching the app, we will instruct Appium to find an element in the app hierarchy. Specifically, webdriverio has a convention where the ~ prefix means to find an element by its “accessbility id”. In the case of Android, it means an element’s “content description”. Therefore 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.

Go back to tutorial

Get industry recognized certification – Contact us

Menu