我为什么要使我的页面对象实例化而不是静态? [英] Why should I be making my page objects instantiated rather than static?

查看:111
本文介绍了我为什么要使我的页面对象实例化而不是静态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名相对较新的QA工程师,致力于学习Selenium(Java),我想使用页面对象来模拟我的页面。

I'm a relatively new QA Engineer working on learning Selenium (in Java) and I want to use page objects to model my pages.

目前,我正在这样做,我的页面对象类是静态变量的集合(通过定位页面元素的对象)和静态方法(用于获取By对象和执行页面功能)。这对我来说似乎是最简单的方法,因为我的方法不需要依赖任何实例变量,只需要依赖定位器。

Currently, the way I'm doing it, my page object classes are collections of static variables (By objects for locating page elements) and static methods (for getting the By objects and performing page functions). This seemed like the simplest way to me as my methods don't need to rely on any instance variables, just the locators.

我只是在我的测试代码中调用这些方法。

I just call these methods as I need them in my test code.

然而,我读到的关于页面对象的所有内容都涉及实例化它们并让方法返回页面对象。这似乎使一切变得更加复杂。例如,我没有一个登录方法,而是需要两个,一个用于登录成功,一个用于登录失败。

However, everything I read about page objects talks about instantiating them and having methods return page objects. This seems like it makes everything more complicated. For example, instead of having one method for logging in, I need two, one for if the login succeeds and one for if it fails.

我知道这似乎是公认的最佳做法,但我想了解原因。谢谢。

I know it seems to be the accepted best practice, but I want to understand why. Thanks.

这是我的pageObject代码,我的测试调用方法为 LoginPage.login(用户名,密码);

Here is my pageObject code, my tests call methods as LoginPage.login(username, password);

package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


public class LogInPage {
    private static By emailTxtB = By.id("user_email");
    private static By passwordTxtB = By.id("user_password");
    private static By logInButton = 
                                 By.xpath("/html/body/div/div[2]/form/div[2]/div[2]/div/button"); 
    private static By signUpButton = By.xpath("/html/body/div/div[2]/form/div[2]/div[2]/div/a");
    private static By valErrorMessage = By.id("flash_alert");

    public static void logIn(WebDriver driver, String email, String password){      
        //Fill out form
        driver.findElement(emailTxtB).sendKeys(email);
        driver.findElement(passwordTxtB).sendKeys(password);

       //Submit form
       driver.findElement(logInButton).click();
    }


    public static void goToSignUp(WebDriver driver){
        driver.findElement(signUpButton).click();
    }

    public static String getValErrorMessage(WebDriver driver){
        return driver.findElement(valErrorMessage).getText();
    }

    public By getEmailTxtB(){
        return emailTxtB;
    }

    public By getPasswordTxtB(){
        return passwordTxtB;
    }

    public By getLogInButton(){
        return logInButton;
    }

    public By getSignUpButton(){
        return signUpButton;
    }
}


推荐答案

我2个月前开始使用Selenium和Page Objects。而且我也很好奇这个话题。大约一个月前我决定使用类和静态方法。但随着代码库的成长,我开始考虑切换到对象实例。
主要原因 - 对象可以有状态。在对象中,我可以检查我是否真的使用正确的页面。对于类,我只能假设当前的html与类匹配(或者在页面对象的每个静态方法中使用断言使我的代码膨胀)。
另一个原因 - 自动完成。静态方法鼓励测试人员使用类,而不是类变量。因此,找到正确的类来调用方法变得越来越难。对于对象,如果要调用任何方法,则必须声明变量。所以你可以打电话给你。使用类和静态方法,您可以随时调用任何方法(并且测试失败导致预期的html不可用,例如)。当您尝试教您的团队成员使用您的代码时,它开始成为一个问题。只要你是唯一的测试作者,它就可以了。

I started to work with Selenium and Page Objects 2 months ago. And I'm also curious about the topic. I decided to go with classes and static methods about 1 month ago. But as a codebase grew up I'm started to think about switching to object instances. Main reason - objects can have state. In object I can check if I'm really working with the correct page. With classes I can only assume that current html matches the class (or bloat my code with asserts in every static method of page object). The other reason - autocomplete. Static methods encourage testers to use classes, not the class variables. So it's getting harder and harder to find correct class to call method from. With objects you'll have to declare a variable if you want to call any method. So you're limited with what you can call. With classes and static methods, you can call any method any time (and fail the test cause the expected html is not available eg). It starts to grow into a problem when you try to teach your team members to use your code. As long as you are the only test writer, it might be ok.

这篇关于我为什么要使我的页面对象实例化而不是静态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆