SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? (Selenium)

天涯浪子 提交于 2019-11-30 05:52:12

I don't think driver is null, that would cause a NullPointerException and it would have no way of knowing to convert it to a SessionNotFoundException. So it looks like driver has been created and then ended, i.e. .quit() has been called too soon, as suggested in the error message.

Here's what I think is happening:

  1. It starts the first test and calls the @Before. This causes InitializeWebDriver.driver to be set as the new WebDriver.
  2. Only after that does it load the class CommonSteps, so CommonSteps.driver is set to the WebDriver that was just created.
  3. The test runs successfully, and .quit() is called on the WebDriver, in the @After method.
  4. Then it starts the second test. A new WebDriver is created in the @Before method. InitializeWebDriver.driver is updated; however, CommonSteps.driver is not updated, because the driver = InitializeWebDriver.driver; only happens when CommonSteps is first loaded.
  5. Therefore, when it gets to driver.get(value), driver is the original WebDriver, which has already been .quit().

This is assuming you're running the tests in series. If they're in parallel then it will be a bit different.

Basically the problem is that you're using static attributes for WebDriver, which shouldn't be shared between different test runs. It's a while since I've done this stuff, and I don't remember how you store variables scoped to a test run. (In any case I wouldn't be able to answer with certainty, since you haven't said which test framework you're using: JUnit, or something else?) So you'll have to fix it yourself, or ask how to get test-scoped variables in whatever framework you're using.

That's if you want to do it properly. If you just want a cheap fix, and if you're not planning to run tests in parallel, I suspect that you can fix it by changing driver.get(value); to InitializeWebDriver.driver.get(value);. In fact, I suggest you try changing that anyway, just to make sure I'm right about this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!