我正在使用Selenium运行集成测试。我有"selenium-webdriver": "^4.0.0-alpha.1" and "chromedriver": "^2.42.0"
。测试正在通过,但仍获得UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id
。我在After hook中调用this.driver.quit()
时发现了这种情况。我已经检查了驱动程序的sessionId在开始和运行After钩子时是相同的。这是代码:
hooks.js
function openWindow(driver) {
var verifyItsYouBtnXpath = "*//span[text()='Continue']/ancestor::div[@role='button']";
switchTab(driver).then(function() {
driver.wait(until.elementLocated(By.xpath(verifyItsYouBtnXpath))).then((verifyElement) => {
verifyElement.click();
navigateToEmail(driver).then(() => {
driver.wait(until.elementLocated(By.xpath("//td[*//span[text()='Integration Test Mail Thread']]")))
.then((element) => {
driver.wait(until.elementIsVisible(element)).click()
.then(() => {
driver.wait(until.elementLocated(By.xpath("//div[@aria-label='Thumbs Up!!']")))
.then(element => {
driver.wait(until.elementIsVisible(element)).click();
});
});
});
});
});
});
}
spec.js
'use strict';
var {Then, When} = require('cucumber');
var {By, until} = require('selenium-webdriver');
var assert = require('assert');
When(/^Reaction Add-on is open$/, function (next) {
next();
});
Then(/^User should get author name and first few words from message body$/, function (next) {
var parentDivXpath = "//div[@jsaction='click:h5M12e; clickmod:h5M12e;']/preceding-sibling::div";
var spanXpath = parentDivXpath + "/div/span[1]";
this.driver.wait(
until.elementsLocated(By.xpath(spanXpath))
).then(elements => {
this.driver.wait(until.elementIsVisible(elements[0]))
.then(element => {
element.getText().then(text => {
assert.equal(text, "Hello, Please like...");
});
});
this.driver.wait(until.elementIsVisible(elements[1]))
.then(element => {
element.getText().then(text => {
assert.equal(text, "Done. I liked it....");
assert.equal(elements.length, 2);
});
});
});
this.driver.wait(
until.elementsLocated(By.xpath(spanXpath + "/following-sibling::span"))
).then(elements => {
this.driver.wait(until.elementIsVisible(elements[0]))
.then(element => {
element.getText().then(text => {
assert.equal(text.includes("J King"), true);
});
});
this.driver.wait(until.elementIsVisible(elements[1]))
.then(element => {
element.getText().then(text => {
assert.equal(text.includes("PM Meg"), true);
assert.equal(elements.length, 2);
next();
});
});
});
});
这就是我的错误。但测试正在通过。
1 scenario (1 passed)
2 steps (2 passed)
0m56.079s
(node:3178) UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id
(Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.13.6 x86_64)
at Object.checkLegacyResponse (/Users/jaiprak/workspace/reaction-app/spec/integration_tests/node_modules/selenium-webdriver/lib/error.js:585:15)
at parseHttpResponse (/Users/jaiprak/workspace/reaction-app/spec/integration_tests/node_modules/selenium-webdriver/lib/http.js:533:13)
at Executor.execute (/Users/jaiprak/workspace/reaction-app/spec/integration_tests/node_modules/selenium-webdriver/lib/http.js:468:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:118:7)
(node:3178) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
使用超时来延迟进程,它就会工作!
setTimeout(() => {
driver.quit();
}, 100);
对我来说,当我添加时,它就解决了
"--remote-debugging-port=" + nextPort
我使用varibale nextPort来使用许多驱动程序完整代码:
let options = new chrome.Options()
nextPort++
console.log('nextPort = ' + nextPort)
options.addArguments(["--no-sandbox",
"--disable-gpu",
"--headless",
"--remote-debugging-port=" + nextPort])
let driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.setChromeOptions(options)
.build()
以下操作对我有效:在退出后,只需从驱动程序中删除引用,因为GC可能还没有删除它,您最终将重用同一驱动程序,但使用无效会话:
function quit(){
driver.quit();
driver = null;
}