SessionNotFoundException:会话ID为null.调用quit()后使用WebDriver吗? (硒) [英] SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? (Selenium)
问题描述
我正在尝试使用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);
}
谢谢!
推荐答案
我不认为driver
是null
,那样会导致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:
- 它将启动第一个测试并调用
@Before
.这导致将InitializeWebDriver.driver
设置为新的WebDriver
. - 仅在此之后才加载类
CommonSteps
,因此将CommonSteps.driver
设置为刚刚创建的WebDriver
. - 测试成功运行,并且在
WebDriver
中的@After
方法中调用了.quit()
. - 然后开始第二个测试.在
@Before
方法中创建一个新的WebDriver
.InitializeWebDriver.driver
已更新;但是,CommonSteps.driver
不会更新,因为driver = InitializeWebDriver.driver;
仅在第一次加载CommonSteps
时发生. - 因此,当到达
driver.get(value)
时,driver
是原始的WebDriver
,已经是.quit()
.
- It starts the first test and calls the
@Before
. This causesInitializeWebDriver.driver
to be set as the newWebDriver
. - Only after that does it load the class
CommonSteps
, soCommonSteps.driver
is set to theWebDriver
that was just created. - The test runs successfully, and
.quit()
is called on theWebDriver
, in the@After
method. - 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 thedriver = InitializeWebDriver.driver;
only happens whenCommonSteps
is first loaded. - Therefore, when it gets to
driver.get(value)
,driver
is the originalWebDriver
, 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屋!