gpt4 book ai didi

java - Selenium 页面对象模式: get objects list

转载 作者:太空宇宙 更新时间:2023-11-04 15:07:13 25 4
gpt4 key购买 nike

我有一个应用程序页面,它提供某种搜索并将搜索结果项列表返回给用户。因此,最初我的测试登录到应用程序,并在主页上开始搜索,如下所示:

HomePage homePage = loginPage.login();     
homePage.searchFor(items);

测试搜索结果是否包含设计方面预期的列表项的最佳实践方法是什么?

  1. 我可以向 Page 对象添加返回结果项的方法,然后我可以在测试类上对它们执行任何我想要的操作。

    public void someTest(List<Items> expectedResultItems) {
    ...
    HomePage homePage = loginPage.login();
    homePage.searchFor(items);
    List<Item> resultItems = homePage.getItems();
    Util.compareItems(resultItems, expectedResultItems);
    }
  2. 我可以向页面对象本身添加比较方法。

    public void someTest(List<Items> expectedResultItems) {   

    ...
    HomePage homePage = loginPage.login();
    homePage.searchFor(items);
    homePage.compareItems(List<Item expectedItems);
    }

最佳答案

herehere

There is a lot of flexibility in how the page objects may be designed, but there are a few basic rules for getting the desired maintainability of your test code. Page objects themselves should never be make verifications or assertions. This is part of your test and should always be within the test’s code, never in an page object. The page object will contain the representation of the page, and the services the page provides via methods but no code related to what is being tested should be within the page object.

因此,根据此指南,选择第一个指南。将断言放在测试中而不是隐藏在页面对象中的某个位置,可以使您的测试更容易阅读和理解它正在做什么。

话虽这么说,我个人倾向于使用第二种方法,这主要是因为我一开始就是这样,现在我已经习惯了。所以我基本上只是在 pageObject 中有一个类似于 public void validateSearchResults(List<?> expected) ... 的公共(public)方法我单独调用它。我的意思是我不会编写像 getSearchDataAndValidate(List<?> expected) 这样的链式方法。这也会调用 validate 方法。因此,我可以单独使用它们,并且如果我在另一个测试中需要它,我可以重复使用搜索方法,而无需进行实际验证。您可能会问为什么不每次都验证您的列表,但如果您的测试套件变得更大,它可能会在性能方面适得其反。

关于java - Selenium 页面对象模式: get objects list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21829517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com