1

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

Community
  • 1
  • 1
Make
  • 17
  • 1
  • 6

3 Answers3

3

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')
Kevin Lamping
  • 2,079
  • 15
  • 19
  • Hey @Kevin , i had tried but unable to click on that element `const elem = await client.$('#digit_2')` `await elem.click();` and found below error `invalid selector: Locator Strategy 'css selector' is not supported for this session` – Make May 10 '19 at 05:56
  • 1
    Ah, that's because CSS selectors don't work inside of mobile apps (which is what's leading to that error). Here are the selector strategies available: https://appium.io/docs/en/commands/element/find-elements/#selector-strategies – Kevin Lamping May 10 '19 at 19:40
1

$ is usually used as a shorthand to run JQuery functions (such as your $('#digit_2'), in the "android_app_test.js" file).

From the WebdriverIO's doc:

The $ command is a short way to call the findElement command in order to fetch a single element on the page. It returns an object that with an extended prototype to call action commands without passing in a selector. However if you still pass in a selector it will look for that element first and call the action on that element.

To fix this you have to install JQuery with this commands:

In a terminal run:

npm install --save jquery
npm install --save-dev @types/jquery

then import it at the top of your "android_app_test.js" file like this

import * as $ from "jquery";

Owlzy
  • 1,375
  • 2
  • 5
  • 10
-1

Make sure you're using the newest version of Webdriver.io. Webdriver.io v5 is the latest version that also implements the $('selector') shortcut.

If you're using Webdriver.io v4 - you may still need to use browser.element('selector') to find your elements.

It appears from the tags in your question, and the code you posted you maybe on version 4.