【问题标题】:How did TestNg annotation mentioned in one class get executed from another from another class?一个类中提到的 TestNg 注释是如何从另一个类的另一个类中执行的?
【发布时间】: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 类,所以BeforeMethodAfterMethod 都可用于LoginTest 类。因此,当您运行 Test 方法时,BeforeMethodAfterMethod 也会被执行。希望这会有所帮助。

标签: selenium selenium-webdriver testng


【解决方案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.

【讨论】:

  • 感谢安库尔的回复。但我正在寻找不同的答案......我直接运行了 LoginTest(右键单击+运行),而不是通过 xml。那么 LoginTest 是如何从 Testcore 类中获取 @beforeMethod 的呢?
  • @PraveenKumar 是的,LoginTest 类正在扩展 testcore 类,显然当您运行 LoginTest 类时,它也会执行 before 和 after 方法。希望这会有所帮助,您可以将其标记为已回答。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 2012-05-18
    • 2014-04-03
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多