While there are lots of blogs on how to run Appium with Mocha, there is not much info on how to do it with Jest as a test runner. In this blog post, we will set up Appium with Jest and WebdriverIO

We will slightly modify the official Appium Get Started tutorial. Please find the demo app link on the page. Place downloaded apk in the root directory of your project.

Here is how project structure will look like:

- ApiDemos-debug.apk
- jest.config.js
- package.json
- tests
  - __tests__
    - first.test.js

Now we need to generate package.json file, initiate node project inside root folder, by running npm init (complete the questions, leave the test script default). Then lets add our dependencies to the project: npm i appium jest webdriverio

{
  "name": "appium-jest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest --config=jest.config.js --detectOpenHandles --forceExit",
    
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "appium": "^1.17.1",
    "jest": "^26.0.1",
    "webdriverio": "^6.1.16"
  }
}

Notice how I set the test script to run Jest, now in the root directory, we need to add jest.config.js file

module.exports = {
    rootDir: './specs',
    testTimeout: 30000,
    bail: 0,
}

Now let’s add our first.test.js file. For this blog, we going to keep it simple and specify capabilities in the beforeAll method, which will be executed before all tests (later I will add a blog on how to abstract capabilities).

const wdio = require("webdriverio");

describe('Appium with Jest automation testing', () => {
let client
let field
beforeAll(async function(){
  opts = {
    path: '/wd/hub',
    port: 4723,
    capabilities: {
      platformName: "Android",
      platformVersion: "9",
      deviceName: "Android Emulator",
      app: "/path_to_the_file/ApiDemos-debug.apk",
      appPackage: "io.appium.android.apis",
      appActivity: ".view.TextFields",
      automationName: "UiAutomator2"
    }
  }

    client = await wdio.remote(opts);
    field = await client.$("android.widget.EditText");

})

afterAll(async function(){
    await client.deleteSession();
)}

  
test('First test', async function() {
    await field.setValue("Hello World!");
    const value = await field.getText();
    assert.equal(value,"Hello World!");
    })
  })
})

To run on an android emulator you would need to install android studio and launch avd specified in capabilities (you can always change it to the one you need). And there we go, all set we can run the test by executing: npm run test

Hope you enjoyed this quick post on how to setup Appium with Jest and WebdriverIO, feel free to post your questions in the comments section.