Selenium&webdriver.io如何使用executeScript?

我试图用Selenium,W​​ebDriver.io和Node.js(用Mocha)来testing一个简单的表单。 所以我有这样的东西:

var webdriverio = require('webdriverio'); var expect = require('expect'); describe('Test form', function(){ beforeEach(function() { browser.url('/'); }); it('should save object', function() { expect(browser.executeScript('return window.data;')).to.be([]); }); afterEach(function() { if (this.currentTest.state !== "passed") { browser.saveScreenshot(); } }); }); 

我的wdio.conf.js

 var selenium = require('selenium-standalone'); var seleniumServer; exports.config = { host: '127.0.0.1', port: 4444, specs: [ 'test/*.spec.js' ], capabilities: [{ browserName: 'chrome' }], baseUrl: 'http://localhost:8080', framework: 'mocha', mochaOpts: { ui: 'bdd' }, onPrepare: function() { return new Promise((resolve, reject) => { selenium.start((err, process) => { if(err) { return reject(err); } seleniumServer = process; resolve(process); }) }); }, onComplete: function() { seleniumServer.kill(); } }; 

但是在控制台中我有: browser.executeScript is not a function 。 使用这些工具在浏览器上下文中执行脚本的正确方法是什么?

好的,我正在search源代码并find/build/lib/protocol/execute.js 。 从那里的例子:

 client.execute(function(a, b, c, d) { // browser context - you may not access neither client nor console return a + b + c + d; }, 1, 2, 3, 4).then(function(ret) { // node.js context - client and console are available console.log(ret.value); // outputs: 10 }); 

但是现在所有的wdio命令都是同步的( certificate问题 )。 所以对我来说正确的方法是:

 var data = browser.execute(function() { return window.data; }); expect(data.value).to.be([]); /* note, here ^ is a property with value of execution */