一个类中提到的TestNg注释是如何从另一个类中的另一个类执行的?

时间:2018-05-08 05:15:48

标签: selenium selenium-webdriver testng

在Udemy上学习Testng时,我遇到了一个我无法理解的代码。教师创建了一个名为“testcore”的类,他定义了@ BeforeMethod / @ aftermethod.Later他创建了另一个名为“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();


}

2 个答案:

答案 0 :(得分:1)

1)这两种方法是如何与@test一起运行的。

首先 LoginTest 扩展 testcore

由于这个原因,我们可以在LoginTest类中继承testcore的方法。

2)当他运行loginTest时,@ BeforeMethod / @ aftermethod也运行了这个。

请参阅以下详细信息

TestNG类的配置信息:

@BeforeSuite: The annotated method will be run before all tests in this suite have run. 
@AfterSuite: The annotated method will be run after all tests in this suite have run. 
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. 
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. 
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. 
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. 
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked. 
@AfterClass: The annotated method will be run after all the test methods in the current class have been run. 
@BeforeMethod: The annotated method will be run before each test method. 
@AfterMethod: The annotated method will be run after each test method.

答案 1 :(得分:0)

这是一个非常简单的继承相关问题。当您在 LoginTest 类中扩展 testcore 类时,父类中可用的所有方法和数据成员都将可用于子类,包括用 @Before Method 等注释的方法。

我认为您因为缺少有关 TestNG 运行程序方式的概念而感到困惑。有多种不同的方式来运行带有 testNG 注释的程序来执行,其中 XML 是一种方式。所以不要混淆所有的类,方法都需要包含在那个xml中。您只需要一个入口点,rest 会相应地调用。