【问题标题】:How to call a function inside webdriverio test configuration file如何在 webdriverio 测试配置文件中调用函数
【发布时间】:2019-08-02 20:12:03
【问题描述】:

url 显示了一个 WebdriverIO 测试运行器配置

https://webdriver.io/docs/configurationfile.html

它有很多钩子。考虑钩子onComplete我要写一个函数,可能是a function to create a file。在另一个文件中并在 onComplete 挂钩内调用该函数。你能帮我实现这个目标吗?

【问题讨论】:

    标签: node.js webdriver-io webdriver-io-v4


    【解决方案1】:

    是的,您几乎描述了流程。

    在一个文件中定义你的函数并导出它:

    module.exports = (() => {
      /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
        * > Def: Polls the DOM until the given 'desiredState' is found. 
        * @param    {string} desiredState ['loading', 'interactive', 'complete']
        * @returns  {Promise} WebdriverIO Promise
        * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      browser.addCommand('waitForReadyState', (desiredState) => {
        let foundState;
    
        browser.waitUntil(() => {
          foundState = browser.execute('return document.readyState;');
          console.log(`\n> Waiting for page to load ... | Current state: '${foundState}'`);
          return foundState === desiredState;
        }, browser.options.waitforTimeout, `Timeout before expected state! Found: '${foundState}' | Expected: '${desiredState}'`);
      });
    })();
    

    然后,将其导入所需的钩子(例如:对于 custom_commandbefore 钩子):

    before: function (capabilities, specs) {
      require('./test/custom_commands/waitForReadyState');
    }
    

    您可以轻松重现模型以实现需要在 onComplete 挂钩中运行的日志记录和文件操作功能。

    【讨论】:

    • 您能否详细解释一下`require('./test/custom_commands/waitForReadyState');`。考虑函数 waitForReadyState 是否写在 utils.js 文件中。那我怎么称呼它?
    【解决方案2】:

    可能晚了,但你可以这样做:

    /** 保存函数的文件应该在 es5 中,或者你必须在 WebdriverIO 启动之前添加 babel 以将其转换为 es6 **/

    test.js

    module.exports = function foo(){
       console.log('here');
    }
    

    在配置文件中 //export.config:

    const foo = require('path-to-test.js');
    

    在 onComplete() 钩子中使用 foo()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多