Ankur
Ankur

Reputation: 71

Can we use variables defined in testng listeners in any of the test class?

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);  
    }
}

Upvotes: 0

Views: 140

Answers (2)

hhrzc
hhrzc

Reputation: 2051

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

Upvotes: 0

Jude Niroshan
Jude Niroshan

Reputation: 4460

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

Upvotes: 1

Related Questions