如何在 PageObjects 模式中使用 WebDriver / Selenium 2 LoadComponents?

发布于 2024-11-13 07:19:27 字数 1818 浏览 2 评论 0原文

我很难适应 WebDriver PageObject 模式。请分享您使用PageObjects模式和loadableComponents的经验和方法。

由于 PageObject 通常代表一个组件或一项功能,因此我首先想到应该使用它在 load() 中实际执行一些操作,并查看它是否执行 isLoaded() 中应执行的操作。

然后我意识到它应该只用于“加载”对象(可能是初始化),因为在网站上移动时,每个对象都有自己的 url。并使用 isLoaded() 来测试对象是否已准备好进行测试。

但是,如果你有一个复杂的 JavaScript 订单提交器要测试,即 JS 文件上传器、基于 2 个独立部分的 JS 表单的组合,并且存在三种订单,那么你不需要移动任何地方(关于 URL),只需移动元素的状态正在发生变化。

考虑 get() 方法。您进入带有交互式表单的页面。当页面上存在表单时加载它。然后你有 form1 和 form2 对象......它们的 load() 和 isLoaded() 方法应该是什么样子,它们立即准备好采取行动,因为它们不需要任何加载,只需测试它们的服务。

这是一团糟,不知道 isLoaded() 方法是否用于检查对象是否已加载,或者对象是否已加载并已正确设置。但我认为前一种方式是正确的,并且应该在测试中保证设置的有效性。

场景:

Testing first part of html form - test that field client side validation works
Testing the second one that depends on the first one
Testing the following file uploader - upload, canceling, clearing, order, fileIDs
Testing the overall html form submission - ServerSide validation errors, results

文档说:

  • LoadableComponent
  • PageObjects

    1. 公共方法代表页面提供的服务

      验证、上传、上传多个、取消、清除

    2. 尽量不要暴露页面的内部

      我唯一想到的是将 Driver 实例隐藏到 UnitTests 中,并使用 Only PageObjects 来保留 PageObjects 中的所有字段名称、CSS 类名称 + 为 PageObjects 提供输入数据并断言服务/功能的布尔结果

    3. 方法返回其他 PageObjects

      这是最难适应的事情。为一种交互式表单提供 4 个页面对象感觉有点不自然。他们推荐 Form1、Form2(Form1)、Upload(Form2)、Submit(Upload),尽管我发现链接和移交对前面对象的引用非常混乱。在测试方法中对所有这些调用 get() 似乎更好。但我猜其背后的想法不是将 Driver 实例暴露给测试,而是仅使用内部使用 Driver 实例的 PageObjects

    4. 同一操作的不同结果被建模为不同的方法 我认为这意味着不应该在页面对象端决定该操作的有效性,而应该在测试端决定

I have a hard time to get used to the WebDriver PageObject pattern. Please share your experience and ways of using PageObjects pattern and loadableComponents.

As PageObject represents usually a component or piece of functionality, it occurred to me at first that I should use it to actually do some things in load() and see if it does what it should in isLoaded().

Then I realized that it should be utilized only for "loading" objects (possibly initialization) as it is in case of moving around a website, each object with its own url. And using isLoaded() to test whether the object IS READY for testing.

But if you have a complex JavaScript order submitter to test, that is compound of JS file uploader, JS form that is based on 2 independent parts and there are three kinds of Orders, you don't move anywhere (regarding URL), just the state of elements is changing.

Consider the get() method. You get into the page with the interactive Form. It is loaded when the form exist on the page. Then you have form1 and form2 objects ... what should their load() and isLoaded() method look like, they are ready for action right away because they don't need any loading, just testing their services.

It's a mess, one doesn't know if the isLoaded() method is used for checking whether object loaded, or whether object loaded and was setup properly. But I guess the former way is correct and that validity of setting it up should be ensured within the tests.

Scenario:

Testing first part of html form - test that field client side validation works
Testing the second one that depends on the first one
Testing the following file uploader - upload, canceling, clearing, order, fileIDs
Testing the overall html form submission - ServerSide validation errors, results

The documentation says :

  • LoadableComponent
  • PageObjects

    1. The public methods represent the services that the page offers

      Validate, upload, upload Multiple, cancel, clear

    2. Try not to expose the internals of the page

      The only thing that occurs to me is having Driver instance hidden to UnitTests and use Only PageObjects to keep all field names, css class names in PageObjects + supply PageObjects with input data and assert boolean result of the services/functionality

    3. Methods return other PageObjects

      This is the most difficult thing to get used to. Having 4 page objects for one interactive form kinda doesn't feel natural. They recommend Form1, Form2(Form1), Upload(Form2), Submit(Upload), although I find the chaining and handing over a reference to the preceding object very chaotic. Calling get() on all of them in a test method seems better. But I guess that the idea behind it is not to expose the Driver instance to the Tests, but use only PageObjects that are using Driver instance internally

    4. Different results for the same action are modelled as different methods
      I suppose that this means that it shouldn't be decided about validity of that action on the Page object side, but on the Test side

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情何以堪。 2024-11-20 07:19:27

在编写页面对象时,我有一些不同的方法。翻译成你的条款load()方法以条件等待结束,确保页面包含你想要的内容。然后我不需要任何 isLoaded() - 要么我在那里,要么抛出异常。

页面中的组件是包含它们的页面的属性,我在组件和页面之间设置了双向关系。它们不是功能齐全的页面对象。也许你的表单也可以这样建模。

I have a bit different approach when writing page objects. Translated to your terms load() method ends with conditional wait ensuring the page contains what you desire. Then I do not need any isLoaded() - either I'm there or Exception is thrown.

Components in page are properties of the page containing them and I set two-way relationship between a component and the page. They are not fully functional page objects. Maybe your forms can be modeled this way too.

够运 2024-11-20 07:19:27

可加载组件是页面对象模式的扩展。 WebDriver 库中的 LoadableComponent 类将帮助测试用例开发人员确保页面或页面的组件已成功加载。它极大地减少了调试测试用例的工作量。页面对象应该扩展此 Loadable Component 抽象类,因此,它必然会提供以下两个方法的实现:

protected abstract void load()

protected abstract void isLoaded() throws java.lang.Error

必须在load()和isLoaded()方法中加载的页面或组件决定页面是否完全加载。如果未完全加载,则会抛出错误。

https://code.google.com/p/selenium/wiki/LoadableComponent

The loadable component is an extension to the Page Object pattern. The LoadableComponent class in the WebDriver library will help test case developers make sure that the page or a component of teh page is loaded successfully. It tremendously reduces the efforts to debug your test cases. The page object should extend this Loadable Component abstract class and as a result, it is bound to provide implementation for the following two methods:

protected abstract void load()

protected abstract void isLoaded() throws java.lang.Error

The page or component that has to be loaded in the load() and isLoaded() methods determines whether or not the page is fully loaded. If it is not fully loaded, it throws an error.

https://code.google.com/p/selenium/wiki/LoadableComponent

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文