title: UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id selenium webdriver
body: <p>I am running integration tests using Selenium.
  I have <code>"selenium-webdriver": "^4.0.0-alpha.1" and "chromedriver": "^2.42.0"</code>. Tests are passing but still getting <code>UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id</code>. I have figured out that it's happening while calling <code>this.driver.quit()</code> in After hook. I have checked driver's sessionId is same in the beginning and while running <em>After</em> hook. This is the code:</p>
  
  <p><strong>hooks.js</strong></p>
  
  <pre><code>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) =&gt; {
        verifyElement.click();
          navigateToEmail(driver).then(() =&gt; {
            driver.wait(until.elementLocated(By.xpath("//td[*//span[text()='Integration Test Mail Thread']]")))
              .then((element) =&gt; {
                driver.wait(until.elementIsVisible(element)).click()
                  .then(() =&gt; {
                    driver.wait(until.elementLocated(By.xpath("//div[@aria-label='Thumbs Up!!']")))
                      .then(element =&gt; {
                        driver.wait(until.elementIsVisible(element)).click();
                      });
                  });
              });
          });
      });
    });
  }
  </code></pre>
  
  <p><strong>spec.js</strong></p>
  
  <pre><code>'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 =&gt; {
      this.driver.wait(until.elementIsVisible(elements[0]))
        .then(element =&gt; {
          element.getText().then(text =&gt; {
            assert.equal(text, "Hello,  Please like...");
          });
        });
  
      this.driver.wait(until.elementIsVisible(elements[1]))
        .then(element =&gt; {
          element.getText().then(text =&gt; {
            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 =&gt; {
      this.driver.wait(until.elementIsVisible(elements[0]))
        .then(element =&gt; {
          element.getText().then(text =&gt; {
            assert.equal(text.includes("J King"), true);
          });
        });
  
      this.driver.wait(until.elementIsVisible(elements[1]))
        .then(element =&gt; {
          element.getText().then(text =&gt; {
            assert.equal(text.includes("PM Meg"), true);
            assert.equal(elements.length, 2);
            next();
          });
        });
    });
  });
  </code></pre>
  
  <p>And this is the error, I am getting. But tests are passing.</p>
  
  <pre><code>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 &lt;anonymous&gt;
      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)
  </code></pre>