【问题标题】:Selenium PageObjects pattern and componentsSelenium PageObjects 模式和组件
【发布时间】:2012-09-30 16:14:07
【问题描述】:

将 PageObjects 模式应用于页面组件时的标准方法是什么?

举个例子,假设我正在为亚马逊产品页面上的功能编写测试。

该页面包含大量单独的功能、产品信息、查看过此内容的客户、建议的其他客户等。

我目前看到的 PageObjects 示例实际上只涵盖了如何处理功能有限的单个页面。我正在寻找的是类似于 PageObject 的东西,它代表产品页面,然后由代表每个组件的 ComponentObjects 组成。

例如:

public class ProductPage
{
    [FindsBy(How = How.Id, Using = "productInformation")]
    public ProductInformationControl ProductionInformation{get;set}

    [FindsBy(How = How.Id, Using = "customersViewed")]
    public CustomersAlsoViewedControl CustomersAlsoViewed{get;set}

    [FindsBy(How = How.Id, Using = "customersSuggested")]
    public CustomersSuggestedControl CustomersSuggested{get;set}
}

public class ProductInformationControl
{
    //Ideally the FindsBy here would find the element based on the Controls context
    // So only resolving to an element that was within the context of this control.
    [FindsBy(How = How.ClassName, Using = "title")] 
    private IWebElement _title;

    public string Title
    {
        get
       { 
           return _title.Text; 
       }
    }
}

然后在测试中我会像这样访问控件:

 var productPage = ScenarioContext.Current.Get<ProductPage>();
 Assert.That(productPage.ProductInformation.GetTitle(), Is.EqualTo("My Title"));

我以前使用过这种方法,但使用的是基于 Selenium 构建的自定义框架,允许解析子对象及其组件。我正在寻找的是开箱即​​用的 Selenium 2 和 C# 方法。

【问题讨论】:

  • 希望这有帮助:我正在使用下一个层次结构 1. BasePage-> anyOtherPage(如果页面包含一个部分,它将实现一个函数来设置该部分的焦点并调用,然后您可以继续使用该部分的功能。或在页面中保留该部分的成员并直接使用它的功能,但是您必须在页面类中实现相同的功能) 2. baseSection->anyOtherSection 实现特定部分的所有功能(点击, sendKeys, 验证...)

标签: c# selenium selenium-webdriver pageobjects


【解决方案1】:

没有现成的方法可以做到这一点。 你的方法很好, 您可以通过使用 IElementLocator 和 IPageObjectMemberDecorator 的自定义实现调用 PageFactory.InitElements() 来实现它 看到这个答案: How to initialize SelectElements while using PageFactory / FindsBy in Selenium C#?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 2013-10-17
    相关资源
    最近更新 更多