Serenity BDD: basics of tests run
Continuing my tutorials about the Serenity BDD, I want to make an introduction to basics of tests run. In this post I’ll examine how to run Serenity BDD tests with JUnit in Eclipse. It’s pretty easy, but I think this will be useful for beginners, who want to know how to launch tests locally.
Since I’m going to build tests on top of the previous post about Serenity BDD, so if you still didn’t read it, it’s a good time to do this. For guys who have already read the article I’ll show the first simple test.
Simple Serenity BDD test
The most simple way to create a Serenity BDD test is to create a class with a void method and decorate them with several annotations. Sounds simple? Let’s look on it on practice:
@RunWith(SerenityRunner.class) @Story(ProductSection.class) public class JustOneProductTest { private String productName = "Evernote"; @Managed public WebDriver webDriver; @ManagedPages public Pages pages; @Steps public GlobalSteps globalSteps; @Test public void checkEvernoteStaticPage() { globalSteps.clickFooterLink(productName); globalSteps.checkLabelHeader(productName); globalSteps.checkPageTitle(productName); } }
The SerenityRunner class indicates that this is a Thucydides tests. The @Managed and @ManagedPages annotations and fields are required to take care of our page objects.
As you can see the test method is developed just with the help of globalSteps object and invocation of its methods. On the Serenity BDD language any class which extends ScenarioSteps class calls the step library. Also the JustOneProductTest class decorated with @Story annotation. It indicates that all test inside the class is mapped to the particular User Story.
The Pages object is a factory class that Serenity BDD uses to provide page objects to the tests and test steps.
Summarising I want to underline high code readability and convenient structure for a maintenance. Almost all testing team members can learn how to work with Serenity BDD without any problems.
Parameterised Serenity BDD test
Analysing the User Story and the previous chapter, you can suggest, that in order to develop all of required test scenarios for the footer links, you have to develop several tests with same logic but with different data. Yeah, definitely it’s the most straightforward way. But Serenity BDD has its own opinion regarding such situations.
@RunWith(SerenityParameterizedRunner.class) @Story(ProductSection.class) public class FooterProductSectionTest { private String productName; @TestData public static Collection< Object[] > testData() { return Arrays.asList(new Object[][] { {"Evernote"}, {"Skitch"}, {"Penultimate"} }); } public FooterProductSectionTest(String productName) { this.productName = productName; } @Managed public WebDriver webDriver; @ManagedPages public Pages pages; @Steps public GlobalSteps globalSteps; @Test public void checkProductStaticPages() { globalSteps.clickFooterLink(productName); globalSteps.checkLabelHeader(productName); globalSteps.checkPageTitle(productName); } }
You probably guessed that I talked about parameterised tests. Pay attention to the new argument of @RunWith annotation. Then look on the static method decorated with @TestData annotation. Inside of the method I specify the data which will be applied to the test method in the end of the FooterProductSectionTest class. Also important to define the constructor of the FooterProductSectionTest class, because it invokes again and again for each data set (in my example – three times).
Usage of parameterised tests are very helpful in cases where you need to perform one test scenario at least twice with different data sets. Such approach is know as Data Driven Testing.
Summary
I believe that my articles were interesting and helpful for you. Be sure that Serenity BDD is much bigger and not all of its powerful methods were overviewed here, but I provided enough information for its basic usage. Therefore I definitely will write more tutorials about Serenity BDD in the future.