【问题标题】:How TestNG annotation from base class is executed when the Test inside the derived class is executed?执行派生类中的 Test 时,如何执行来自基类的 TestNG 注解?
【发布时间】: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


    【解决方案1】:

    如果你读到这一行:

    public class LoginTest extends TestBase 
    

    这清楚地表明,LoginTestTestBase 的子类。

    所以TestBase 获得更多优先权。

    现在让我们了解一下@BeforeMethod是什么。

    @BeforeMethod

    The annotated method will be run before each test method.
    

    所以这是默认的Testng 架构,在您的测试套件中的每个@Test 之前运行@BeforeMethod

    你的程序执行应该是这个顺序:-

    @BeforeMethod
    

    然后

    @Test
    

    然后

    @AfterMethod
    

    如果您有多个@Test,则顺序应相同。

    您可以参考here,以上参考取自TestNG官方文档。

    【讨论】:

      【解决方案2】:

      所以@beforeMethod@AfterMethod 将在您的实际测试之前和之后运行,这就是TESTNG 生命周期的工作方式。

      您可以找到更多详情here

      【讨论】:

        猜你喜欢
        • 2013-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-22
        • 2021-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多