displayName
displayName

Reputation: 215

How to instantiate objects before testNG class?

I have multiple testNG test classes that extend a Base Test class and all use the same common objects. I want to have the object creation done automatically in BaseTest so I don't have to include it in each test class. As of now, the code only works if I add createPages() to the start of the test. I tried putting them in the BaseTest class using @BeforeClass and @BeforeSuite but both gave a null pointer exception meaning they weren't instantiated before the @Test test123 was run I beleive.

public someTest extends BaseTest {

    @Test
    public void test123(){
        createPages(); //i want to be able to remove this and have it done in BaseTest
        menuPage.scroll();
    }
}

public BaseTest {
    MenuPage menuPage;

    public void createPages() {
        menuPage = new MenuPage(getDriver());
    }
/*
    @BeforeSuite
    public void beforeSuite() {
        createPages();
    }

    @BeforeClass
    public void beforeClass() {
        createPages();
    }
*/
}

Upvotes: 0

Views: 320

Answers (1)

Amit Kumar Lal
Amit Kumar Lal

Reputation: 5779

@beforeTest is one such annotation. A method with @beforeTest annotation will run, before any test method belonging to the classes inside the test tag

inside ur BaseTest

public class Basetest{
 @BeforeTest
    public void doBeforeTest() {
        createPages();
    }
}

Upvotes: 2

Related Questions