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

javascript - Webdriver.io - Unable to load spec files quite likely because they rely on `browser` object - Stack Overflow

programmeradmin3浏览0评论

I'm using Webdriver.io to run tests on a large number of pages. Because all the specs for the pages are in a JSON file, I have a special class that sets up the test. It looks like this:

module.exports = class PageTester {
    suiteName = '';
    browser = {};

    constructor (suiteName, browser) {
        this.suiteName = suiteName;
        this.browser = browser;
    }

    testModel(currentModel) {

        describe(this.suiteName + ' endpoint ' + currentModel.url, () => {
            this.browser.url(currentModel.url);
    
            /* it() statements for the test */

        });
    }        
}

Then in my specs folder I have a file that loads the JSON and plugs it into the PageTester class, like this:

const PageTester = require('../modules/PageTester');

const models = require('/path/to/some/file.json');
const pageTester = new PageTester('Some Name', browser);

for (const modelName in models) {
    pageTester.testModel(models[modelName]);
}

When I run this code, WebdriverIO gives me the following warning:

WARN @wdio/mocha-framework: Unable to load spec files quite likely because they rely on `browser` object that is not fully initialised.
`browser` object has only `capabilities` and some flags like `isMobile`.
Helper files that use other `browser` commands have to be moved to `before` hook.
Spec file(s): /suite/test/specs/test.js

All the tests seem to run fine, so I don't actually understand what this warning is complaining about and what negative consequences ignoring it may have. So I would like to a) understand why this is happening and b) how it would be possible to get rid of this warning given the way my code is set up.

I'm using Webdriver.io to run tests on a large number of pages. Because all the specs for the pages are in a JSON file, I have a special class that sets up the test. It looks like this:

module.exports = class PageTester {
    suiteName = '';
    browser = {};

    constructor (suiteName, browser) {
        this.suiteName = suiteName;
        this.browser = browser;
    }

    testModel(currentModel) {

        describe(this.suiteName + ' endpoint ' + currentModel.url, () => {
            this.browser.url(currentModel.url);
    
            /* it() statements for the test */

        });
    }        
}

Then in my specs folder I have a file that loads the JSON and plugs it into the PageTester class, like this:

const PageTester = require('../modules/PageTester');

const models = require('/path/to/some/file.json');
const pageTester = new PageTester('Some Name', browser);

for (const modelName in models) {
    pageTester.testModel(models[modelName]);
}

When I run this code, WebdriverIO gives me the following warning:

WARN @wdio/mocha-framework: Unable to load spec files quite likely because they rely on `browser` object that is not fully initialised.
`browser` object has only `capabilities` and some flags like `isMobile`.
Helper files that use other `browser` commands have to be moved to `before` hook.
Spec file(s): /suite/test/specs/test.js

All the tests seem to run fine, so I don't actually understand what this warning is complaining about and what negative consequences ignoring it may have. So I would like to a) understand why this is happening and b) how it would be possible to get rid of this warning given the way my code is set up.

Share Improve this question edited Aug 26, 2020 at 1:21 Bintz asked Aug 26, 2020 at 1:06 BintzBintz 8012 gold badges10 silver badges26 bronze badges 1
  • Well, I have discovered that if I move this.browser.url inside an it() block that makes the warning go away. It would still be nice to have an explanation for this. – Bintz Commented Aug 27, 2020 at 13:42
Add a comment  | 

5 Answers 5

Reset to default 6

In my case, I resolve it by fixing the path for the require files. I noticed that my path was wrong. But the error that wdio throws is not really helpful. :/

you can only interact with browser object inside it blocks because it is not fully accessible before the browser session is started.

See https://webdriver.io/blog/2019/11/01/spec-filtering.html for details.

You simply should ensure your spec file and respective page file are kept on a similar folder structure.

// import { it } from "mocha";
// import { $ } from "webdriverio/build/commands/browser";

Comment on these two unwanted imports. It will work fine

IN my case, it occurs because of this line added in my code -

const { format, addMonths, isSameMonth, isSameDay } = require('date-fns');

solved using install -

npm install date-fns

after install it runs properly

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论