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 expressionsWriting to cucumber report in cucumber
We can write to Cucumber HTML report using write method of Scenario class. Below step definition class illustrates how to write to HTML report. Note how we have passed scenario object to Before method.
package org.softpost;
import com.google.common.primitives.Bytes;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import junit.framework.Assert;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
* Created by Sagar on 12-07-2016.
*/
@SuppressWarnings("ALL")
public class seleniumsteps {
WebDriver driver;
Scenario scenario;
@Before("@selenium")
public void launchBrowser(Scenario scenario){
driver = new FirefoxDriver();
this.scenario = scenario;
}
@Given("^I am on the www\.softpost\.org home page$")
public void i_am_on_the_www_softpost_org_home_page() throws Throwable {
driver.get("https://www.softpost.org");
scenario.write("Navigated to www.softpost.org");
}
@Then("^I verify that title contains tutorials$")
public void i_verify_that_title_contains_tutorials() throws Throwable {
Assert.assertTrue(driver.getTitle().toLowerCase().contains("tutorials"));
}
@Then("^I verify that title contains tutooorials$")
public void i_verify_that_title_contains_tutooorials() throws Throwable {
Assert.assertTrue(driver.getTitle().toLowerCase().contains("tutooorials"));
}
@After("@selenium")
public void killBrowser(){
scenario.write("Finished scenario");
if (scenario.isFailed()) {
scenario.embed(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES), "image/png");
}
driver.close();
driver.quit();
}
}
Here is the sample HTML report.
Web development and Automation testing
solutions delivered!!