我從頭開始創建一個基于 Selenium POM 的框架,在其中我定義了頁面、頁面相關功能、三個不同包中的測驗。
在測驗定義的類中,我正在撰寫多個測驗用例。因此,在我運行一次測驗類之后,我希望它@AfterTest每次在@Test方法完成執行后都會執行。但它沒有按預期發生。
測驗用例包中的 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");
}
}
Functions 包中的 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();
}
}
pages包中的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有效。
uj5u.com熱心網友回復:
如果您需要在每個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() {
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/515482.html
上一篇:僅在單個div中選擇單選按鈕
下一篇:抓取資訊并將其存盤到csv檔案中
