Question

I am trying to write extension methods in WatiN for H1, H2, H3 and UL, all these elements should be supported for Browser, Frame, Form and Div. My approach was to create different .cs file like FormExtensions.cs, FrameExtensions.cs and say DocumentExtensions.cs

In my DocumentExtensions.cs , I include below code.

    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));
    }

While my FormExtensions.cs includes

    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));
    }

Both the above classes are under same namespace. When I try to use and test the above extensions I get ambiguity issue.

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);

What should be the right approach to support all Div, Form, Frame and Browser? When I use only DomContainer then Browser and Form work. When I use only Document then Browser and Frame work. Waiting for your feedback!

Was it helpful?

Solution

It seems I should do it in a different way that is by extending required class and checking inheritance. Best thing is to extend the last derived class and not using the where clause.

public static H1 H1(this Form form, string text)

public static H1 H1(this Frame frame, string text)

That's it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top