Search code examples
javascriptmocha.jswebdriver-ioappium-androidwebdriver-io-v4

ReferenceError: $ is not defined (WebdriverIO)


I'm launching native apps with the help of WebdriverIO and mocha, but unable to communicate with the device, but able to launch the application but not interact with the element.

android_app_test.js

const webdriverio = require('webdriverio');
const androidOptions = require('../../../helpers/caps').androidOptions;
const assert = require('chai').assert;

androidOptions.capabilities.appPackage = "com.google.android.calculator"
androidOptions.capabilities.appActivity = "com.android.calculator2.Calculator"

describe('Create Chrome web session', function () {
    
    let client;

    before(async function () {
        client = await webdriverio.remote(androidOptions)
    });

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

    it('should create and destroy Android browser session', async function () {
        const elem = await $('#digit_2')
        elem.waitForDisplayed(3000);

        await client.touchClick('digit_2');
    });
});

config.js

var Mocha = require('mocha'), fs = require('fs');

var mocha = new Mocha({
    reporter: 'mochawesome-screenshots',
    reporterOptions: {
        reportDir: 'customReportDir',
        reportName: 'customReportName',
        reportTitle: 'customReportTitle',
        reportPageTitle: 'customReportPageTitle',
        takePassedScreenshot: true,
        clearOldScreenshots: true,
        shortScrFileNames: true,
        jsonReport: false,
        multiReport: false
    },
    timeout: 600000,
})

var file = ['./test/basic/app/']; //location of the test js

for (var i = 0; i < file.length; i++) {
    fs.readdirSync(file[i]).forEach(function (filename) {
        mocha.addFile(file[i] + filename);
    });
}

mocha.run(function (failures) {
    process.on('exit', function () {
        process.exit(failures);
    });
});

package.json

"scripts": {
    "test": "mocha config.js"
  },

Not sure about that, i think something was wrong in my configuration or else


Solution

  • The $ global is added through the WebdriverIO test runner. Since you're using wdio through standalone mode, you don't get access to those globals. Try this instead:

    const elem = await client.$('#digit_2')