Hot questions for Using Cucumber in cucumber serenity

Top Java Programmings / Cucumber / cucumber serenity

Question:

I use Serenity BDD + Cucumber to perform my test, in fact that I have too many tests I need to run hem separately sometimes. Is it possible to execute Serenity BDD features by groups? Like in jUnit @Group(MyTestGroup.class)?

I can not find any examples.


Answer:

You can use tagging for that purpose. Information about that feature can be found in Serenity BDD official documentation.

Just a short excerpt, tags are added to jUnit tests using WithTag annotation. The following will add a tag of type feature (this is a default Serenity type) with name Reporting:

@WithTag(type="feature", name="Reporting")

Then you can run tests with this tag with jUnit like this:

mvn clean verify -Dtags="feature:Reporting"

Question:

Trying to run Cucumber with Serenity using JUnit 5.

When the Test Runner has a @RunWith(Cucumber.class) annotation, the tests run, although Serenity does not record the outcome.

When the Test Runner has @RunWith(CucumberWithSerenity.class), a "NoClassDefFound cucumber/runtime/junit/Assertions" is thrown.

I thought it may be JUnit 5, so I switched to JUnit 4 and the same outcome happens.

I loaded the sample project into GitHub: https://github.com/WB3Tech/Cucumber-Serenity-JUnit5

The goal is to build the project then be able to see the Cucumber results in the Serenity html output.

Anyone have an idea of where I may be going wrong?


Answer:

See https://github.com/serenity-bdd/serenity-core/#cucumber-4. I expect Serenity will only work with JUnit 5 in vintage mode.

Question:

I have a gradle + Serenity + RestAssured automated check suite setup that I usually run via gradle commands in shell sessions, but sometimes I need to run single Scenarios using IntelliJ.

When I run Scenarios on IntelliJ I usually get a lot of warnings like this one:

8312 [main] WARN cucumber.runtime.SerenityBackend - It looks like you are 
running a feature using @RunWith(Cucumber.class) 
instead of @RunWith(CucumberWithSerenity.class). 
Are you sure this is what you meant to do?

I'd like to know where and how I can configure the Run/Debug Configuration in IntelliJ in order to run the checks with the CucumberWithSerenity.class and fix the warning.

I'm using the following dependencies:

serenity-rest-assured:1.9.31
serenity-core:1.9.31
serenity-cucumber:1.9.12

IntelliJ version 2018.1.5 (Community Edition)


Answer:

Below are the steps from John Ferguson Smart' blog, author of Serenity-BDD (considering that you have installed Cucumber for Java plugin).

Running Cucumber with Serenity feature files directly from IntelliJ:

IntelliJ provides excellent integrated support for Cucumber feature files. You can even run features simply by right-clicking on the feature file. But this won’t work when you are using Cucumber with Serenity, as Serenity needs to instrument the feature file before execution. Fortunately, this is easy to fix. Here’s how:

  • Click on the feature file you want to run
  • In the Run menu Select Run…
  • In the contextual menu, select the feature, then "Edit…"
  • You should now see the ‘Edit Configuration Settings’ window. Set the main class to ‘net.serenitybdd.cucumber.cli.Main’
  • Change the Glue field to the root package of your project (or of your step definitions)
  • Click Apply

Now you can run your feature directly by right-clicking on the feature file.

P.S. Not all versions of Cucumber for Java plugin works correctly, especially when you have just updated IntelliJ IDEA to the latest version. I can confirm that next setup works correctly:

  • IntelliJ IDEA 2018.2.3 (Community version);
  • Cucumber for Java plugion version 182.3934;
  • net.serenity-bdd:serenity-core:2.0.6;
  • net.serenity-bdd:serenity-cucumber:1.9.18

Question:

I found an interesting blog explaining in general how to realize polymorphic step definitions using cucumber bdd and picocontainer dependency injection. You can visit the blog here

Where I get stuck is not knowing how to keep the automation logic behind a "generic" interface and then provide two different implementations, One that talks directly to the domain model for unit-integration testing, and another one that uses Selenium WebDriver for UI-testing.

Can someone give me advise/skeleton how to implement this. I can't thank you enough for helping the community.


Answer:

You can use a Utility class to feed the variables between classes.

For example, let's take an example of WebDriver driver initiation.

public class ClassUtility {

    public WebDriver baseDriver; 

}

You have a class where you want to access the webDriver.

public class InventoryPage extends ClassUtility {
private ClassUtility driver;
public InventoryPage(ClassUtility driver, ClassUtility fileElementLocator, ClassUtility elementLocatorProperties, ClassUtility page) {
    this.driver= driver;
    this.page =page;
}

@When("^Open the Google Page$")
public void openInventoryPage() throws Throwable {  
    driver.baseDriver = new FirefoxDriver();
    driver.baseDriver.get("www.google.com");
}

If you have to inject to different class, do it the similar fashion.

public class IntroductoryPage extends ClassUtility {

private ClassUtility driver;

public IntroductoryPage(ClassUtility driver, ClassUtility logger) {
    this.driver= driver;    
}

@When("^It should go to Account \"([^\"]*)\"")
public void openIntroductoryPage(String region) throws Throwable {
    driver.baseDriver.findElement(By.linkText("link")")).click();   
}

Please let me know if you need additional information. You can play around with the dependency injection as you wish.