首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium PageObjects模式和组件

Selenium PageObjects模式和组件
EN

Stack Overflow用户
提问于 2012-10-10 16:31:46
回答 1查看 1.7K关注 0票数 5

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

为了举例,假设我正在为Amazon产品页面上的功能编写测试。

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

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

例如:

代码语言:javascript
复制
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; 
       }
    }
}

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

代码语言:javascript
复制
 var productPage = ScenarioContext.Current.Get<ProductPage>();
 Assert.That(productPage.ProductInformation.GetTitle(), Is.EqualTo("My Title"));

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

EN

回答 1

Stack Overflow用户

发布于 2016-11-15 17:16:10

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12815145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档