Optimising Appium Tests
Tags: appium, testing, android, ios
Published: 2022-05-28 07:31:49

Let's first begin by recapping what Appium is, what it does and why it is used by developers and QA for both mobile web testing and mobile app testing.

## What is Appium?

Appium is an open-source framework, built in Javascript, which provides a collection of tools allowing you to run automated tests on mobile simulators/emulators and physical mobile devices.

Automated tests are test scripts which perform various actions on a website, or mobile app, while checking if the result of those actions are the intended result.

Both website and mobile app developers, as well as QA departments, use automated testing as a way to find regression bugs or incompatibilities on various devices.

## How do I use Appium?

You can easily set up Appium if you have already installed NodeJS. To install, simply run this command:

```
npm i appium
```

Next, make sure you have either an iOS simulator or Android emulator installed on the same system. You can also connect a physical device to the machine, through USB, which should work just as well with Appium.

Once setup, you can write your first Appium test. In this example, let's use WebDriverIO:
```
npm install webdriverio
```

Now, copy/paste the example code below to run your first test:
```
const opts = {
  path: '/wd/hub',
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "12",
    deviceName: "Android Emulator",
    app: "https://github.com/appium/appium/raw/master/sample-code/apps/ApiDemos-debug.apk",
    appPackage: "io.appium.android.apis",
    appActivity: ".view.TextFields",
    automationName: "UiAutomator2"
  }
};

async function main () {
  const client = await wdio.remote(opts);
  const field = await client.$("android.widget.EditText");
  await field.setValue("Hello World!");
  const value = await field.getText();
  assert.strictEqual(value, "Hello World!");
  await client.deleteSession();
}

main();
```

This will start an automated test session on an Android 12 emulator, running on your machine. It will open a demo app, input 'Hello World' in an input field and check if the input was successful.


## Optimising Appium Tests

Appium provides various options, out of the box, that make testing as stable as possible on various devices.

There are some settings you can tweak, to [make Appium more performant](https://testingbot.com/resources/articles/getting-started-with-appium).

These settings depend on wether you want to run tests on Android or iOS. Let's see a list of settings we can modify to make Appium faster:

**Cross Platform Capabilities**

- noReset: By default Appium will reset as much as possible in between sessions. If you do not want this, setting this to `false` will speed up test execution.

- fullReset: Only use this if you really have to. Usually not required

- isHeadless: Setting this to `true` will start a simulator or emulator without an UI. This should be faster.


**Android Capabilities**

- autoGrantPermission: Setting this capability to `true` allows Appium to determine your app permissions and grant them at the start of the test.

- appWaitPackage / appWaitActivity: Indicate to Appium which activity is supposed to start. This prevents having to wait for Appium to inspect the `apk` and loop over activities.

- skipUnlock: Appium uses a helper tool to check if the device or emulator under test is locked, for example with a pin code. If this is not the case, skipping this will shave off a few seconds.

- skipDeviceInitialization: Setting this to `true` will cause Appium not to install the `io.appium.settings` helper app.

- skipServerInstallation: If the server app is already on the device, skipping this will shave off a second or so.



**iOS Capabilities**

- iosInstallPause: Let Appium know how long to wait before starting the test.

- maxTypingFrequency: This capability specifies the maximum speed when typing on an iOS device or simulator.

- realDeviceScreenshotter: This will use the `idevicescreenshot` program, which is part of `libimobiledevice`, to capture screenshots. It should be more performant.

- simpleIsVisibleCheck: This will use a simpler visible check than the default one used by WebDriverAgent.

## Appium Cloud Testing

Once you want to run Appium automated tests on multiple devices, you might want to consider using a cloud provider. The advantage of this is that these provide multiple physical devices, iOS and Android, which are already preconfigured and ready to run your tests.

You will get instant test results, including a video, screenshots and logs. [TestingBot](https://testingbot.com) is an Appium cloud provider which offers access to physical Android and iOS devices for native mobile app testing.