有没有办法为Firefox和Chrome使用相同的测试代码?一种抽象,我可以为所有浏览器驱动程序执行一段代码,而不必为每个浏览器驱动程序重复测试代码块
例如代码
var searchBox = ffDriver.findElement(webdriver.By.name('q'));
为每个浏览器驱动程序重复。
var assert = require('assert'),
test = require('selenium-webdriver/testing'),
webdriver = require('selenium-webdriver'),
SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
var server = new SeleniumServer('selenium-server-standalone-2.39.0.jar', {
port: 4444
});
server.start();
var ffDriver = new webdriver.Builder()
.usingServer(server.address())
.withCapabilities(webdriver.Capabilities.firefox())
.build();
var chromeDriver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
test.describe('Google Search', function () {
test.it('should have a query box that you can type in ff', function () {
ffDriver.get('http://www.google.com');
var searchBox = ffDriver.findElement(webdriver.By.name('q'));
searchBox.sendKeys('webdriver');
searchBox.getAttribute('value').then(function (value) {
assert.equal(value, 'webdriver');
});
ffDriver.quit();
});
test.it('should have a query box that you can type in chrome', function () {
chromeDriver.get('http://www.google.com');
var searchBox = chromeDriver.findElement(webdriver.By.name('q'));
searchBox.sendKeys('webdriver');
searchBox.getAttribute('value').then(function (value) {
assert.equal(value, 'webdriver');
});
chromeDriver.quit();
});
});
答案 0 :(得分:0)
是
var chromeDriver = //Create your chromeDriver here
var ffDriver = //Create your ffDriver here
var drivers = [chromeDriver, ffDriver];//This creates an array of all of the drivers
for (var i = 0; i< drivers.length; i++){//Run the following block of code for each driver
var driver = drivers[i];//Assign the driver variable
driver.get('http://www.google.com');//Use the driver variable.
//The rest of the automation code
}
请注意,将来您可能需要在代码中使用更多结构,但上面的代码应该可以正常工作。