【发布时间】:2018-05-08 05:15:47
【问题描述】:
在 Udemy 上学习 Testng 时,我遇到了一个我无法理解的代码。讲师创建了名为“testcore”的类,他在其中定义了@BeforeMethod/@aftermethod。后来他创建了另一个名为“LoginTest”的类,他在其中使用@test 编写了实际测试。他扩展了 loginTest 中的 testcore 类,以在 testcore 类中启动变量。当他运行 loginTest 时,@BeforeMethod/@aftermethod 也随之运行。 当这两个方法在不同的类中时,这两个方法是如何与@test 一起运行的。 这是两个代码:
public class testcore {
public static Properties config = new Properties();
public static Properties obj = new Properties();
public static Xls_Reader xls = null;
public static WebDriver driver;//=null;
@BeforeMethod
public void init() throws Exception{
if(driver==null) {
// Loading Config Properties File
File Config_f = new File(System.getProperty("user.dir")+"\\src\\dd_Properties\\config.properties");
FileInputStream fs = new FileInputStream(Config_f);
config.load(fs);
// Loading Object Properties File
File Obj_f = new File(System.getProperty("user.dir")+"\\src\\dd_Properties\\Object.properties");
fs = new FileInputStream(Obj_f);
obj.load(fs);
//Loading xlsx file
xls = new Xls_Reader(System.getProperty("user.dir")+"\\src\\dd_Properties\\Data.xlsx");
System.out.println(config.getProperty("browerName"));
if(config.getProperty("browerName").equalsIgnoreCase("Firefox")) {
driver = new FirefoxDriver();
}
else if(config.getProperty("browerName").equalsIgnoreCase("Chrome")) {
driver = new ChromeDriver();
}
else {
throw new Exception("Wrong/No Browswer sepcified");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
@AfterMethod
public void quitdriver() throws EmailException {
driver.quit();
//monitoringMail.Sendmail();
}
这里是 LoginTest 类:
public class LoginTest extends testcore {
@Test
public void doLogin() {
driver.findElement(By.xpath(obj.getProperty("LoginBtn"))).click();
driver.findElement(By.xpath(obj.getProperty("username"))).sendKeys();
driver.findElement(By.xpath(obj.getProperty("password"))).sendKeys();
}
【问题讨论】:
-
官方文档中对此进行了解释-testng.org/doc/documentation-main.html#methods
-
感谢您的回复。但我没有在 doc 中找到任何合适的内容来澄清我的疑问。请帮忙指出文档中的内容。
-
这是一个简单的Java继承概念。因为
LoginTest类扩展了testcore类,所以BeforeMethod和AfterMethod都可用于LoginTest类。因此,当您运行Test方法时,BeforeMethod和AfterMethod也会被执行。希望这会有所帮助。
标签: selenium selenium-webdriver testng