Home > Back-end >  TestNG AfterMethod does not works as expected
TestNG AfterMethod does not works as expected

Time:10-17

Im creating a Selenium POM based framework from the scratch where i define Pages,Pages related functions, Tests in three different packages.

With in Tests defined class, I'm writing multiple test cases. So After i run a test class once, i'm hoping it would execute @AfterTest every time after a @Test method finishes its execution. But its not happening as expected.

LoginPageTests.java in testcases package

package testcases;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import functions.LoginPageFunctions;
import resources.BaseClass;

public class LoginPageTests extends BaseClass {
 public WebDriver driver;
    
    @BeforeTest
    public void initialize() {
        driver = initializeDriver();        
    }
    
    @Test(priority=0)
    public void checkLoginSuccessful() {
        LoginPageFunctions lpf = new LoginPageFunctions(driver);
        lpf.loginToPortal();
        String logoutText = lpf.locateLogoutBtn();
        String expectedText = "Logout";
        
        Assert.assertEquals(expectedText,logoutText);
    }
    
    @Test(priority=1)
    public void checkLoginUnsuccessful() {
        LoginPageFunctions lpf = new LoginPageFunctions(driver);
        lpf.loginWithInvalidData();
        String incorrectText = "Invalid credentials";
        String expectedIncorrectText = lpf.locateInvalidLoginButton();
        
        Assert.assertEquals(expectedIncorrectText,incorrectText);
        
    }
    
    
    @AfterTest
    public void teardown() {
        driver.close();
        System.out.println("You have closed the broswer");
    }
    
}

LoginPageFunctions.java in Functions package

package functions;
import org.openqa.selenium.WebDriver;
import pages.LoginPage;

public class LoginPageFunctions {
  
  WebDriver driver;
  LoginPage lp ;
  
  public LoginPageFunctions(WebDriver driver) {
      this.driver = driver;
  }
  
  public void loginToPortal() {
      lp= new LoginPage(driver);
      lp.enterUsername();
      lp.enterPassword();
      lp.clickSubmitBtn();
  }
  
  public String locateLogoutBtn() {
      lp= new LoginPage(driver);
      lp.clickUserSettingsBtn();
      return lp.locateLogOutBtn();
  }
  
  public void loginWithInvalidData() {
      lp = new LoginPage(driver);
      lp.enterUsername();
      lp.enterInvalidPassword();
      lp.clickSubmitBtn();
  }
  
  public String locateInvalidLoginButton() {
      return lp.locateInvalidLoginText();
  }
  
}

LoginPage.java in pages package

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class LoginPage {    
        
    By txt_username = By.name("username");
    By txt_password = By.name("password");
    By btn_submit = By.xpath("//button[1]");
    By btn_userSettings = By.xpath("//p[text()='Paul Collings']");
    By btn_logout = By.xpath("//a[text()='Logout']");
    By txt_invalidtxt = By.xpath("//p[text()='Invalid credentials']");
    
    
    public WebDriver driver;
    
    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }
    
      public void enterUsername() {
           driver.findElement(txt_username).sendKeys("Admin");
           System.out.println("Entered Username");
       }
      
      public void enterPassword() {
          driver.findElement(txt_password).sendKeys("admin123");
          System.out.println("Entered Password");
      }
      
      public void clickSubmitBtn() {
          driver.findElement(btn_submit).click();
          System.out.println("Clicked Submit button");
      }
      
      //We are using this to validate whether user is logged in
      public void clickUserSettingsBtn() {
          driver.findElement(btn_userSettings).click();
      }
      
      //We are using this to validate whether user is logged in
      public String locateLogOutBtn() {
         String logoutText= driver.findElement(btn_logout).getText();
         return logoutText;
      }
      
      
      public String locateInvalidLoginText() {
          String invalidText = driver.findElement(txt_invalidtxt).getText();
          return invalidText;
      }
      
      public void enterInvalidPassword() {
          driver.findElement(txt_password).sendKeys("xxx123");
          System.out.println("Entered Password");
      }

}

Base.java in resources package

package resources;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class BaseClass {
WebDriver driver;
String url = "https://opensource-demo.orangehrmlive.com/web/auth/login";

public WebDriver initializeDriver() {
    WebDriverManager.chromedriver().setup();        
    driver = new ChromeDriver();
    driver.navigate().to(url);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    return driver;
  }
}

If i run individual test cases, it gets passed. If i run all in LoginPageTests class, one gets passed, other will get failed. How to use @Test and @AfterTest affectively.

CodePudding user response:

If you need to run a method after each Test method is executed, You have to give the AfterMethod annotation to instead of AfterTest annotation.

Check TestNG annotation hierarchy

package testcases;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import functions.LoginPageFunctions;
import resources.BaseClass;

public class LoginPageTests extends BaseClass {
    public WebDriver driver;

    @BeforeMethod
    public void initialize() {
    }

    @Test(priority=0)
    public void checkLoginSuccessful() {
    }

    @Test(priority=1)
    public void checkLoginUnsuccessful() {
    }

    @AfterMethod
    public void teardown() {
    }
}
  • Related