Search code examples
seleniumtestngextentreports

Extent Reports: Test Steps are getting merged in last test in Extent Reports, When executing test in Parallel


The Test Steps and Test Logs are getting merged in to the single last test.

Extent Report 3.2

Actual Reports Function 1 logs

enter image description here

Function 2 logs [Having all steps]

enter image description here

My Project Structure is

enter image description here

HomePage.java

package pom;

import test.BaseTest;

public class HomePage extends BaseTest
{

    public void setClick()
    {
        test.pass("This test is pass which is in click of home page");

    }

    public void setName()
    {
        test.fail("This test is fail which is in set of home page");

    }

    public void select()
    {
        test.pass("This test is info which is in selct of home page");

    }
}

Test1.java

package test;

import org.testng.annotations.Test;

import pom.HomePage;

public class Test1 extends BaseTest
{

    @Test
    public void funtion1() 
    {
        HomePage hp = new HomePage();
        hp.setName();
        hp.setClick();
        hp.select();

        test.pass("Test is Passed! ins funtion 2");

    }
}

Test2.java


package test;

import org.testng.annotations.Test;

import pom.HomePage;

public class Test2 extends BaseTest
{
    @Test
    public void funtion2()
    {
        HomePage hp = new HomePage();
        hp.setClick();
        hp.select();

        test.pass("Test is Passed!");

    }
}


BaseTest.Java

package test;
import java.lang.reflect.Method;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;



import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class BaseTest
{
    public static ExtentHtmlReporter htmlReporter;
    public static ExtentReports extent;
    public static ExtentTest test;

    @BeforeSuite
    public void setUp()
    {
        htmlReporter = new ExtentHtmlReporter("./Reports/MyOwnReport.html");
        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);

        extent.setSystemInfo("OS", "Mac Sierra");
        extent.setSystemInfo("Host Name", "Jayshreekant");
        extent.setSystemInfo("Environment", "QA");
        extent.setSystemInfo("User Name", "Jayshreekant S");

        htmlReporter.config().setChartVisibilityOnOpen(false);
        htmlReporter.config().setDocumentTitle("AutomationTesting.in Demo Report");
        htmlReporter.config().setReportName("My Own Report");
        htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
        //htmlReporter.config().setTheme(Theme.DARK);
        htmlReporter.config().setTheme(Theme.STANDARD);
    }

    @BeforeMethod
    public void startTest(Method m)
    {
        test = extent.createTest(m.getName(),"This is the description of Test" + m.getName());

    }


    @AfterMethod
    public void getResult(ITestResult result)
    {
        if(result.getStatus() == ITestResult.FAILURE)
        {
            test.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:", ExtentColor.RED));
            test.fail(result.getThrowable());
        }
        else if(result.getStatus() == ITestResult.SUCCESS)
        {
            test.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" Test Case PASSED", ExtentColor.GREEN));
        }
        else
        {
            test.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE));
            test.skip(result.getThrowable());
        }
    }

    @AfterSuite
    public void tearDown()
    {
        extent.flush();
    }
}

testngall.xml

<suite name="Suite" parallel="tests">
    <test name="Test 1 ">
        <classes>
            <class name="test.Test1" />
        </classes>
    </test>
    <test name="Test 2">
        <classes>
            <class name="test.Test2" />
        </classes>
    </test>
</suite> <!-- Suite -->

So this is the entire project code structure, I am getting the logs appending in last test


Solution

  • This is your problem:

    public static ExtentTest test;
    

    Since this is static there is only ever one instance of it. When you run your tests in parallel this @BeforeMethod is called twice.

    @BeforeMethod
    public void startTest(Method m)
    {
        test = extent.createTest(m.getName(),"This is the description of Test" + m.getName());
    
    }
    

    The second time it is called the first test probably hasn't finished, but it is still referencing the test object so you will get the output of the second test and some parts of the first test that had no completed running at the point the @BeforeMethod was called. You are going to need to rewrite your code to not use a static test object.