我正在使用以下代码(取自 Setting up your own test automation environment )
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var driver_fx = new webdriver.Builder()
.forBrowser('firefox')
.usingServer('http://localhost:4444/wd/hub')
.build();
var driver_chr = new webdriver.Builder()
.forBrowser('chrome')
.usingServer('http://localhost:4444/wd/hub')
.build();
searchTest(driver_fx);
searchTest(driver_chr);
function searchTest(driver) {
driver.get('http://www.google.com');
driver.findElement(By.name('q')).sendKeys('webdriver');
driver.sleep(1000).then(function() {
driver.findElement(By.name('q')).sendKeys(webdriver.Key.TAB);
});
driver.findElement(By.name('btnK')).click();
driver.sleep(2000).then(function() {
driver.getTitle().then(function(title) {
if(title === 'webdriver - Google Search') {
console.log('Test passed');
} else {
console.log('Test failed');
}
driver.quit();
});
});
}
它应该打开firefox和chrome,转到谷歌,在输入中输入一些文本,单击搜索按钮,然后检查窗口标题。
我使用以下命令运行selenium服务器:
java -Dwebdriver.gecko.driver=/home/sean/bin/geckodriver -Dwebdriver.chrome.driver=/home/sean/bin/chromedriver -jar ~/Downloads/selenium-server-standalone-3.14.0.jar
然后node index.js
进行测试。这会打开firefox和chrome并出现此错误:
WebDriverError: Element <input name="btnK" type="submit"> is not clickable at point (564,411) because another element <div class="sbqs_c"> obscures it
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:13:22.693Z'
System info: host: 'mx', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.13.0-46-generic', java.version: '1.8.0_171'
Driver info: driver.version: unknown
at Object.checkLegacyResponse (/home/sean/workspace/selenium-test/node_modules/selenium-webdriver/lib/error.js:546:15)
at parseHttpResponse (/home/sean/workspace/selenium-test/node_modules/selenium-webdriver/lib/http.js:509:13)
at doSend.then.response (/home/sean/workspace/selenium-test/node_modules/selenium-webdriver/lib/http.js:441:30)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:118:7)
From: Task: WebElement.click()
at thenableWebDriverProxy.schedule (/home/sean/workspace/selenium-test/node_modules/selenium-webdriver/lib/webdriver.js:807:17)
at WebElementPromise.schedule_ (/home/sean/workspace/selenium-test/node_modules/selenium-webdriver/lib/webdriver.js:2010:25)
at WebElementPromise.click (/home/sean/workspace/selenium-test/node_modules/selenium-webdriver/lib/webdriver.js:2092:17)
at searchTest (/home/sean/workspace/selenium-test/index.js:58:39)
at Object.<anonymous> (/home/sean/workspace/selenium-test/index.js:47:1)
at Module._compile (module.js:649:30)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
如果我一次在1个浏览器中运行测试,它们都会通过。
我尝试不使用selenium服务器并让selenium-webdriver模块处理启动进程。还尝试启动具有多个节点的集线器(在同一台机器中)并在不同节点中运行每个浏览器。都有同样的错误。
我是否理解,如果我的测试有click()之类的操作,那么在同一物理/虚拟机中一次只能打开一个浏览器实例(相同的webdriver或不同的webdriver),以避免浏览器窗口相互重叠?
什么是最佳做法?每个selenium节点有1个docker,每个节点设置maxSession 1?只打开一个浏览器并在那里运行所有测试?
是否存在没有此约束的测试自动化工具?
编辑:使用第三方服务,如browserstack和saucelabs不是一个选项
我发现我可以先运行一个集线器:
java -Dwebdriver.gecko.driver=/home/sean/bin/geckodriver -Dwebdriver.chrome.driver=/home/sean/bin/chromedriver -jar ~/Downloads/selenium-server-standalone-3.14.0.jar -role hub
然后使用-maxSession 1运行多个selenium节点实例,并使用带有-a参数的xvfb(尝试获取一个空闲的X服务器号):
xvfb-run -a --server-args="-screen 1, 1366x768x24" java -Dwebdriver.gecko.driver=/home/sean/bin/geckodriver -Dwebdriver.chrome.driver=/home/sean/bin/chromedriver -jar ~/Downloads/selenium-server-standalone-3.14.0.jar -role node -maxSession 1
这样我仍然无法在同一个会话中打开2个浏览器(如上面的代码所示)但我可以在同一台机器上打开多个会话,每个会话都运行自己的浏览器
我仍然希望了解其他解决方案和最佳实践,因为我怀疑这是最好的解决方案