- Integration of Allure Report with Selenium and TestNG
- What is Allure Framework?
- Pre-Requisite:
- This framework consists of:
- Implementation Steps
- Step 1 – Update Properties section
- Step 2 – Add Selenium, TestNG, and Allure TestNG dependencies in POM.xml
- Step 3 – Update the Build Section of pom.xml in the Allure Report Project
- Step 4 – Create Pages and Test Code for the pages
- Step 5 – Create testng.xml for the project
- Step 6 – Run the Test and Generate Allure Report
- Allure Report Dashboard
- Categories in Allure Report
- Suites in Allure Report
- Graphs in Allure Report
- Timeline in Allure Report
- Behaviors of Allure Report
- Packages in Allure Report
- Integration of Allure Report with Rest Assured and TestNG
- Pre-Requisite
- This framework consists of:
- Implementation Steps
- Step 1 – Update Properties section in Maven pom.xml
- Step 2 – Add the Allure-Rest Assured dependency
- Add other dependencies like Rest Assured and Allure-TetNG dependencies in POM.xml
- Step 3 – Update the Build Section of pom.xml in Allure Report Project
- Step 4 – Create the Test Code for the testing of REST API under src/test/java
- Step 5 – Create testng.xml for the project
- Step 6 – Run the Test and Generate Allure Report
- Allure Report Dashboard
- Categories in Allure Report
- Suites in Allure Report
- View test history
- Graphs in Allure Report
- Timeline in Allure Report
- Behaviors of Allure Report
- Packages in Allure Report
Integration of Allure Report with Selenium and TestNG
In this tutorial, I will explain how to integrate Allure Report (one of the very famous Reports) with Selenium and TestNG.
What is Allure Framework?
Allure is an open-source framework designed to create interactive and comprehensive test reports by Yandex QA Team.
The below example covers the implementation of Allure Reports in Selenium using TestNG, Java, and Maven.
Pre-Requisite:
- Java 11 installed
- Maven installed
- Eclipse or IntelliJ installed
- Environment variables JAVA_HOME, MAVEN_HOME and ALLURE_HOME are correctly configured
This framework consists of:
- Selenium – 3.141.59
- Java 11
- TestNG – 7.4.0
- Maven – 3.8.1
- Allure Report – 2.14.0
- Allure TestNG – 2.14.0
Implementation Steps
- Update the Propertiessection in Maven pom.xml
- Add Selenium, TestNG and Allure TestNG dependencies in POM.xml
- Update Build Section of pom.xml in Allure Report Project.
- Create Pages and Test Code for the pages
- Create testng.xml for the project
- Run the Test and Generate Allure Report
Step 1 – Update Properties section
UTF-8 3.141.59 7.4.0 2.14.0 3.5.1 11 11 1.9.6 3.0.0-M5
Step 2 – Add Selenium, TestNG, and Allure TestNG dependencies in POM.xml
org.seleniumhq.selenium selenium-java $org.testng testng $test io.qameta.allure allure-testng $test
Step 3 – Update the Build Section of pom.xml in the Allure Report Project
org.apache.maven.plugins maven-compiler-plugin $$ $ org.apache.maven.plugins maven-surefire-plugin $TestNG.xml -javaagent:"$/org/aspectj/aspectjweaver/$/aspectjweaver-$.jar" org.aspectj aspectjweaver $
Step 4 – Create Pages and Test Code for the pages
Below is the sample project which uses Selenium and TestNG which is used to generate an Allure Report.
We have 2 pages. Below is the code for Login Page which contains all the web elements and methods related to that web elements.
LoginPage.java
public class LoginPage < WebDriver driver; By userName = By.name("txtUsername"); By password = By.name("txtPassword"); By titleText = By.id("logInPanelHeading"); By login = By.id("btnLogin"); By errorMessage = By.id("spanMessage"); public LoginPage(WebDriver driver) < this.driver = driver; >// Set user name in textbox public void setUserName(String strUserName) < driver.findElement(userName).sendKeys(strUserName); >// Set password in password textbox public void setPassword(String strPassword) < driver.findElement(password).sendKeys(strPassword); >// Click on login button public void clickLogin() < driver.findElement(login).click(); >@Step("Verify title of Login Page") public void verifyPageTitle() < String loginPageTitle = driver.findElement(titleText).getText(); Assert.assertTrue(loginPageTitle.contains("LOGIN Panel")); >/* Failed Test */ @Step("Verify error message when invalid credentail is provided") public void verifyErrorMessage() < String invalidCredentialErrorMessage = driver.findElement(errorMessage).getText(); Assert.assertTrue(invalidCredentialErrorMessage.contains("Incorrect Credentials")); >@Step("Enter username and password") public void login(String strUserName, String strPasword) < // Fill user name this.setUserName(strUserName); // Fill password this.setPassword(strPasword); // Click Login button this.clickLogin(); >>
Dashboard.java
public class DashboardPage < WebDriver driver; By dashboardPageTitle = By.id("welcome"); By options = By.cssSelector( "#dashboard-quick-launch-panel-menu_holder >table > tbody > tr > td:nth-child(1) > div > a > span"); public DashboardPage(WebDriver driver) < this.driver = driver; >@Step("Verify title of Dashboard page") public void verifyDashboardPageTitle() < String DashboardPageTitle = driver.findElement(dashboardPageTitle).getText(); Assert.assertTrue(DashboardPageTitle.contains("Welcome")); >@Step("Verify Quick Launch Options on Dashboard page") public void verifyQuickLaunchOptions() < String QuickLaunchOptions = driver.findElement(options).getText(); Assert.assertTrue(QuickLaunchOptions.contains("Assign Leave")); >>
Below are the Test classes for Login Page and Dashboard Page. Here, we have BaseTest Class also which contains the common methods needed by other test pages.
BaseTest.java
public class BaseTest < public static WebDriver driver; LoginPage objLogin; DashboardPage objDashboardPage; @Step("Start the application") @BeforeMethod public void setup() < System.setProperty("webdriver.gecko.driver", "C:\\Users\\Vibha\\Software\\geckodriver-v0.26.0-win64\\geckodriver.exe"); driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://opensource-demo.orangehrmlive.com/"); >@Step("Stop the application") @AfterMethod public void close() < driver.close(); >>
LoginTests.java
@Epic("Web Application Regression Testing") @Feature("Login Page Tests") @Listeners(TestExecutionListener.class) public class LoginTests extends BaseTest < LoginPage objLogin; DashboardPage objDashboardPage; @Severity(SeverityLevel.NORMAL) @Test(priority = 0, description = "Verify Login Page") @Description("Test Description : Verify the title of Login Page") @Story("Title of Login Page") public void verifyLoginPage() < // Create Login Page object objLogin = new LoginPage(driver); // Verify login page text objLogin.verifyPageTitle(); >/* Failed Test */ @Severity(SeverityLevel.BLOCKER) @Test(priority = 1, description = "Login with invalid username and password") @Description("Test Description : Login Test with invalid credentials") @Story("Unsuccessful Login to Application") public void invalidCredentialTest() < // Create Login Page object objLogin = new LoginPage(driver); objLogin.login("test", "test123"); // Verify login page text objLogin.verifyErrorMessage(); >>
We can order tests by severity by using @Severity annotation. Click here to know more about other Allure annotations.
DashboardTests.java
@Epic("Web Application Regression Testing") @Feature("Dashboard Page Tests") @Listeners(TestExecutionListener.class) public class DashboardTests extends BaseTest < LoginPage objLogin; DashboardPage objDashboardPage; @Severity(SeverityLevel.BLOCKER) @Test(priority = 0, description = "Verify Dashboard Page") @Description("Test Description : After successful login to application opens Dashboard page") @Story("Successful login of application opens Dashboard Page") public void DasboardTest() < objLogin = new LoginPage(driver); // login to application objLogin.login("Admin", "admin123"); // go the dashboard page objDashboardPage = new DashboardPage(driver); // Verify dashboard page objDashboardPage.verifyQuickLaunchOptions(); >>
We can group tests with @Epic, @Feature, and @Stories annotations. Click here to know more about other Allure annotations.
TestExecutionListener.class
We can add attachments to our reports by using @Attachment annotation. It can return String, byte [], etc. I need to add @Listeners(< TestExecutionListener.class >) declaration at the top of the test classes. Click here to know more about other Allure annotations.
public class TestExecutionListener implements ITestListener < @Attachment(value = "Screenshot of ", type = "image/png") public byte[] saveScreenshot(String name, WebDriver driver) < return (byte[]) ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); >@Override public void onTestFailure(ITestResult result) < saveScreenshot(result.getName(), BaseTest.driver); >>
Step 5 – Create testng.xml for the project
Step 6 – Run the Test and Generate Allure Report
To run the tests, use the below command
In the below image, we can see that one test failed and two passed out of three tests.
To create an Allure Report, use the below command
This will generate the beautiful Allure Test Report as shown below.
Allure Report Dashboard
The overview page hosts several default widgets representing the basic characteristics of your project and test environment.
- Statistics – overall report statistics.
- Launches – if this report represents several test launches, statistics per launch will be shown here.
- Behaviors – information on results aggregated according to stories and features.
- Executors – information on test executors that were used to run the tests.
- History Trend – if tests accumulated some historical data, it’s a trend that will be calculated and shown on the graph.
- Environment – information on the test environment.
Categories in Allure Report
The categories tab gives you a way to create custom defects classifications to apply for test results. There are two categories of defects – Product Defects (failed tests) and Test Defects (broken tests).
Suites in Allure Report
On the Suites tab a standard structural representation of executed tests, grouped by suites and classes can be found.
Graphs in Allure Report
Graphs allow you to see different statistics collected from the test data: status breakdown or severity and duration diagrams.
Timeline in Allure Report
The timeline tab visualizes retrospective of tests execution, allure adaptors collect precise timings of tests, and here on this tab, they are arranged accordingly to their sequential or parallel timing structure.
Behaviors of Allure Report
This tab groups test results according to Epic, Feature, and Story tags.
Packages in Allure Report
The packages tab represents a tree-like layout of test results, grouped by different packages.
If you click on the (highlighted tab), it will show the test execution report in the below format.
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!
Additional tutorials on Allure Reports:
Integration of Allure Report with Rest Assured and TestNG
In the previous tutorial, I explained the Integration of the Allure Report with Rest Assured with JUnit4. In this tutorial, I will explain how to Integrate Allure Report with Rest Assured and TestNG.
The below example covers the implementation of Allure Report for Rest API using Rest Assured, TestNG, Java, and Maven.
Pre-Requisite
This framework consists of:
- Java 11
- Maven – 3.8.1
- Allure Report – 2.14.0
- Rest Assured – 4.4.0
- Allure Rest Assured – 2.14.0
- Allure TestNG – 2.14.0
- Aspectj – 1.9.6
Implementation Steps
- Update Propertiessection in Maven pom.xml
- Add Rest Assured, Allure-Rest Assured and Allure-TetNG dependencies in POM.xml
- Update BuildSection of pom.xml in Allure Report Project.
- Create the Test Code for the testing of REST API under src/test/java
- Create TestNG.xml for the project
- Run the Testsand Generate Allure Report
Step 1 – Update Properties section in Maven pom.xml
UTF-8 4.4.0 2.14.0 2.14.0 2.12.3 20210307 3.5.1 11 11 1.9.6 3.0.0-M5
Step 2 – Add the Allure-Rest Assured dependency
io.qameta.allure allure-rest-assured $
Add other dependencies like Rest Assured and Allure-TetNG dependencies in POM.xml
io.qameta.allure allure-testng $test io.rest-assured rest-assured $test com.fasterxml.jackson.core jackson-databind $org.json json $
Step 3 – Update the Build Section of pom.xml in Allure Report Project
org.apache.maven.plugins maven-compiler-plugin $$ $ org.apache.maven.plugins maven-surefire-plugin $TestNG.xml -javaagent:"$/org/aspectj/aspectjweaver/$/aspectjweaver-$.jar" org.aspectj aspectjweaver $
Step 4 – Create the Test Code for the testing of REST API under src/test/java
To see our request and response in more detail using Rest Assured, we need to add a line to our Rest Assured tests. This will provide the request and response details in the report.
.filter(new AllureRestAssured())
@Epic("REST API Regression Testing using TestNG") @Feature("Verify CRUID Operations on Employee module") public class EmployeeDetailsTest < String BaseURL = "http://dummy.restapiexample.com/api"; @Test(description = "GET Request Operation") @Story("GET Request with Valid User") @Severity(SeverityLevel.NORMAL) @Description("Test Description : Verify the details of employee of id-2") public void verifyUser() < // GIVEN given() .filter(new AllureRestAssured()) // WHEN .when() .get(BaseURL + "/v1/employee/2") // THEN .then() .statusCode(200) .statusLine("HTTP/1.1 200 OK") // To verify booking id at index 2 .body("data.employee_name", equalTo("Garrett Winters")) .body("message", equalTo("Successfully! Record has been fetched.")); >@Test(description = "GET Request Operation") @Story("GET Request with Invalid User") @Severity(SeverityLevel.NORMAL) @Description("Test Description : Verify the details of employee of id-99999") public void verifyInvalidUser() < // Given given() .filter(new AllureRestAssured()) // WHEN .when() .get(BaseURL + "/v1/employee/99999") // THEN .then() .statusCode(200) .statusLine("HTTP/1.1 200 OK") .body("message", equalTo("Successfully! Record has not been fetched.")); >@Test(description = "POST Request Operation") @Story("POST Request") @Severity(SeverityLevel.NORMAL) @Description("Test Description : Verify the creation of a new employee") public void createUser() < JSONObject data = new JSONObject(); data.put("employee_name", "APITest"); data.put("employee_salary", "99999"); data.put("employee_age", "30"); //GIVEN given() .filter(new AllureRestAssured()) .contentType(ContentType.JSON) .body(data.toString()) // WHEN .when() .post(BaseURL + "/v1/create") // THEN .then() .statusCode(200) .body("data.employee_name", equalTo("APITest")) .body("message", equalTo("Successfully! Record has been added.")); >>
Step 5 – Create testng.xml for the project
Step 6 – Run the Test and Generate Allure Report
To run the tests, use the below command
In the below image, we can see that two tests failed and one passed out of three tests.
This will create the allure-results folder with all the test reports. These files will be used to generate the Allure Report.
To create Allure Report, use the below command
This will generate the beautiful Allure Test Report as shown below.
Allure Report Dashboard
The overview page hosts several default widgets representing the basic characteristics of your project and test environment.
Categories in Allure Report
The categories tab gives you a way to create custom defect classifications to apply for test results. There are two categories of defects – Product Defects (failed tests) and Test Defects (broken tests).
Suites in Allure Report
On the Suites tab a standard structural representation of executed tests, grouped by suites and classes, can be found.
View test history
Each time you run the report from the command line with the mvn clean test command, a new result JSON file will get added to the allure-results folder. Allure can use those files to include a historical view of your tests. Let’s give that a try.
To get started, run mvn clean test a few times and watch how the number of files in the allure-reports folder grows.
Now go back to view your report. Select Suites from the left nav, select one of your tests and click Retries in the right pane. You should see the history of test runs for that test:
Graphs in Allure Report
Graphs allow you to see different statistics collected from the test data: status breakdown or severity and duration diagrams.
Timeline in Allure Report
Timeline tab visualizes retrospective of tests execution, allure adaptors collect precise timings of tests, and here on this tab, they are arranged accordingly to their sequential or parallel timing structure.
Behaviors of Allure Report
This tab groups test results according to Epic, Feature, and Story tags.
Packages in Allure Report
The packages tab represents a tree-like layout of test results, grouped by different packages.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!