Skip to content
Advertisement

How TestNG annotation from base class is executed when the Test inside the derived class is executed?

While learning TestNG on Udemy, I come across a code that I am unable to understand. The instructor has created a class named “TestBase” where he defined @BeforeMethod/@aftermethod.Later he created another class named “LoginTest” where he wrote the actual test with @test. He extended TestBase class in loginTest to get variable initiated in TestBase class. When he ran loginTest then @BeforeMethod/@aftermethod also ran with this. How did these two methods ran along with @test when these methods are in different classes. here are both codes:

public class TestBase {

    public static String getURL() {
        String URL = null;
        switch (GetProperties.getPropertyValueByKey("env")) {
            case "qa":
                URL = GetProperties.getPropertyValueByKey("qaUrl");
                break;
            case "dev":
                URL = GetProperties.getPropertyValueByKey("devUrl");
                break;
            case "uat":
                URL = GetProperties.getPropertyValueByKey("uatUrl");
                break;
            case "prod":
                URL = GetProperties.getPropertyValueByKey("prodUrl");
                break;
            default:
                LogFactory.info("No env has been set in Properties file");
        }
        return URL;
    }

    @BeforeMethod
    public void setup() {
        //ToDo: Pass browser value from config.properties
        WebDriver driver = BrowserFactory.create(GetProperties.getPropertyValueByKey("browser"));
        DriverFactory.setDriver(driver);
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.get(getURL());
        driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(Constants.PAGE_LOAD_TIMEOUT));
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(Constants.IMPLICIT_WAIT));
    }

    @AfterMethod
    public void tearDown() {
        if (null != DriverFactory.getDriver()) {
            try {
                DriverFactory.getDriver().quit(); // quit WebDriver session gracefully
                DriverFactory.removeDriver();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }



public class LoginTest extends TestBase {

    /**
     * Below Login Test case has hardcoded data being passed from test method itself
     **/
    @Test(description = "Verify agent login with valid credentials")
    public void loginWithValidCredentials() {
        LoginPage loginPage = new LoginPage();
        DashboardPage dashboardPage = new DashboardPage();
        loginPage.loginWithValidUser("xyx@yopmail.com", "Hello1136");
        try {
            Thread.sleep(10000); // Added just for now will remove this in future and will implement proper selenium waits !
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Assert.assertEquals(dashboardPage.getDashboardPageURL(), Constants.URL + "/dashboard/");
    }
}

Advertisement

Answer

If you read this line :

public class LoginTest extends TestBase 

this clearly tells that, LoginTest is a child class of TestBase .

so TestBase gets more precedence.

Now let’s understand what is @BeforeMethod.

@BeforeMethod

The annotated method will be run before each test method.

so this is by default Testng architecture to run @BeforeMethod before each @Test in your test suite.

Your program execution should be in this order :-

@BeforeMethod

then

@Test

then

@AfterMethod

if you have more than one @Test, the order should be same.

You can refer here and the above reference has been taken from TestNG official docs.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement