'Why is my IE browser getting launched twice with Selenium and Cucumber?
I am writing very basic automation test with Selenium-Cucumber that is launching an IE browser and closing it at the end.
The problem is that the browser gets launched twice.
The test does not have much it other than few System.out statements. I am kind of new to both selenium-based automation testing and Cucumber and not able to understand why is it getting launched twice.
Please guide.
BrowserConfig.java
public class BrowserConfig {
private static final String IE_DRIVER_EXE = "drivers/IEDriverServer.exe";
private static final String WEBDRIVER_IE_DRIVER = "webdriver.ie.driver";
private static final String BASE_URL = "https://www.google.com";
public static WebDriver getIEWebDriver() {
String filePath = ClassLoader.getSystemClassLoader().getResource(IE_DRIVER_EXE).getFile();
System.setProperty(WEBDRIVER_IE_DRIVER, filePath);
InternetExplorerOptions options = new InternetExplorerOptions().requireWindowFocus();
options.setCapability(INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
options.setCapability(ENABLE_ELEMENT_CACHE_CLEANUP, true);
options.setCapability(IE_ENSURE_CLEAN_SESSION, true);
options.setCapability(ACCEPT_SSL_CERTS, true);
options.setCapability("nativeEvents", false);
options.setCapability(INITIAL_BROWSER_URL, BASE_URL);
WebDriver driver = new InternetExplorerDriver(options);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
return driver;
}
public static void releaseResources(WebDriver driver) {
if (null != driver) {
driver.close();
driver.quit();
}
}
}
TestRunner.java
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "json:target/cucumber-reports/cucumber.json"},
features = {"src/test/resources/features"})
public class TestRunner extends ApplicationTests {
}
LoginStep.java
@Ignore
public class LoginStep {
WebDriver driver;
@Before
public void setup() {
if (this.driver == null) {
this.driver = BrowserConfig.getIEWebDriver();
}
}
@After
public void cleanUp() {
BrowserConfig.releaseResources(driver);
}
@Given("^The user is on the Login page$")
public void doLogin() {
System.out.println("The user is on the Login page");
}
@When("^The user enters the correct credentials on the Login page$")
public void setWelcomePage() {
System.out.println("The user enter the correct credentials on the Login page");
}
@Then("^The user is displayed Welcome page$")
public void validate() {
System.out.println("The user is displayed Welcome page");
}
}
HelpStep.java
@Ignore
public class HelpStep {
WebDriver driver;
@Before
public void setup() {
if (this.driver == null) {
this.driver = BrowserConfig.getIEWebDriver();
}
}
@After
public void cleanUp() {
BrowserConfig.releaseResources(driver);
}
@When("^The user clicks on the Help menu link from the Welcome page$")
public void setWelcomePage() {
System.out.println("The user clicks on the Help menu link from the Welcome page");
}
@Then("^The user is displayed Help page$")
public void validate() {
System.out.println("The user is displayed Help page");
}
}
help.feature
Feature: Check that the user is able to navigate to Help page
Background:
Given The user is on the Login page
When The user enters the correct credentials on the Login page
Then The user is displayed Welcome page
Scenario:
When The user clicks on the Help menu link from the Welcome page
Then The user is displayed Help page
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>cucumber-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cucumber-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<cucumber.version>4.2.3</cucumber.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>3.14.0</version>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>${project.artifactId}</projectName>
<outputDirectory>${project.build.directory}/cucumber-reports</outputDirectory>
<cucumberOutput>${project.build.directory}/cucumber-reports/cucumber.json</cucumberOutput>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Solution 1:[1]
Because you are initializing and call your driver twice in feature files.
Background part of your feature file are initializing browser firstly in LoginStep.java then your Scenario is also initialize browser in HelpStep.java.
I prefer using global Hooks.java class for @Before and @After hooks and inject driver between different .java classes.
Solution 2:[2]
Found the solution. The below sample code is not the same as original post but this fixes the issue. Added new class "Hooks.java" that contains common steps and removed "Background" from feature files. That helped fix the issue.
Hooks.java
public class Hooks {
public static WebDriver driver;
@Before
public void setUp() {
System.out.println("Into the setup method of AccountStep...");
driver = BrowserConfig.getDriver();
}
@After
public void cleanUp() {
System.out.println("Into the cleanUp method of AccountStep...");
if (null != driver) {
driver.close();
driver.quit();
}
}
}
help.feature
Feature: Check that the user is able to navigate to Help page
Scenario:
Given The user is on the Help page
When The user clicks on the links within the Help page
Then The user is navigated to that Help section
HelpStep.java
@Ignore
public class HelpStep {
private WebDriver driver;
public HelpStep() {
this.driver = Hooks.driver;
}
@Given("^The user is on the Help page$")
public void onPage() {
System.out.println("The user is on the Help page");
}
@When("^The user clicks on the links within the Help page$")
public void clickLinks() {
System.out.println("The user clicks on the links within the Help page");
}
@Then("^The user is navigated to that Help section$")
public void validate() {
System.out.println("The user is navigated to that Help section");
}
}
Solution 3:[3]
Make sure you specific the before and after hooks with tag for every Feature, Why ? ok for example you have two features called UsersFeature and ProductsFeature and you make some hooks like initBrowser as before and closeBrowser as after for UsersFeature and ProductsFeature what happens when you run that test without any tags ? let me tell you that hooks it will running for tow times or (N times for parallel) for every Feature for that test and N = "number of the features in that test" so all the before hooks in that test will runing first even if there is million Features and also the all the after hooks so make sure use tags like @Before('@users_feature') @After('@users_feature') in that time that hooks will runing only when the test executing the feature scenarios with @users_feature tag
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | slckayhn |
| Solution 2 | Nital |
| Solution 3 | procrastinator |
