我正在尝试在WatiN中为H1、H2、H3和UL编写扩展方法,所有这些元素都应该支持Browser、Frame、Form和div。我的方法是创建不同的.cs文件,如FormExtension.cs、FrameExtension.cs和DocumentExtension.cs
在我的DocumentExtension.cs中,我包含了以下代码。
public static H1 H1<TDoc>(this TDoc doc, string text)
where TDoc : Document
{
ElementFactory.RegisterElementType(typeof(H1));
return doc.ElementOfType<H1>(Find.ByText(text));
}
public static H2 H2<TDoc>(this TDoc doc, string text)
where TDoc : Document
{
ElementFactory.RegisterElementType(typeof(H2));
return doc.ElementOfType<H2>(Find.ByText(text));
}
public static H3 H3<TDoc>(this TDoc doc, string text)
where TDoc : Document
{
ElementFactory.RegisterElementType(typeof(H3));
return doc.ElementOfType<H3>(Find.ByText(text));
}
而我的FormExtension.cs包括
public static H1 H1<TForm>(this TForm form, string text)
where TForm : Form
{
ElementFactory.RegisterElementType(typeof(H1));
return form.ElementOfType<H1>(Find.ByText(text));
}
public static H2 H2<TForm>(this TForm form, string text)
where TForm : Form
{
ElementFactory.RegisterElementType(typeof(H2));
return form.ElementOfType<H2>(Find.ByText(text));
}
public static H3 H3<TForm>(this TForm form, string text)
where TForm : Form
{
ElementFactory.RegisterElementType(typeof(H3));
return form.ElementOfType<H3>(Find.ByText(text));
}
以上两个类都在同一个命名空间下。当我尝试使用和测试上述扩展时,我会遇到歧义问题。
Console.WriteLine(browser.H1("h1 browser").Exists);
Console.WriteLine(browser.Forms.First().H1("h1 form").Exists);
Console.WriteLine(browser.Frames.First().H1("h1 frame").Exists);
支持所有Div、Form、Frame和Browser的正确方法应该是什么?当我只使用DomContainer时,浏览器和表单就可以工作了。当我只使用"文档"时,"浏览器"one_answers"框架"会起作用。正在等待您的反馈!
我似乎应该用一种不同的方式来完成,那就是扩展所需的类并检查继承。最好的方法是扩展最后一个派生类,而不使用where子句。
public static H1 H1(this Form form, string text)
public static H1 H1(this Frame frame, string text)
就是这样!