【问题标题】:Fluent API how to handle splits to different typesFluent API 如何处理不同类型的拆分
【发布时间】:2018-01-26 22:05:37
【问题描述】:

我正在使用 Fluent API 作为我在项目中进行的 Selenium UI 测试的一部分。 我读这篇文章是为了参考 fluent api Scott Lilly Fluent API。 目的是控制测试流程并使其尽可能具有可读性。
如图所示,我有一个简单的向导。 登录页面指向指向第二页的第一页(到目前为止一切都很简单),但问题从第二页开始,可以将您带回第一页或最后一页。 所以我想知道我应该返回什么来让用户两个选项,但只有那些选项?

我希望它看起来像这样:

DoLogin().WithText().FillForm().GoToSecondPage().FillForm().xxx 

xxx = 在这里我希望有两个选项可以返回第一页或最后一页

到目前为止,它的代码看起来像这样:

public interface IFirstPage
{
    void WithText();
    IGoToSecondPage FillForm();
}

public interface ISecondPage
{
    //Here starts the problem
    //From this page you can go back to the first page or continue to the last page

    Object FillForm();
}

public interface ILastPage
{
    void SetSomething();
    IGoToSecondPage FillForm();
}

public interface IGoToSecondPage
{
    void GoToSecondPage();
}

public interface IGoToFirstPage
{
    void GoToFirstPage();
}

public interface IGoToLastPage
{
    void GoToSecondPage();
}

public class FirstPage : IFirstPage
{
    public void WithText()
    {
    }

    public IGoToSecondPage FillForm()
    {
        throw new System.NotImplementedException();
    }
}

public class SecondPage : ISecondPage
{
    public object FillForm()
    {
        throw new NotImplementedException();
    }
}

Wizard pages flow

【问题讨论】:

  • FirstPage 和 SecondPage 是否有任何关联?可以创建另一个接口ILoggedInPage,它包含FirstPage 和SecondPage 使用的常用方法(或者它可以是一个空的占位符)。然后让FirstPage 和SecondPage 继承自ILoggedInPage,这样两个页面就有一个共同的祖先,你的返回类型可以是ILoggedInPage。

标签: c# selenium testing fluent method-chaining


【解决方案1】:

在尝试了不同的方法后,我发现下一个是最通用和最准确的,除了第一页和最后一页没有上一页或下一页。

每个页面构建器接口都实现了 IFillForm,它作为参数 T 获取您可以从当前页面来回移动的页面接口。

public interface IFillForm<T>
{
    T FillForm();
}

public interface ICanGoToThesePages<TPreviousPage, TNextPage>
{
    TPreviousPage ClickForPreviousPage();
    TNextPage ClickForNextPage();
}

public interface IFirstPage : IFillForm<ICanGoToThesePages<NullReferenceException, ISecondPage>>
{
    void WithText();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多