【发布时间】:2021-10-28 04:13:30
【问题描述】:
在 Udemy 上学习 TestNG 时,我遇到了一个我无法理解的代码。讲师创建了一个名为“TestBase”的类,他在其中定义了@BeforeMethod/@aftermethod。后来他创建了另一个名为“LoginTest”的类,他在其中使用@test 编写了实际测试。他在 loginTest 中扩展了 TestBase 类以获取在 TestBase 类中启动的变量。当他运行 loginTest 时,@BeforeMethod/@aftermethod 也随之运行。当这些方法在不同的类中时,这两种方法是如何与 @test 一起运行的。这是两个代码:
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/");
}
}
【问题讨论】:
标签: java selenium testng ui-automation