Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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).

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()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top