在使用Selenium-webdriver运行测试时,为什么为什么总是得到“ UnhandledPromiseRejectionWarning”?

我正在尝试在我的应用程序i React上在前端和后端都没有mocha的情况下实现Selenium测试。但是我总是出错。摩卡咖啡运行,所以硒有问题吗?

在具有以下版本的MacOS上:

"mocha": "^6.2.2","selenium-webdriver": "^4.0.0-alpha.5"
"geckodriver": "^1.19.1","safari": "0.0.1"

这只是开始,这是一个非常简单的测试,用于返回应用程序的标题。

该应用已在http://localhost:3000/上启动并运行

我尝试了不同的浏览器; Firefox和野生动物园。 它们全部打开,但是它们(最终)都返回相同的错误。

在test / src node SimpleTest.js中运行测试

这是SimpleTest.js

var webdriver = require("selenium-webdriver");
const serverUri = "http://localhost:3000";

var browser = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.firefox())
    .build();

browser.get(serverUri);

browser.getTitle()
    .then(function (title) {
        console.log("The title",title);
    });

browser.quit();

我预期的输出

$ node SimpleTest.js 
The title Me-Me-Me

但是我知道了

$ node SimpleTest.js 
The title 
(node:1582) UnhandledPromiseRejectionWarning: NoSuchSessionError: Tried to run command without establishing a connection
    at Object.throwDecodedError (/$HOME/jsramverk/node_modules/selenium-webdriver/lib/error.js:550:15)
    at parseHttpResponse (/$HOME/jsramverk/node_modules/selenium-webdriver/lib/http.js:563:13)
    at Executor.execute (//$HOME/jsramverk/node_modules/selenium-webdriver/lib/http.js:489:26)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:1582) 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:1582) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

已解决 我有太多运行异步代码的异步代码。

const { Builder } = require('selenium-webdriver');
const Firefox = require('selenium-webdriver/firefox');
require('geckodriver');
const serverUri = "http://localhost:3000/#";
let browser;

async function main() {
    const builder = new Builder().forBrowser('firefox');
    builder.setfirefoxOptions(new Firefox.Options().headless());

    browser = await builder.build();
    await browser.get(serverUri);

    try {
        const title = await browser.getTitle();
        console.log(title);
    } catch (ex) {
        console.log("Something went wrong",ex.message);
    } finally {
        await browser.quit();
    }

}

main().catch(console.error);
vighi 回答:在使用Selenium-webdriver运行测试时,为什么为什么总是得到“ UnhandledPromiseRejectionWarning”?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3166769.html

大家都在问