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'
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.
Here is my code:
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
public class InitializeWebDriver {
public static WebDriver driver = null;
@Before
public void beforeScenario() {
driver = new ChromeDriver();
}
@After
public void afterScenario() {
driver.quit();
}
}
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);
}
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…