Im从头开始创建一个基于SeleniumPOM的框架,在其中我在三个不同的包中定义了Pages、Pages相关函数和Test。
使用在测试中定义的类,我正在编写多个测试用例。因此,在我运行一次测试类之后,我希望它每次在@Test
方法完成执行后都会执行@AfterTest
。但事情并没有如预期的那样发生。
测试用例包中的LoginPageTests.java
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
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
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
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;
}
}
如果我运行单独的测试用例,它就会通过。如果我在LoginPageTests类中运行所有测试,一个通过,另一个将失败。如何有效地使用@Test
和@AfterTest
。
如果您需要在执行每个Test
方法后运行一个方法,则必须为提供AfterMethod
注释,而不是AfterTest
注释。
检查TestNG注释层次
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() {
}
}