Photo by RetroSupply on Unsplash
Generating Test Reports with a Spring Boot Selenium Automation Framework
Automated testing has become an integral part of software development processes. To perform automated testing with Selenium, we use a Selenium automation framework. In this article, we'll discuss how to generate test reports using a Spring Boot Selenium automation framework.
Setting up the Automation Framework
To set up the automation framework, we can use Maven to manage our dependencies. We need to include the following dependencies in our pom.xml
file:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webdriver</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.0.9</version>
</dependency>
In this example, we've included the selenium-java
and spring-boot-starter-webdriver
dependencies to set up our Selenium automation framework. We've also included the extentreports
library, which we'll use to generate our test reports.
Next, we can create a simple Selenium test class to test our application.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class MySeleniumTest {
@Autowired
private WebDriver driver;
@Test
public void testHomePage() {
driver.get("http://localhost:8080/");
// test code here
}
}
In this example, we've used the @SpringBootTest
annotation to start our application in a defined port. We also used the @Autowired
annotation to inject the WebDriver
instance into our test class.
Implementing Assertion Strategies
To implement assertion strategies, we can use the AssertJ library. AssertJ is a fluent library for making assertions in Java.
@Test
public void testHomePage() {
driver.get("http://localhost:8080/");
assertThat(driver.getTitle()).isEqualTo("Home Page");
assertThat(driver.findElement(By.tagName("h1")).getText())
.isEqualTo("Welcome to My Application");
}
In this example, we've used the assertThat()
method to make assertions on the title of the web page and the text content of the first h1
tag. AssertJ provides a wide range of assertion methods that make the code more readable and easier to understand.
We can also use the Hamcrest library for making assertions in Java. Hamcrest provides a wide range of matchers that can be used to make assertions on objects, strings, and collections.
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
@Test
public void testHomePage() {
driver.get("http://localhost:8080/");
assertThat(driver.getTitle(), equalTo("Home Page"));
assertThat(driver.findElements(By.tagName("h1")), hasSize(1));
}
In this example, we've used the assertThat()
and equalTo()
methods from the Hamcrest library to make assertions on the title of the web page. We've also used the assertThat()
and hasSize()
methods to make assertions on the number of h1
tags in the web page.
Generating Test Reports with Extent Reports
To generate test reports with Extent Reports, we need to create an instance of the ExtentReports
class in our test class.
public class MySeleniumTest {
private ExtentReports extent;
@Before
public void startReport() {
extent = new ExtentReports();
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("test-output/reports/MySeleniumTest.html");
extent.attachReporter(htmlReporter);
}
@Test
public void testHomePage() {
ExtentTest test = extent.createTest("testHomePage", "Test if the home page is working");
driver.get("http://localhost:8080/");
// test code here
test.pass("Home page is working");
}
@After
public void endReport() {
extent.flush();
}
}
In this example, we've created an instance of the ExtentReports
class and attached an ExtentHtmlReporter
to it. The ExtentHtmlReporter
will generate an HTML test report with the specified file name (test-output/reports/MySeleniumTest.html
).
Next, we've created an ExtentTest
instance for our test method and added a test description. We've also included our test code in the method and added a test status with the pass()
method.
Finally, we've flushed the ExtentReports
instance in our @After
method to create the test report.
Conclusion
In this article, we've discussed how to generate test reports in a Spring Boot Selenium automation framework using the extentreports
library. By setting up the automation framework, creating a Selenium test class, and using the ExtentReports
and ExtentHtmlReporter
classes, we can generate HTML test reports for our automated tests.