javascript – Selenium和webdriver.io如何使用executeScript?
作者:互联网
我正在尝试使用Selenium,WebDriver.io和amp;来测试一个简单的表单. Node.js(使用Mocha).所以我有这样的事情:
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不是一个函数.使用这些工具在浏览器上下文中执行脚本的正确方法是什么?
解决方法:
好的,我在源代码中搜索并找到/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中的所有命令都是同步的(proof issue).所以对我来说正确的方法是:
var data = browser.execute(function() {
return window.data;
});
expect(data.value).to.be([]);
/* note, here ^ is a property with value of execution */
标签:javascript,node-js,selenium-webdriver,selenium,webdriver-io 来源: https://codeday.me/bug/20190519/1135443.html