selenium - POM 设计模式中的 Page 类中应该包含哪些方法?

标签 selenium pageobjects

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

让我们举一个由用户名、密码文本框和提交按钮组成的登录页面对象的简单示例。 SeleniumHQ 链接创建了以下方法:

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

在查看这些方法时我有点困惑。当我已经创建了一个 loginAs 方法时,为什么要创建前 3 个方法(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 - POM 设计模式中的 Page 类中应该包含哪些方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60294851/

相关文章:

java - 在 Selenium Web 驱动程序中使用 Parent::* 轴时出现问题

ruby - 如何获取 page_object 元素的 html 源?

java - 从下到上迭代元素列表的最佳方法是什么?

Python:导入错误:没有名为 selenium 的模块

java - 截取整个页面的屏幕截图

selenium - 使用 Screenplay 模式而不是 Page 对象的优点/缺点是什么?

java - 为什么我应该使我的页面对象实例化而不是静态的?

java - 如何确定 WebElement 是否存在于 Selenium 中?

asp.net - 将异常信息从 Global.asax 传递到 ErrorPage.aspx

java - 在chrome不弹出窗口的情况下更改下载目录