selenium - 使用 Nodejs 和 Mocha 运行 Selenium 时出现“NoSuchSessionError”
问题描述
我正在用 JavaScript 中的 Selenium 和 Mocha 编写一个测试用例。节点版本为 v10.10.0。摩卡版本为 v5.2.0。
这是我的测试代码:
require('chromedriver');
const assert = require('assert');
const {Builder, By, until} = require('selenium-webdriver');
var capabilities = {
'browserName' : 'chrome',
'chromeOptions' : {
'args' : ["--disable-plugins"]
}
}
describe('Checkout Formy', () => {
let driver;
before(async () => {
driver = await new Builder().withCapabilities(capabilities).build();
});
it('Large page scroll', async () => {
try {
await driver.get('http://formy-project.herokuapp.com/scroll');
// Find form input at the bottom of the page
let name = await driver.findElement(By.id('name'));
// Scroll to the name input field
driver.executeScript('arguments[0].scrollIntoView()', name);
driver.sleep(300);
name.sendKeys('Dale Nguyen');
// Find date field
let date = await driver.findElement(By.id('date'));
date.sendKeys('01/01/2018');
// // assertion codes...
// done();
} catch (error) {
console.log(error);
}
});
after(() => driver.quit());
})
我第一次运行良好,然后第二次或第三次运行此测试,它显示警告。
(node:45609) UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id
(Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)
at Object.checkLegacyResponse (/Users/dnguyen/Documents/projects/unit-test/selenium-javascript/node_modules/selenium-webdriver/lib/error.js:585:15)
at parseHttpResponse (/Users/dnguyen/Documents/projects/unit-test/selenium-javascript/node_modules/selenium-webdriver/lib/http.js:533:13)
at Executor.execute (/Users/dnguyen/Documents/projects/unit-test/selenium-javascript/node_modules/selenium-webdriver/lib/http.js:468:26)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:45609) 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: 2)
(node:45609) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminatethe Node.js process with a non-zero exit code.
有什么想法吗?谢谢,
解决方案
试试这样设置
var selenium = require('selenium-webdriver');
var capabilities = selenium.Capabilities.chrome();
capabilities.set('chromeOptions', {
'args': ['--disable-plugins']
});
var driver = new selenium.Builder().forBrowser('chrome').withCapabilities(capabilities).build();
推荐阅读
- javascript - 使用 javascript 加载多个图像
- yocto - Yocto 查找定义任务的配方或类
- reactjs - toggleInlineStyle() 是否有可用内联样式的列表或类型?
- php - 是否可以在 .asp 文件的 VBScript 块内运行 php 代码?
- javascript - 如何使用 Jest 模拟非默认导入
- node.js - 当 Firebase Realtime DB 从 Dialogflow 获取新数据时收到电子邮件
- javascript - 为什么我的 forloop + if/else 控制台会记录未定义的值
- r - 捕获从 fread 加载的数据到 R 中闪亮的 withProgress 栏
- javascript - 我想将文本框中的用户输入存储到变量中,改变显示给用户的内容
- excel - Excel:计算一组多个条件的唯一日期