哪种方法应该在POM设计模式中的Page类内?

问题描述 投票:0回答:1

我正在使用POM设计模式创建UI测试自动化框架。阅读SeleniumHQ页面对象页面后,我正在考虑应该在页面对象内部创建所有方法。

让我们以登录页面对象为例,它由用户名,密码文本框和提交按钮组成。 SeleniumHQ链接创建了以下方法:

1. typeUsername(String username)
2. typePassword(String password)
3. submitLogin()
4. submitLoginExceptionFailure()
5. loginAs(String username, String password)

在查看这些方法时,我有些困惑。当我已经创建了loginAs方法时,为什么要创建前三个方法(typeUsername,typePassword,submitLogin)。有什么想法吗?

SeleniumHQ链接-https://github.com/SeleniumHQ/selenium/wiki/PageObjects

粘贴LoginPage的代码PageObject代码:

public class LoginPage {
    private final WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;

        // Check that we're on the right page.
        if (!"Login".equals(driver.getTitle())) {
            // Alternatively, we could navigate to the login page, perhaps logging out first
            throw new IllegalStateException("This is not the login page");
        }
    }

    // The login page contains several HTML elements that will be represented as WebElements.
    // The locators for these elements should only be defined once.
        By usernameLocator = By.id("username");
        By passwordLocator = By.id("passwd");
        By loginButtonLocator = By.id("login");

    // The login page allows the user to type their username into the username field
    public LoginPage typeUsername(String username) {
        // This is the only place that "knows" how to enter a username
        driver.findElement(usernameLocator).sendKeys(username);

        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;    
    }

    // The login page allows the user to type their password into the password field
    public LoginPage typePassword(String password) {
        // This is the only place that "knows" how to enter a password
        driver.findElement(passwordLocator).sendKeys(password);

        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;    
    }

    // The login page allows the user to submit the login form
    public HomePage submitLogin() {
        // This is the only place that submits the login form and expects the destination to be the home page.
        // A seperate method should be created for the instance of clicking login whilst expecting a login failure. 
        driver.findElement(loginButtonLocator).submit();

        // Return a new page object representing the destination. Should the login page ever
        // go somewhere else (for example, a legal disclaimer) then changing the method signature
        // for this method will mean that all tests that rely on this behaviour won't compile.
        return new HomePage(driver);    
    }

    // The login page allows the user to submit the login form knowing that an invalid username and / or password were entered
    public LoginPage submitLoginExpectingFailure() {
        // This is the only place that submits the login form and expects the destination to be the login page due to login failure.
        driver.findElement(loginButtonLocator).submit();

        // Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials 
        // expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject.
        return new LoginPage(driver);   
    }

    // Conceptually, the login page offers the user the service of being able to "log into"
    // the application using a user name and password. 
    public HomePage loginAs(String username, String password) {
        // The PageObject methods that enter username, password & submit login have already defined and should not be repeated here.
        typeUsername(username);
        typePassword(password);
        return submitLogin();
    }
}
selenium pageobjects
1个回答
0
投票

您可能想检查是否仅输入用户名,然后单击提交按钮显示正确的错误消息,或仅显示密码等。>

我通常查看页面,并尝试总结用户可以在该页面上执行的“操作”,每个操作都成为一种方法。不同的动作可能在不同的“级别”上。例如在博客网站上,用户可以输入博客标题和博客内容,这是用户可以执行的两个操作,但是从另一个抽象层看,用户希望“创建”帖子。因此该功能可能会再次调用其他功能。

基本上与其他程序一样,您具有多个抽象层,这就是为什么首先拥有页面对象的原因。

并且仅使用迭代开发,创建一个要测试的功能,如果发现自己在其他功能中重复使用相同的代码(或标识符),请在新功能中将其分开

© www.soinside.com 2019 - 2024. All rights reserved.