![](/img/trans.png)
[英]How can I do multiple h1 elements replace to h1,h2,h3,h4,h5,h6 in javascript?
[英]WatiN extensions for H1 H2 H3 for Form, Browser and Frame
我正在嘗試在WatiN中為H1,H2,H3和UL編寫擴展方法,所有這些元素都應支持Browser,Frame,Form和Div。 我的方法是創建不同的.cs文件,例如FormExtensions.cs,FrameExtensions.cs並說DocumentExtensions.cs
在我的DocumentExtensions.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));
}
雖然我的FormExtensions.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時,瀏覽器和表單即可工作。 當我僅使用文檔時,瀏覽器和框架便可以工作。 等待您的反饋意見!
似乎我應該通過擴展所需的類並檢查繼承來采用其他方法。 最好的辦法是擴展最后一個派生類,而不使用where子句。
public static H1 H1(this Form form, string text)
public static H1 H1(this Frame frame, string text)
而已!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.