简体   繁体   English

在Selenium页面对象模型测试中重用登录功能

[英]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. 我正在探索在Web应用程序上使用Selenium 2,这需要在用户可以使用任何应用程序之前进行身份验证。 I am planning on either JUnit 4 or TestNG (Still investigating which one to use with Grid 2). 我正在计划JUnit 4或TestNG(仍在调查哪一个用于Grid 2)。 I may plan to use jbehave as well. 我也可以计划使用jbehave。

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. 查看案例研究@ http://blog.infostretch.com/?p=806以获得更好的想法。 If you are at initial level of development i would suggest you to try using QAF (formerly ISFW) . 如果您处于初始开发水平,我建议您尝试使用QAF(以前的ISFW)

Your best option might be to use the LoginPageTest class as a parent class and extend each of your test classes from LoginPageTest. 您最好的选择可能是将LoginPageTest类用作父类,并从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() 对象的解决方案是使用super eg: super.login()

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

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