简体   繁体   中英

Re-using login functionality in Selenium page object model tests

I am exploring the use of Selenium 2 on a web application which requires authentication before a user can use any of the application. I am planning on either JUnit 4 or TestNG (Still investigating which one to use with Grid 2). I may plan to use jbehave as well.

Does anyone have any suggestions on how I can improve the following test so I can use successful login functionality across all my tests? I want to avoid duplicating login in the tests itself.

public class LoginPageTest {

    private LoginPage page;

    @Before
    public void openTheBrowser() {
        page = PageFactory.initElements(new FirefoxDriver(), LoginPage.class);
        page.open("http://www.site.com/Login");
    }

    @After
    public void closeTheBrowser() {
        page.close();
    }

    @Test
    public void whenTheUserEntersValidCredentialsTheUserIsLoggedIn() {
        assertThat(page.getTitle(), containsString("Login") );
    }   
}

The test is simplified but it will return a page object of a successful login.

thanks

check case study @ http://blog.infostretch.com/?p=806 for better idea. If you are at initial level of development i would suggest you to try using QAF (formerly ISFW) .

Your best option might be to use the LoginPageTest class as a parent class and extend each of your test classes from LoginPageTest.

That way, each test can login to the system using the parent's setup and tear down methods and do its own additional testing as well.

Create Libraries and call the sequence of test cases to execute one test case/scenario.
Eg:

lib.login();
lib.whenTheUserEntersValidCredentialsTheUserIsLoggedIn();
lib.logout();

for doing this take care of object creations. solution for object is use of super eg: super.login()

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