简体   繁体   中英

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

NEW USER ALERT

Using Java, webdriver and TestNG

I have 2 separate methods 1. For initiating driver 2. Login

I have called driver initiating method in @BeforeTest and this will also launch the application login page. 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). 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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