如何处理WebDriver的PageObjects模式中的继承?
有人可以建议一种处理有关页面对象模式的继承的方法吗?
例如,页面对象需要共享属性和方法。
public class LoginPage extends SuperPage
public class SuperPage extends LoadableComponent<SuperPage>
这样就可以了。但问题是你的UnitTests不扩展SuperPage,它们扩展TestCase,它保存测试的全局属性等。
我尽量避免重复,因为我需要在 TestCase 和 SuperPage 中共享相同的内容...页面和测试只能扩展其中之一。
例如,我需要在可用的 PageObject 和测试中使用相同的表单数据...
示例:
如果您有一个用于填写 html 表单的 PageObject,则需要表单字段的名称,但即使在其他 pageObject 中也需要它们。这样您就可以在字段名称所在的位置扩展 SuperPage。它们不能从 UnitTests 提供,因为例如:
@CacheLookup
@FindBy(how = How.ID, using = namespace + signifLvl)
private WebElement sigLvl;
@CacheLookup
@FindBy(how = How.ID, using = namespace + languageTo)
private WebElement langTo;
@CacheLookup
@FindBy(how = How.ID, using = namespace + languageFrom)
private WebElement langFrom;
@CacheLookup
@FindBy(how = How.ID, using = namespace + description)
private WebElement desc;
但另一方面,您需要在 UnitTest 方法中使用它们,因为您向 PageObjects 提供不同的值。
否则它总是会这样,用 TestCase 中的变量预填充 PageObjects :
@Test
public void doStuff() throws IOException {
driver.navigate().refresh();
FillOutFormPage fofp = new FillOutFormPage(driver);
fofp.fill(some values from TestCase);
fofp.get();
}
Could please anybody suggest a way to deal with Inheritance regarding Page Objects pattern ?
For instance, page objects need to share properties and methods.
public class LoginPage extends SuperPage
public class SuperPage extends LoadableComponent<SuperPage>
This would be Ok. But the problem is that your UnitTests don't extend SuperPage, they extend TestCase, that holds global properties etc. for Tests.
I try to avoid duplication, because I need to share the same stuff in TestCase and SuperPage... Pages and Tests can extend only one of these.
For instance, I need the same Form Data in PageObjects and Tests available...
Example:
If you have a PageObject for filling out a html form, you need names of the form fields, but you need them even in other pageObjects. So that you extend SuperPage where the field names are. They cannot be supplied from UnitTests, because of this for instance :
@CacheLookup
@FindBy(how = How.ID, using = namespace + signifLvl)
private WebElement sigLvl;
@CacheLookup
@FindBy(how = How.ID, using = namespace + languageTo)
private WebElement langTo;
@CacheLookup
@FindBy(how = How.ID, using = namespace + languageFrom)
private WebElement langFrom;
@CacheLookup
@FindBy(how = How.ID, using = namespace + description)
private WebElement desc;
But on the other hand, you need to use these in UnitTest methods, because you supply different values from them to PageObjects.
Otherwise it would always by like this, prepopulating PageObjects with variables from TestCase :
@Test
public void doStuff() throws IOException {
driver.navigate().refresh();
FillOutFormPage fofp = new FillOutFormPage(driver);
fofp.fill(some values from TestCase);
fofp.get();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
页面对象模式的关键方面之一是测试用例和页面结构的隔离。如果您的测试是在字段中输入内容,那么您可能做错了。您的页面应该提供允许测试执行重要操作的方法,而不是让测试填充页面的变量。因此,而不是:
myPage.sigLvl = "10";
myPage.langTo = "en";
myPage.langFrom = "de";
你这样做:
myPage.addTranslation("en", "de", 10);
重点是你的测试不应该关心结构页面的内容(字段的名称、它们在页面上的排列方式等),而不是应用程序的行为。
One of the key aspects of the Page Object pattern is the isolation of test cases and page structure. If your tests are typing into fields, you're probably Doing It Wrong. Instead of having tests fill in the pages' variables, your pages should provide methods that allow tests to perform important actions. Thus instead of:
myPage.sigLvl = "10";
myPage.langTo = "en";
myPage.langFrom = "de";
you do:
myPage.addTranslation("en", "de", 10);
The point is that your test shouldn't care about the structure of the page (what the fields are called, how they're arranged on the page, etc.), but rather about the behavior of the application.
我没有对 WebDriver 做太多事情,但我记得,我创建和测试的大多数 PageObject 都不需要填充。它们只包含一个场景 - 要测试的条件序列,以从 UnitTest 调用的测试方法的形式。 PageObject 之间共享的唯一属性只是几个字段或字段名称(非常特殊 - 就像单击提交按钮时会触发验证 - 它需要从构成整个页面并验证某些内容的所有组件都可访问)并且例如命名空间。
在创建需要填充不同数据的 PageObject 的情况下。它必须从外部进行(单元测试)。所以我想应该用unitTests中的数据填充或者更确切地说构建pageObjects,但前提是它是其他人需要的“类似服务的组件”并且必须处于与初始状态不同的状态。如果您总是这样做,则不需要在 TestCase 和 PageObjects 之间共享此类数据。
PageObjects 和 UnitTests 之间应该有一个“边界”...不同的数据必须从 unitTests 进入 pageObjects 测试方法,有关 html 的静态数据保留在 PageObjects 中...恕我直言,
我不建议使用 SuperPageObejct,因为你拥有它。 SomePageObject.get() 方法返回 SuperObject 而不是 SomePageObject...您在 object1 中需要的内容保留在对象一中。不应该有继承。在 Object1 中,您不需要从 object2 中获取任何内容。
I haven't done much with WebDriver, but I remember, that most of of the PageObjects I created and tested, didn't need to be populated. They just contained a scenario - sequence of conditions to be tested, in form of testing methods to be called from UnitTest. The only property to be shared between PageObjects were just a few fields or field names (very exceptionally - like a submit button that when clicked, validation is triggered - it needs to be reachable from all components that forms the entire page and validate something) and namespace for instance.
In case of creating PageObjects that need to be populated with varying data. It must go from outside (UnitTests). So I guess one should be populating or rather constructing pageObjects with data in unitTests, but only if it is a "service like component" that is needed by others and must be in different that initial state. If you always do it like this, there shouldn't be a need for sharing this kind of data between TestCase and PageObjects.
There should be a "boarder" between PageObjects and UnitTests ...varying data must come from unitTests into pageObjects testing methods, static data regarding html stay in PageObjects... IMHO
I don't recommend having SuperPageObejct because as you have it. SomePageObject.get() method returns SuperObject instead of SomePageObject... What you need in object1, stays in object one. There should be no inheritance. In Object1 there is nothing that you should need from object2.