【问题标题】:How to execute a js file using webdriver.io?如何使用 webdriver.io 执行 js 文件?
【发布时间】:2019-09-28 21:42:10
【问题描述】:

当我使用 selenium-webdriver 时,我可以通过调用 executeScript() 来做到这一点。

const script = fs.readFileSync(__dirname+'/script.js', 'utf8');
await driver.executeScript(script);

有效

如何使用 webdriver.io 做到这一点?如果我使用 execute() 我会收到以下错误:

const script = fs.readFileSync(__dirname+'/script.js', 'utf8');
await browser.execute(script);
ERROR webdriver: Request failed due to Error: {"errorMessage":"Unexpected token ')'"

【问题讨论】:

    标签: webdriver-io


    【解决方案1】:

    如果你看the documentation,它表明你需要传入一个函数,而不是一个文件:

    it('should inject javascript on the page', () => {
        const result = browser.execute((a, b, c, d) => {
            // browser context - you may not access client or console
            return a + b + c + d
        }, 1, 2, 3, 4)
        // node.js context - client and console are available
        console.log(result) // outputs: 10
    });
    

    如果您想将函数作为字符串传递,请像以前一样使用executeScript

    【讨论】:

      猜你喜欢
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      相关资源
      最近更新 更多