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>
Advertisement
Answer
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.