I am using TestNG and annotations- "dependsonMethods"
Code sample:
@Test(dependsOnMethods = { "test2" })
public void Test1() throws IOException { }
Now, programmatically need to get the name of the the Test method (Test1) which has triggered the other Test method (Test2).
******************updated the context & code samples**************************
I am running the TestNg,java, extent reports 4.0.9, selenium tests in Maven project.
CURRENT: Test [addAndPopulateTest] which calls [addTest] using TestNg-"dependsonMethods". After the run, Extent Report shows the results of [addAndPopulateTest] & [addTest] separately as 2 individual tests. This is the link to the current situation : https://imgur.com/ZnBNqzo
EXPECTED: Extent HTML report should show- Single test i.e.[addAndPopulateTest] with 2 child tests i.e. [addTest] & [addAndPopulateTest]. This is the link to the ToBe situation : https://imgur.com/NKMxoIB
*****Code Samples
///TC_addAndPopulateTest
package com.tra.testCases;
import org.testng.annotations.Test;
import com.tram.pageObjects.BaseClass;
public class TC_addAndPopulateTest extends TC_addTest {
@Test(dependsOnMethods = { "addTest" })
public void addAndPopulateTest() throws IOException, InterruptedException {
System.out.println("Currently executing Test-> addAndPopulateTest");
}
}
///TC_addTest
package com.tram.testCases;
import java.io.IOException;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.tram.pageObjects.BaseClass;
public class TC_addTest extends BaseClass {
@Test()
public void addTest() throws IOException, InterruptedException {
System.out.println("Currently executing Test->'addTest'");
}
}
///Reporting Class
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.tram.pageObjects.BaseClass;
public class Reporting extends BaseClass implements ITestListener {
private static ExtentReports extent =
ExtentManager.createInstance("Extent-Report.html");
private static ThreadLocal parentTest = new ThreadLocal();
private static ThreadLocal test = new ThreadLocal();
@Override
public synchronized void onStart(ITestContext context) {
}
@Override
public synchronized void onFinish(ITestContext context) {
extent.flush();
}
@Override
public synchronized void onTestStart(ITestResult result) {
**//Currently, [addTest] is ready to be executed, So, need to the find
that this test method [addTest] has triggered directly by [TC_addTest]
class or by other test method.
A] If directly by the test class, set
PARENT=result.getMethod().getMethodName()
B] if indirectly by the other test method, set PARENT= Name of the test
method [addAndPopulateTest] which has triggered the [addTest] //**
ExtentTest parent =
Extent.createTest(result.getMethod().getMethodName());
parentTest.set(parent);
}
@Override
public synchronized void onTestSuccess(ITestResult result) {
ExtentTest child = ((ExtentTest)
parentTest.get()).createNode(result.getMethod().getMethodName());
test.set(child);
((ExtentTest) test.get()).pass("Test passed");
}
@Override
public synchronized void onTestFailure(ITestResult result) {
((ExtentTest) test.get()).fail(result.getThrowable());
}
@Override
public synchronized void onTestSkipped(ITestResult result) {
((ExtentTest) test.get()).skip(result.getThrowable());
}
@Override
public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult
result) {
}
}
//Extent Manager
package com.tram.utilities;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class ExtentManager {
private static ExtentReports extent;
public static ExtentReports getInstance() {
if (extent == null)
createInstance("test-output/extent.html");
return extent;
}
public static ExtentReports createInstance(String fileName) {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setDocumentTitle(fileName);
htmlReporter.config().setReportName(fileName);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
return extent;
}
}