参考错误:$ 未定义(WebdriverIO)

Mak*_*ake 2 javascript mocha.js webdriver-io appium-android webdriver-io-v4

我在 WebdriverIO 和 mocha 的帮助下启动本机应用程序,但无法与设备通信,但能够启动应用程序但不能与元素交互。

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');
    });
});
Run Code Online (Sandbox Code Playgroud)

配置文件

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);
    });
});
Run Code Online (Sandbox Code Playgroud)

包.json

"scripts": {
    "test": "mocha config.js"
  },
Run Code Online (Sandbox Code Playgroud)

不确定,我认为我的配置有问题或其他

Kev*_*ing 5

$全球通过WebdriverIO测试运行添加。由于您通过独立模式使用 wdio,因此您无法访问这些全局变量。试试这个:

const elem = await client.$('#digit_2')
Run Code Online (Sandbox Code Playgroud)