SessionNotFoundException:会话ID为null.调用quit()后使用WebDriver吗? (硒) [英] SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? (Selenium)

查看:1094
本文介绍了SessionNotFoundException:会话ID为null.调用quit()后使用WebDriver吗? (硒)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Cucumber/Java编写一些硒自动UI测试.如果我的功能文件中只有一个测试,则一切正常.但是,如果我添加第二个测试,则会在driver.get()上收到此错误:

I am trying to write some selenium automated UI tests using Cucumber/Java. If I have only one test in my feature file, everything works fine. But if I add a second test, I get this error on driver.get():

org.openqa.selenium.remote.SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '2.51.0', revision: '1af067dbcaedd7d2ab9af5151fc471d363d97193', time: '2016-02-05 11:20:57'

基本上,我将在一个包中的InitializeWebdriver类上初始化webdriver变量,然后在其他(步骤定义)类中对其进行引用.我确实将下面的步骤定义作为InitializeWebdriver类的一部分列出,并且工作正常(直到移至另一个类中的另一个步骤.所以我将该步骤移至CommonSteps.java文件中以查看它是否正确).会失败,但是确实如此.所以现在我被困住了.我正在考虑在@Before中执行if (driver.equals(null))并执行其他操作(如果已初始化),但是我不知道其他什么行动将会是.

Basically, I am initializing the webdriver variable on the InitializeWebdriver class in one package, and then referencing it in the other (step definition) classes. I did have the step definition listed below as a part of the InitializeWebdriver class, and it was working just fine (until moved on to a different step in a different class. So I moved that step to a CommonSteps.java file to see if it would then fail, and it did. So now I'm just stuck. I was thinking of doing an if (driver.equals(null)) in the @Before and doing a different action if had already been initialized, but I don't know what that other action would be.

这是我的代码:

tests.feature

tests.feature

Feature:  Two tests

Background:
    Given I navigate to "http://www.google.com"

Scenario: Test one
    When something happens

Scenario: Test two
    When something else happens

InitializeWebDriver.java

InitializeWebDriver.java

public class InitializeWebDriver {

    public static WebDriver driver = null;

    @Before
    public void beforeScenario() {
        driver = new ChromeDriver();
    }

    @After
    public void afterScenario() {
        driver.quit();
    }
}

CommonSteps.java

CommonSteps.java

import myPackage.InitializeWebDriver;

public class CommonSteps {

    static WebDriver driver = InitializeWebDriver.driver;

    @Given("^I navigate to \"([^\"]*)\"$")
    public void i_navigate_to(String url) {
        driver.get(value);
    }

谢谢!

推荐答案

我不认为drivernull,那样会导致NullPointerException,并且它没有办法将其转换为SessionNotFoundException.因此,看起来driver已创建然后结束,即.quit()的调用时间过早,如错误消息中所建议.

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. 它将启动第一个测试并调用@Before.这导致将InitializeWebDriver.driver设置为新的WebDriver.
  2. 仅在此之后才加载类CommonSteps,因此将CommonSteps.driver设置为刚刚创建的WebDriver.
  3. 测试成功运行,并且在WebDriver中的@After方法中调用了.quit().
  4. 然后开始第二个测试.在@Before方法中创建一个新的WebDriver. InitializeWebDriver.driver已更新;但是,CommonSteps.driver不会更新,因为driver = InitializeWebDriver.driver;仅在第一次加载CommonSteps时发生.
  5. 因此,当到达driver.get(value)时,driver是原始的WebDriver,已经是.quit().
  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.

基本上,问题在于您为WebDriver使用的是静态属性,不应在不同的测试运行之间共享这些属性.自从我完成这些工作已经有一段时间了,而且我不记得您是如何存储范围为测试运行的变量的. (在任何情况下,我都无法确定地回答,因为您还没有说明您使用的是哪个测试框架:JUnit,还是其他?)因此,您必须自己修复它,或者询问如何在您使用的任何框架中获取测试范围的变量.

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.

那是您要正确执行的操作.如果您只想进行廉价的修复,并且不打算并行运行测试,我怀疑您可以通过将driver.get(value);更改为InitializeWebDriver.driver.get(value);来进行修复.实际上,我建议您还是尝试更改此设置,以确保我对此是正确的.

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.

这篇关于SessionNotFoundException:会话ID为null.调用quit()后使用WebDriver吗? (硒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆