Allure reporting integration in Selenium Java - Build an Automation Script
import io.qameta.allure.Allure; import io.qameta.allure.Step; import io.qameta.allure.Attachment; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.io.ByteArrayInputStream; import java.time.Duration; public class LoginTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test(description = "Verify login functionality with Allure reporting") public void testLogin() { openLoginPage(); enterUsername("testuser"); enterPassword("Test@1234"); clickLoginButton(); waitForDashboardUrl(); verifyDashboardPage(); } @Step("Open login page") public void openLoginPage() { driver.get("https://example.com/login"); Allure.step("Navigated to login page"); } @Step("Enter username: {username}") public void enterUsername(String username) { WebElement usernameField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); usernameField.clear(); usernameField.sendKeys(username); Allure.step("Entered username"); } @Step("Enter password") public void enterPassword(String password) { WebElement passwordField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password"))); passwordField.clear(); passwordField.sendKeys(password); Allure.step("Entered password"); } @Step("Click login button") public void clickLoginButton() { WebElement loginBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))); loginBtn.click(); Allure.step("Clicked login button"); } @Step("Wait for dashboard URL") public void waitForDashboardUrl() { boolean urlChanged = wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard")); Assert.assertTrue(urlChanged, "URL did not change to dashboard"); Allure.step("Dashboard URL loaded"); } @Step("Verify dashboard page title") public void verifyDashboardPage() { String expectedTitle = "Dashboard - Example App"; String actualTitle = driver.getTitle(); try { Assert.assertEquals(actualTitle, expectedTitle, "Page title mismatch"); Allure.step("Page title verified: " + actualTitle); } catch (AssertionError e) { saveScreenshot(); throw e; } } @Attachment(value = "Screenshot on failure", type = "image/png") public byte[] saveScreenshot() { return ((org.openqa.selenium.TakesScreenshot) driver).getScreenshotAs(org.openqa.selenium.OutputType.BYTES); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses Selenium WebDriver with TestNG and Allure for reporting.
Setup: We start ChromeDriver and create an explicit wait.
Test steps: Each step is a method annotated with @Step for Allure to log it clearly.
We open the login page, enter username and password, click login, then wait for the dashboard URL.
Assertions check the URL and page title. If the title assertion fails, a screenshot is attached to the Allure report using @Attachment.
This structure follows the Page Object Model style by separating actions into methods, uses explicit waits to avoid timing issues, and integrates Allure steps and attachments for clear, detailed reports.
Now add data-driven testing with 3 different username and password combinations using TestNG data provider.
Practice
Solution
Step 1: Understand Allure reporting purpose
Allure reporting is designed to make test results easy to read and share by adding detailed steps and descriptions.Step 2: Compare options with Allure features
Only To generate clear and shareable test reports with detailed steps correctly describes Allure's role in generating clear, detailed reports. Other options describe unrelated features.Final Answer:
To generate clear and shareable test reports with detailed steps -> Option AQuick Check:
Allure reporting = clear, shareable reports [OK]
- Confusing Allure with test execution speed tools
- Thinking Allure replaces Selenium WebDriver
- Assuming Allure writes tests automatically
Solution
Step 1: Identify Allure annotations for steps
The@Stepannotation marks methods as steps in Allure reports, adding detailed descriptions.Step 2: Eliminate unrelated annotations
@Test runs tests, @BeforeMethod runs setup, @Ignore skips tests. Only @Step adds step details.Final Answer:
@Step -> Option AQuick Check:
@Step = detailed step in Allure [OK]
- Confusing @Step with @Test annotation
- Using @BeforeMethod for step descriptions
- Thinking @Ignore adds report details
@Step("Open login page")
public void openLoginPage() {
driver.get("https://example.com/login");
}Solution
Step 1: Understand @Step annotation usage
The @Step annotation with a string parameter sets the step name shown in Allure reports.Step 2: Analyze the method behavior
When openLoginPage() runs, Allure logs the step with the name 'Open login page'. No return value is needed.Final Answer:
The report will show a step named 'Open login page' when this method runs -> Option CQuick Check:
@Step("Open login page") = step shown as 'Open login page' [OK]
- Thinking @Step needs a return value
- Assuming steps are ignored without extra config
- Expecting URL to appear instead of step name
Solution
Step 1: Check Allure integration requirements
Allure requires adding listeners or adapters (like Allure TestNG listener) to capture @Step annotations during test execution.Step 2: Identify common misconfigurations
Without the listener, steps are not recorded. The other options are incorrect because Allure supports Selenium tests, @Step works with TestNG, and @Test is for test methods.Final Answer:
You forgot to add the Allure listener or adapter in your test setup -> Option DQuick Check:
Missing listener = no steps in report [OK]
- Assuming @Step works without listener
- Confusing @Step with @Test annotation
- Believing Allure doesn't support Selenium
Solution
Step 1: Understand Maven and Allure commands
To run tests and generate an Allure report, first run tests withmvn clean test, then generate and serve the report withallure:serve.Step 2: Analyze command order
mvn clean test allure:serve correctly runs clean, then tests, then serves the report. Other options have wrong order or invalid commands.Final Answer:
mvn clean test allure:serve -> Option BQuick Check:
Run tests then serve report = mvn clean test allure:serve [OK]
- Running allure commands before tests
- Mixing command order incorrectly
- Using invalid Allure commands
Ready to prove it?
Pass the test and this pattern moves unknown
