ReferenceError:未定义$(WebdriverIO)

时间:2019-05-09 10:04:39

标签: javascript mocha 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');
    });
});

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"
  },

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

3 个答案:

答案 0 :(得分:0)

$通常用作运行JQuery函数的简写(例如,“ android_app_test.js”文件中的$('#digit_2'))。

摘自WebdriverIO的文档:

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.

要解决此问题,您必须使用以下命令安装JQuery:

在终端运行中:

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

然后将其像这样导入“ android_app_test.js”文件的顶部

import * as $ from "jquery";

答案 1 :(得分:0)

The $ global is added through the WebdriverIO test runner。由于您是通过独立模式使用wdio的,因此无法访问这些全局变量。尝试以下方法:

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

答案 2 :(得分:0)

确保您使用的是最新版本的Webdriver.io。 Webdriver.io v5是最新版本,还实现了$('selector')快捷方式。

如果您使用的是Webdriver.io v4,则可能仍需要使用browser.element('selector')来查找元素。

它会出现在您问题中的标签中,以及您发布在版本4上的代码。