Serenity Framework Overview - Notes By ShariqSP

Serenity Framework Overview

The Serenity Framework is a popular open-source test automation framework designed to help developers and testers write automated acceptance and regression tests. It integrates seamlessly with tools like Selenium WebDriver and Appium for UI testing and works well for API testing. Serenity Framework is particularly useful for behavior-driven development (BDD) because it provides extensive reporting capabilities and supports tools like Cucumber and JBehave.

Key Features of Serenity Framework

  • Behavior-Driven Development (BDD) Support: Serenity integrates with BDD tools like Cucumber and JBehave, enabling teams to write tests in plain English (Gherkin syntax) that are easy to understand by both technical and non-technical stakeholders.
  • Rich Reporting: Serenity generates comprehensive, visually appealing reports that include screenshots, test execution status, and aggregated metrics.
  • Built-in Test Management: Provides annotations to manage and organize test cases linked to business requirements.
  • Integration with Selenium: Simplifies Selenium WebDriver integration, allowing focus on test logic rather than boilerplate code.
  • Scalability and Flexibility: Supports UI, API, and database testing.
  • Reusable Components: Promotes modular and maintainable test code with Page Objects and Actions classes.
  • Parallel Test Execution: Reduces test execution time by supporting parallel execution.
  • Integration with Build Tools: Works with Maven and Gradle for dependency management and test execution.
  • Cross-Browser Testing: Supports running tests on multiple browsers.
  • Parameterized Tests: Allows execution of tests with different input values.

How Serenity Framework Works

  1. Setup:

    Add Serenity dependencies to your Maven or Gradle project. For Maven, include the following in your pom.xml:

                  <dependency>
                      <groupId>net.serenity-bdd</groupId>
                      <artifactId>serenity-core</artifactId>
                      <version>[latest_version]</version>
                  </dependency>
                  <dependency>
                      <groupId>net.serenity-bdd</groupId>
                      <artifactId>serenity-junit</artifactId>
                      <version>[latest_version]</version>
                  </dependency>
                        
  2. Writing Tests: Write tests using Cucumber for BDD or JUnit for traditional test styles.
  3. Page Object Pattern: Define page classes for webpages, leveraging Serenity's built-in methods.
  4. Run Tests: Execute tests using commands like mvn clean verify.
  5. Generate Reports: Serenity automatically creates detailed HTML reports in the target/site/serenity directory.

Example of Serenity with Cucumber

Feature File (login.feature):

              Feature: User Login
              
                Scenario: Valid Login
                  Given the user is on the login page
                  When the user enters valid credentials
                  Then the user should see the dashboard
                

Step Definitions (LoginSteps.java):

              public class LoginSteps {
                  @Given("the user is on the login page")
                  public void userOnLoginPage() {
                      // Code to navigate to the login page
                  }
              
                  @When("the user enters valid credentials")
                  public void userEntersValidCredentials() {
                      // Code to enter username and password
                  }
              
                  @Then("the user should see the dashboard")
                  public void userSeesDashboard() {
                      // Code to verify the user is on the dashboard
                  }
              }
                

Page Object (LoginPage.java):

              @DefaultUrl("https://example.com/login")
              public class LoginPage extends PageObject {
              
                  @FindBy(id = "username")
                  private WebElementFacade usernameField;
              
                  @FindBy(id = "password")
                  private WebElementFacade passwordField;
              
                  @FindBy(id = "loginButton")
                  private WebElementFacade loginButton;
              
                  public void enterCredentials(String username, String password) {
                      usernameField.type(username);
                      passwordField.type(password);
                  }
              
                  public void clickLogin() {
                      loginButton.click();
                  }
              }
                

Advantages of Serenity Framework

  • Comprehensive Test Reporting: Makes debugging easier with detailed logs and screenshots.
  • Encourages Best Practices: Promotes modular and reusable test code.
  • Easy Integration: Works with Selenium, Appium, REST Assured, and other tools.
  • Improves Collaboration: BDD support helps bridge the gap between business and technical teams.

Use Cases for Serenity Framework

  • UI Testing: Automate tests for web applications with Selenium.
  • API Testing: Validate RESTful services with REST Assured integration.
  • BDD Testing: Write human-readable tests in Gherkin syntax with Cucumber or JBehave.
  • Regression Testing: Run automated tests to ensure existing features work as expected.

Serenity is ideal for teams seeking a unified framework that supports BDD, provides advanced reporting, and facilitates writing maintainable and scalable test automation scripts.