如何实现WebPage模式并在保存类测试中添加接口

时间:2017-05-25 08:36:42

标签: c# selenium

请参阅此结构(来自here):

will-change: transform;

我想添加一些操作的界面

public abstract class AbstractPage<T> where T : AbstractPageEmenetsMap, new()
{
    protected readonly string url;
    protected VendorInfo vendorInfo;

    public AbstractPage(VendorInfo vendorInfo)
    {
        this.vendorInfo = vendorInfo;
        this.url = this.vendorInfo.Url;
    }

    public void Navigate()
    {
        WebDriver.Driver.Navigate().GoToUrl(this.url);
    }

    protected T Map
    {
        get { return new T(); }
    }        
}

public abstract class AbstractPage<M, V> : AbstractPage<M>, ITest

    where M : AbstractPageEmenetsMap, new()
    where V : AbstractPageValidator<M>, new()
{
    public AbstractPage(VendorInfo vendorInfo)
        : base(vendorInfo) { }

    public V Validate()
    {
        return new V();
    }

    public void Login();
    {
        throw new System.NotImplementedException();
    }

    public void Logout();
    {
        throw new System.NotImplementedException();
    }
}

现在这是public interface ITest { void Login(); void Logout(); } 类:

Son

包含所有元素的类:

public class GmailPage : AbstractPage<GmailPageElementsMap, GmailPageValidator>, ITest
{
    public GmailPage() : base("http:...") { }
}

验证员:

public IWebElement EmailAddressTextBox
{
    get
    {
        return WebDriver.WebDriverWait.Until(ExpectedConditions.ElementIsVisible(By.Id("identifierId")));
    }
}

正如你所看到我从我的public class GmailPageValidator : AbstractPageValidator<GmailPageElementsMap> { } 类实现ITest但我没有收到任何编译错误,虽然我没有添加这2个接口方法(登录和注销)。

1 个答案:

答案 0 :(得分:0)

这是因为这些方法是在父AbstractPage中实现的。如果您想强制GmailPage(和所有其他派生类)实施Login()Logout()在父abstract类AbstractPage >

public abstract class AbstractPage<M, V> : AbstractPage<M>, ITest
    where M : AbstractPageEmenetsMap, new()
    where V : AbstractPageValidator<M>, new()
{
    public AbstractPage(VendorInfo vendorInfo) : base(vendorInfo) { }

    public V Validate()
    {
        return new V();
    }

    public abstract void Login();

    public abstract void Logout();
}

并在GmailPage

中覆盖
public class GmailPage : AbstractPage<GmailPageElementsMap, GmailPageValidator>
{
    public GmailPage() : base("http:...") { }
    public override void Login()
    {
        throw new System.NotImplementedException();
    }

    public override void Logout()
    {
        throw new System.NotImplementedException();
    }
}