最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReferenceError: $ is not defined (WebdriverIO) - Stack Overflow

programmeradmin3浏览0评论

I'm launching native apps with the help of WebdriverIO and mocha, but unable to municate 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 = ".google.android.calculator"
androidOptions.capabilities.appActivity = ".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

I'm launching native apps with the help of WebdriverIO and mocha, but unable to municate 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 = ".google.android.calculator"
androidOptions.capabilities.appActivity = ".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

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked May 9, 2019 at 10:04 MakeMake 491 gold badge1 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

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')

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.

$ 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 $ mand is a short way to call the findElement mand in order to fetch a single element on the page. It returns an object that with an extended prototype to call action mands 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 mands:

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";

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>