0

I am setting up a new framework and want to implement Extent Report as part of IInvokedMethod listeners so that before every method I can start the extentTest and log the steps.

Currently I am starting the extentTest(reference of ExtentTest declared in BaseTest class) in every method in test class. But I don't want to write the same code again and again. Below is my code. Is there any way that I can initialize extentTest in testng listeners and use the same in below test class?

Current Code(Test Class):

public class ApplyNowPageTests extends BaseTest {

    @Test(groups = {"Apply Now"}, enabled = false, dataProvider = "validStates", dataProviderClass = GeneralDataProvider.class)
    public void verifyApplyNowRangeValues(String state) throws Exception {
        testCaseName = className + " : " +state;
        extentTest = extentReport.startTest(
        className + " : " + new Throwable().getStackTrace()[0].getMethodName()+" - " +state);
        extentTest.log(LogStatus.INFO, "Starting the test");
        extentTest.assignAuthor("Ankur Magan");

        ApplyNow applyNow = new ApplyNow(driver, extentTest);
        PageFactory.initElements(new AppiumFieldDecorator(driver), applyNow);

        applyNow.gotoApplyNowFlow();
        applyNow.enterEmail(dataClient.getEmail());
        applyNow.enterState(state);
        applyNow.verifyRangeSliderDisplayed();

        Map<String, String> loanActualValues =applyNow.getRangeSliderAmount();

        Assert.assertEquals(States.getStateMinAmount(state), loanActualValues.get("min"));
        Assert.assertEquals(States.getStateMaxAmount(state), loanActualValues.get("max"));
        applyNow.reportPass(state + " : Passed ");
    }


    /*  Verify Apply Now - Login Functionality*/

    @Test(groups = {"Apply Now"}, enabled = true)
    public void verifyApplyNowLogin() throws Throwable {
        testCaseName = className + " : " +new Throwable().getStackTrace()[0].getMethodName();
        extentTest = extentReport.startTest(
        className + " : " + new Throwable().getStackTrace()[0].getMethodName());
        extentTest.log(LogStatus.INFO, "Starting the test");
        extentTest.assignAuthor("Ankur Magan");

        ApplyNow applyNow = new ApplyNow(driver, extentTest);
        PageFactory.initElements(new AppiumFieldDecorator(driver), applyNow);

        CreateYourAccount createAccnt=applyNow.completeApplyNowPage(dataClient);
        createAccnt.completeCreateYourAccountPage(dataClient);  
    }
}
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
Ankur
  • 51
  • 6

2 Answers2

1

But I don't want to write the same code again and again. Below is my code. Is there any way that I can initialize extentTest in testng listeners and use the same in below test class?

@BeforeEach
public void init(){
//This method will be executed before every @Test

}

check this answer

Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
0
public class MyTest{

@BeforeMethod
public void init(){
//your logic
    testCaseName = className + " : " +state;
    extentTest = extentReport.startTest(
    className + " : " + new Throwable().getStackTrace()[0].getMethodName()+" - " +state);
    extentTest.log(LogStatus.INFO, "Starting the test");
    extentTest.assignAuthor("Ankur Magan");
//.............

}

@Test
public void test1(){
    //test some
}

@Test
public void test2(){
    //
}

}

Annotate init() with @BeforeMethod annotation. See http://testng.org/doc/documentation-main.html#annotations

Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59