Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Adding Cucumber to JUnit project

mkrzyzanowski edited this page Oct 25, 2016 · 11 revisions

If you want to add Cucumber to JUnit project, you need to do few things:

Add this dependency to pom.xml

<dependency>
        <groupId>com.cognifide.qa.bb</groupId>
        <artifactId>bb-cumber</artifactId>
        <version>${bb.version}</version>
</dependency>

Create Cucumber Injector

  • create CucumberInjectorSource.java
import com.google.inject.Guice;
import com.google.inject.Injector;
import cucumber.api.guice.CucumberModules;
import cucumber.runtime.java.guice.InjectorSource;

public class CucumberInjectorSource implements InjectorSource {

	@Override
	public Injector getInjector() {
		return Guice.createInjector(CucumberModules.SCENARIO, new GuiceModule());
	}

}
  • create cucumber-guice.properties under src/test/resources with fully qualified name of your CucumberInjectorSource
guice.injector-source=<your-package>.CucumberInjectorSource

Create Hooks

  • create package 'hooks' under src/main/java/com.cognifide

  • create TakeScreenshot.java in hooks package

import com.google.inject.Inject;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

/**
 * A helper class that can create a screenshot file.
 */
public class TakeScreenshot {
	/**
	 * Inject a WebDriver instance, because it will create the screenshot.
	 */
	@Inject
	private WebDriver webDriver;

	/**
	 * Creates the screenshot, embeds it in the scenario and closes webDriver.
	 */
	@After
	public void takeScreenshotAndClose(Scenario scenario) {
		if (scenario.isFailed() && webDriver instanceof TakesScreenshot) {
			byte[] screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES);
			scenario.embed(screenshot, "image/png");
		}
		webDriver.quit();
	}

}
  • create also WindowMaximize.java in hooks package
import com.google.inject.Inject;
import cucumber.api.java.Before;
import org.openqa.selenium.WebDriver;

/**
 * A helper class that will maximize window
 */
public class WindowMaximize {

	@Inject
	private WebDriver webDriver;

	@Before
	public void maximize() {
		webDriver.manage().window().maximize();
	}
}

That's all! Now you can develop your test cases in JUnit and in Cucumber! Go to Getting Started to look how to do it.

Getting started with Bobcat

  1. Getting started

AEM Related Features

  1. Authoring tutorial - Classic
  1. AEM Classic Authoring Advanced usage
  1. Authoring tutorial - Touch UI
Clone this wiki locally