在测试代​​码中使用面向对象的原理

Editor’s Note: Writing test code is an important (if annoying) part of developing software that is reliable and maintainable over the long term. In her piece from 97 Things Every Java Programmer Should Know, Java Champion and test automation expert Angie Jones shares why you should apply the same OO principles to test code as you would to production code.

编者注:编写测试代码是开发软件的重要组成部分(如果令人讨厌),该软件长期可靠且可维护。 Java冠军和测试自动化专家Angie Jones的《每个Java程序员应该知道的97件事》一文中,分享了为什么您应该对生产代码应用相同的OO原理来测试代码。

When writing test code, it’s important to exercise the same care that you’d use when developing production code. Here are common ways to use object-oriented (OO) principles when implementing test code.

编写测试代码时,请务必与开发生产代码时所用的相同。 这是在实现测试代码时使用面向对象(OO)原理的常用方法。

封装形式 (Encapsulation)

The Page Object Model design pattern is commonly used in test automation. This pattern prescribes creating a class to interact with a page of the application under test. Within this class are the locator objects for the elements of the web page and the methods to interact with those elements.

页面对象模型设计模式通常用于测试自动化中。 此模式规定创建一个类以与被测应用程序的页面进行交互。 在此类中,是网页元素的定位器对象以及与这些元素进行交互的方法。

It’s best to properly encapsulate by restricting access to the locators themselves and only exposing their corresponding methods:

最好通过限制对定位器本身的访问并仅公开其对应的方法来正确封装:

public class SearchPage {
private WebDriver driver;
private By searchButton =
By.id(“searchButton”);
private By queryField = By.id(“query”); public SearchPage(WebDriver driver){
this.driver = driver;
} public void search(String query) {driver.findElement(queryField).sendKeys(query);
driver.findElement(searchButton).click();
} }

遗产 (Inheritance)

While inheritance should not be abused, it can certainly be useful in test code. For example, given there are header and footer components that exist on every page, it’s redundant to create fields and methods for interacting with these components within every Page Object class. Instead, create a base Page class containing the common members that exist on every page, and have your Page Object classes inherit from this class. Your test code will now have access to anything in the header and footer no matter what Page Object they are currently interacting with.

尽管不应该滥用继承,但是继承肯定可以在测试代码中有用。 例如,假设每个页面上都存在页眉和页脚组件,那么在每个Page Object类中创建用于与这些组件进行交互的字段和方法是多余的。 而是,创建一个包含每个页面上存在的公共成员的基本Page类,并使您的Page Object类从该类继承。 现在,您的测试代码将可以访问页眉和页脚中的任何内容,无论它们当前正在与哪个Page Object进行交互。

Another good use case for inheritance within test code is when a given page has various implementations. For example, your app may contain a User Profile page that has different functionality based on roles (e.g., Administrator, Member). While there are differences, there could also be overlap. Duplicating code across two classes is not ideal. Instead, create a ProfilePage class that contains the common elements/interactions, and create subclasses (e.g., AdminProfilePage, MemberProfilePage) that implement the unique interactions and inherit the common ones.

测试代码中继承的另一个好用例是给定页面具有各种实现时。 例如,您的应用程序可能包含一个用户配置文件页面,该页面具有基于角色(例如,管理员,成员)的不同功能。 尽管存在差异,但也可能存在重叠。 在两个类之间复制代码并不理想。 相反,创建一个包含公共元素/交互的ProfilePage类,并创建实现唯一交互并继承公共交互的子类(例如AdminProfilePageMemberProfilePage )。

多态性 (Polymorphism)

Assume we have a convenience method that goes to the User Profile page. This method doesn’t know what type of profile page it is — an Administrator or a Member.

假设我们有一个方便的方法可以转到“用户配置文件”页面。 此方法不知道它是什么类型的配置文件页面-管理员还是成员。

You’re faced with a design decision here. Do you make two methods — one for each of the profile types? This seems like overkill since they both would do the exact same thing but just have a different return type.

您在这里面临设计决策。 您是否采用两种方法-一种针对每种配置文件类型? 这似乎有点过头了,因为他们俩都会做完全相同的事情,但是返回类型不同。

Instead, return the superclass (ProfilePage) since both AdminProfilePage and MemberProfilePage are both subclasses of ProfilePage. The test method that is calling this convenience method has more context and can cast accordingly:

而是返回超类( ProfilePage ),因为AdminProfilePageMemberProfilePage都是ProfilePage子类。 调用此便捷方法的测试方法具有更多上下文,并且可以相应地强制转换:

@Test
public void badge_exists_on_admin_profile() {
var adminProfile =
(AdminProfilePage)page.goToProfile("@admin");
...}

抽象化 (Abstraction)

Abstraction is used sparingly in test code, but there are valid use cases. Consider a type of widget that has been customized for different usages throughout the app. Creating an abstract class that specifies the behaviors expected is helpful when developing classes that interact with specific implementations of that widget:

在测试代​​码中很少使用抽象,但是存在有效的用例。 考虑一种针对整个应用程序的不同用途而定制的小部件类型。 在开发与该小部件的特定实现进行交互的类时,创建一个指定预期行为的抽象类将很有帮助:

public abstract class ListWidget {
protected abstract List<WebElement> getItems();
int getNumberOfItems() {
return getItems().size();
}}public class ProductList extends ListWidget {
private By productLocator =
By.cssSelector(".product-item");
@Override
protected List<WebElement> getItems() {
return driver.findElements(productLocator);
}}

Test code is indeed code, meaning that it has to be maintained, enhanced, and scaled. Therefore, it’s in your best interest to follow good programming practices when developing it — including the foundational OO principles.

测试代码确实是代码,这意味着必须对其进行维护,增强和扩展。 因此,在开发程序时遵循良好的编程习惯(包括基本的OO原则)是您的最大利益。

学习更快。 深入挖掘。 看得更远。 (Learn faster. Dig deeper. See farther.)

Join the O’Reilly online learning platform. Get a free trial today and find answers on the fly, or master something new and useful.

加入O'Reilly在线学习平台。 立即获得免费试用版,即时找到答案,或者掌握一些新的有用的知识。

Learn more

学到更多

Angie Jones is a senior developer advocate who specializes in test automation strategies and techniques. She shares her wealth of knowledge by speaking and teaching at software conferences all over the world, writing tutorials and technical articles on angiejones.tech, and leading the online learning platform, Test Automation University. As a Master Inventor, Angie is known for her innovative and out-of-the-box thinking style, which has resulted in more than 25 patented inventions in the US and China. In her spare time, Angie volunteers with Black Girls Code to teach coding workshops to young girls in an effort to attract more women and minorities to tech.

Angie Jones是一位高级开发人员拥护者,专门研究测试自动化策略和技术。 她通过在世界各地的软件会议上演讲和教学,在angiejones.tech上撰写教程和技术文章以及领导在线学习平台Test Automation University来分享自己的知识。 作为首席发明家,安吉以其创新和开创性的思维风格而闻名,该风格在美国和中国产生了25多项专利发明。 在业余时间,安吉(Angie)用“黑人女孩代码”(Black Girls Code)志愿为年轻女孩教授编码讲习班,以吸引更多的女性和少数民族学习技术。

翻译自: https://medium.com/oreillymedia/using-object-oriented-principles-in-test-code-48ec3f228f37

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值