UnhandledPromiseRejectionWarning:NoSuchSessionError:无效的会话ID Selenium Webdriver

时间:2018-10-31 07:31:52

标签: node.js selenium-webdriver selenium-chromedriver integration-testing

我正在使用Selenium进行集成测试。 我有"selenium-webdriver": "^4.0.0-alpha.1" and "chromedriver": "^2.42.0"。测试通过了,但仍然获得UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id。我发现在After挂钩中调用this.driver.quit()时会发生这种情况。我在开始和运行 After 钩子时已经检查了驱动程序的sessionId是否相同。这是代码:

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)

2 个答案:

答案 0 :(得分:0)

对我来说,添加时已解决:

"--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()

答案 1 :(得分:0)

使用超时来延迟该过程,它将起作用!

setTimeout(() => {
        driver.quit();
    }, 100);