Search code examples
javaseleniumwebdriverextentreports

ExtentReports - separate steps for separate driver instances


I've got a Selenium/Java project where we're using ExtentReports. We use the PageFactory model, and have a BasePageFactory that takes care of all test preparation.

The Before method looks approximately like this:

@Before
public void beforeTest() throws MalformedURLException {
    this.driver = DriverBase.getDriver();
    driver.manage().window().maximize();
    Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
    browserName = cap.getBrowserName();
    ExtentReportsWrapper.startTest(getTestName(), "Browser: " + browserName + "<br>" + getDescription());
}

The wrapper for ExtentReports takes care of the reports, and it's called from the junit TestWatcher. We also use this method for posting 'info messages' - which are test steps. Those are usually placed in PageObject methods - everytime the method does a step that a manual tester would make (click, sendKeys, etc.)

public static void postInfoMessage(String message) {
    getRunningTest().log(Status.INFO, message + ".");
}

We have recently started writing tests that have multiple driver instances open. The reason for this is testing, for example, user rights. We wanted to do it in different instances to avoid having to log in and out all the time to check the rights. Instead, one user is changing the rights in one browser, and the other one trying them in the second browser.

My issue here are the ExtentReports, as they mix all the steps together, since the reports instance is bound to that BasePageFactory and not each and every browser instance.

Example simple test where a new user is created in one instance, and then logs in in the other instance:

    @Test
public void run() throws ItemNotFoundException {
    UserFacade userFacade = new UserFacade(driver);
    UserParams userParams = new UserParams();
    userParams.setLanguage(Language.ENGLISH);
    UserData userData = userFacade.createNewUser(userParams); //contains multiple postInfoMessage()

    WebDriver newDriver = this.getNewWebDriver();
    this.loginAnotherUser(newDriver, userParams.getNick(), userParams.getPassword()); //contains multiple postInfoMessage()

    this.closeWebDriver(newDriver);
    userFacade.deleteUser(this.userData); //contains multiple postInfoMessage()
}

As you can see, there are multiple steps that get printed in the report as info messages, but the steps are combined and in case of a really long test, it would be difficult to say which steps were performed in what instance.

The UserFacade uses the driver instance from BasePageFactory, which the test class extends. All new instances are created through getNewWebDriver()

I saw that ExtentReports offers something like child nodes, but I haven't been able to find much info on them or make any usable solutions. I'd appreciate any help.


Solution

  • I have a simple example which I used for learning extentreports (Not too successfully though). Maybe it helps...

        ExtentReports extent = new ExtentReports(Use your config);
        ExtentTest test = extent.startTest("Hello","Yeah"); 
    
        extent.loadConfig(ExtentReports.class, "extent-config.xml");
    
        test.log(LogStatus.PASS, "Before Step details");
    
        ExtentTest child1 = extent.startTest("Child 1");
        child1.log(LogStatus.INFO, "Info");
        child1.log(LogStatus.PASS, "Pass");
    
        ExtentTest child2 = extent.startTest("Child 2");
        child2.log(LogStatus.INFO, "Info");
        child2.log(LogStatus.PASS, "Pass");
    
        test.appendChild(child1).appendChild(child2);
    
        test.log(LogStatus.PASS, "After Step details");
    
        extent.endTest(test);
        extent.flush();     
        extent.close();
    

    The report generated, you can see the child nodes messages are separately added. enter image description here