Selenium PageObjects模式和组件

Gra*_*vor 5 c# selenium pageobjects selenium-webdriver

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

为了举例,我可以说我正在为亚马逊产品页面上的功能编写测试.

该页面包含大量单独的功能,产品信息,浏览此客户,其他客户建议等.

我在PageObjects中看到的当前示例实际上仅涵盖了如何处理功能有限的单个页面.我正在寻找的东西是一个PageObject,它代表Product页面,然后由代表每个组件的ComponentObject组成.

例如:

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; 
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

我之前使用过这种方法,但是在Selenium之上构建了一个自定义框架,允许解析子对象及其组件.我正在寻找的是采用Selenium 2和C#开箱即用的方法.

小智 0

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