简体   繁体   English

Selenium Webdriver:需要解决与@BeforeTest (TestNG) 有关的非常具体的问题

[英]Selenium Webdriver: Need solution for a very specific issue pertaining to @BeforeTest (TestNG)

NEW USER ALERT新用户提醒

Using Java, webdriver and TestNG使用 Java、webdriver 和 TestNG

I have 2 separate methods 1. For initiating driver 2. Login我有 2 个单独的方法 1. 用于启动驱动程序 2. 登录

I have called driver initiating method in @BeforeTest and this will also launch the application login page.我在@BeforeTest 中调用了驱动程序启动方法,这也将启动应用程序登录页面。 I also want to call the 'login' method in @BeforeTest (since this is needed by almost all of the tests) but problem is that I also have 4-5 tests for this login page (like testing version, copyright, forgot password link).我还想在@BeforeTest 中调用“登录”方法(因为几乎所有测试都需要这样做),但问题是我也有 4-5 个针对此登录页面的测试(例如测试版本、版权、忘记密码链接) )。 So for these tests logging in is not required (or rather login should not happen).因此,对于这些测试,不需要登录(或者不应该登录)。

Is there a way I can execute a set of method calls before tests pertaining to login page AND a different set of method calls before all other tests.有没有一种方法可以在与登录页面有关的测试之前执行一组方法调用,以及在所有其他测试之前执行一组不同的方法调用。 Please let me know if there's an alternate way this can be achieved.请让我知道是否有其他方法可以实现。

Let me know if more information is required here.如果需要更多信息,请告诉我。

I would create a base-class where I put the @BeforeTest method with login and let only the testclasses inherit it that will need a login:我将创建一个基类,在其中放置带有登录名的 @BeforeTest 方法,并且只让需要登录的测试类继承它:

public abstract class TestBaseForLogin{

    @BeforeTest
    public void loginBeforeEachTest() {
        // do the login
    }
}

Now if you wanna pool tests that need the login before each test, then just inherit from the baseclass.现在,如果您想在每次测试之前汇集需要登录的测试,那么只需从基类继承即可。

public class TestThatNeedLogin extends TestBaseForLogin{

    @BeforeTest
    public void beforeEachTest() {
        // do whatever you need before the test
    }   
}

Otherwise just go without the baseclass (or another baseclass)否则就不用基类(或另一个基类)

public class TestLogin {

    @BeforeTest
    public void beforeEachTest() {
        // do whatever you need before the test
    } 

}

Your inherited classes will FIRST execute the @BeforeTest of the base class and only after that their own @BeforeTest tagged methods.您继承的类将首先执行基类的@BeforeTest,然后才执行它们自己的@BeforeTest 标记方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM