Unable to use @before & @after in cucumber | Selenium Forum
M
Posted on 22/01/2017
Sir,

i have a feature with 4 scenarios.I want to execute a method before each step of these scenarios.

Tried @Before,@after in stepdefination class with junit &cucumber.api.java.*======This is not working as discussed in below links:
https://www.tutorialspoint.com/cucumber/cucumber_hooks.htm
https://sukesh15.gitbooks.io/cucumber-jvm-test-framework-/content/cucumber_-_more_details/scenario_hooks.html
http://stackoverflow.com/questions/24029341/before-doesnt-execute-in-java-cucumber-step
https://github.com/cucumber/cucumber-jvm/issues/515

My code is as following in my [b:x2fyncfz]stepdefination[/b:x2fyncfz]:

@Before
public void before(Scenario scenario) {
this.scenario = scenario;
scenario.write("*****************************************************");
System.out.println("BEFOREEEEEEEEEEEEEEEEEEEE");

}

@After
public static void embedScreenshot(Scenario scenario) {
if (scenario.isFailed()) {
try {
byte[] screenshot = ((TakesScreenshot) selenium.w)
.getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image_png");
System.out.println("-----------SCREENSHOT TAKEN---------------------");
} catch (Exception e) {
e.printStackTrace();
}
}
}
=================TRIED EVEN THIS===========================

@Before("@Bestallhamtning")
public void before(Scenario scenario) {
this.scenario = scenario;
scenario.write("*****************************************************");
System.out.println("BEFOREEEEEEEEEEEEEEEEEEEE");}

@After("@Bestallhamtning")
public static void embedScreenshot(Scenario scenario) {
if (scenario.isFailed()) {
try {
byte[] screenshot = ((TakesScreenshot) selenium.w)
.getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image_png");
System.out.println("-----------SCREENSHOT TAKEN---------------------");
} catch (Exception e) {
e.printStackTrace();
}
}
}

If i put above code in runner-Initialization error...
[b:x2fyncfz]Could you please suggest sir.Am confused sir....[/b:x2fyncfz]