简体   繁体   English

我应该为具有页面对象模式的每个页面使用不同的类吗?

[英]Should i use different class for every page with Page Object Pattern?

I want to make automation for my email address (gmail) and i implemented Page Object Pattern using this tutorial. 我想使我的电子邮件地址(gmail)自动化,并使用教程实现了Page Object Pattern

So i wonder if i need to create different class for every page for example for Login page , Inbox page etc. 所以我想知道是否需要为每个page创建不同的class ,例如Login pageInbox page等。

Or maybe its better to use one class GmailMainPage and from this class implement all the actions that user can do ? 或者,也许它能够更好地使用一个class GmailMainPage从这个class实现所有的操作用户可以做什么?

It is better to create one class per page, which also happens to be how the pattern is normally implemented. 最好每页创建一个类,这也恰好是通常实现该模式的方式。

If you use only one class, it will become very big very quickly. 如果只使用一个类,它将很快变得非常大。 You will also have methods on different pages with similar names which will get confusing and force you to suffix all methods on the single page object with the "real" page name (ie: SaveUserPreferences, SaveEmailForwarding preferences, etc.). 您还将在具有相似名称的不同页面上使用方法,这将使您感到困惑,并迫使您在单个页面对象上使用“真实”页面名称(例如:SaveUserPreferences,SaveEmailForwarding首选项等)作为后缀。

I create one class per page unless: 我每页创建一个类,除非:

  1. The page is really big and I can split it into distinct logical units. 该页面非常大,我可以将其分成不同的逻辑单元。
  2. One part of the page is repeated on many pages and will be reused across the test suite. 页面的一部分在许多页面上重复,并将在测试套件中重复使用。

But as mentioned above, there is not necessarily a one-to-one mapping between a complete HTML page and a page object. 但是如上所述,完整的HTML页面和页面对象之间不一定存在一对一的映射。 A page object should represent a meaningful contextual part of the page. 页面对象应代表页面的有意义的上下文部分。

Really you should almost not think about page objects as specific pages, but shared functionality for a specific part of your application. 确实,您几乎不应该将页面对象视为特定页面,而应该将应用程序特定部分的共享功能考虑在内。 The goal is to have the least amount of reused code as possible. 目标是使重用代码量最少。 One of the ways you can do that is by turning each page into its own class. 做到这一点的方法之一是将每个页面变成自己的类。 That may work for normal websites, but things like gmail you can further break down features into specific classes. 这可能适用于普通网站,但是像gmail这样的东西可以进一步将功能分解为特定的类。

For instance, Starred/Important/Sent Mail all those pages share a lot of the same functionality. 例如,加星标/重要/已发送邮件所有这些页面都共享许多相同的功能。 If you go by one page per class rule then each of those would have its own class. 如果您按照每个班级规则一页浏览,则每个班级都有自己的班级。 However, in reality those three pages share almost everything except the content in the list of emails. 但是,实际上,这三个页面几乎共享除电子邮件列表中的内容以外的所有内容。 So you don't end up coding everything 3 times over, the better option is instead extend something other then pageobject. 因此,您不必最终对所有内容进行3次编码,更好的选择是扩展除pageobject之外的其他内容。 You could have an emailListObject that has all the functions of those three pages, then emailListObject could extend PageObject. 您可以拥有一个具有这三个页面的所有功能的emailListObject,然后emailListObject可以扩展PageObject。 The only difference in each of those three pages may only be which of them gets highlighted in red when clicked. 这三页中每页的唯一区别可能只是单击它们时哪个会以红色突出显示。

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

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