Cucumber + Java
Introduction to Cucumber Installation of CucumberAdding Cucumber dependency to Java project Writing first cucumber test Executing Cucumber tests using Cucumber class Cucumber options Tagging the scenarios Using name option Passing parameters to steps Key – Value pair Datatable Multiple column datatable in Cucumber Scenario Background Scenario outline Running multiple feature files in Cucumber Sharing selenium Webdriver instance using PicoContainer Embedding the screenshot Writing to Cucumber HTML reports Cucumber dependency for using Lambda expressions in Java 8 Cucumber test using Lambda expressionsSharing selenium webdriver in cucumber
In Selenium automation, we create a WebDriver instance. But we can not use the same driver instance in different step definition classes in Cucumber. We can share the same Webdriver instance in 2 ways.- By creating a static Webdriver
- By using PicoContainer (Dependency Injection)
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.42.2</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
Next, we need to create a class where we will create a Webdriver instance as shown in below example. Note that we instantiates the driver only once. We have got a method getDriver to get the driver.
package org.softpost;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SharedClass {
private static boolean startBrowser = false;
private WebDriver driver;
public String title = "";
@Before("@sharedselenium")
public void init() throws Exception {
if (!startBrowser) {
driver = new FirefoxDriver();
//To stop launching browser after every scenario, assign below variable with true value
startBrowser = false;
}
}
public WebDriver getDriver() {
return driver;
}
@After("@sharedselenium")
public void cleanUp() {
driver.close();
driver.quit();
}
}
Next in the step definition classes, we can pass the instance of above class as shown in below example. Note that we are using getDriver() method to access the driver instance. So similarly you can pass the instance of SharedClass to any step definition class that wants to use the driver. PicoContainer is used internally to wire up dependent classes.
package org.softpost;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import junit.framework.Assert;
public class ShareSeleniumSteps {
SharedClass sharedClass;
public ShareSeleniumSteps(SharedClass sharedClass){
this.sharedClass = sharedClass;
}
@Given("^I am on the www\.softpost\.org home page$")
public void i_am_on_the_www_softpost_org_home_page() throws Throwable {
sharedClass.getDriver().get("https://www.softpost.org");
}
@Then("^I verify that title contains tutorials$")
public void i_verify_that_title_contains_tutorials() throws Throwable {
Assert.assertTrue( sharedClass.getDriver().getTitle().toLowerCase().contains("tutorials"));
}
@Then("^I verify that title contains tutooorials$")
public void i_verify_that_title_contains_tutooorials() throws Throwable {
Assert.assertTrue( sharedClass.getDriver().getTitle().toLowerCase().contains("tutooorials"));
}
}
Web development and Automation testing
solutions delivered!!