Determine element visibility in WatiN with jQuery

This Stack Overflow post by MrBlueSky describes how to determine element visibility in a WatiN test case. As a convenience, here’s that code refactored as an extension method on Browser:

public static class BrowserExtensions
{
    public static bool IsElementVisible(this Browser browser, Element element)
    {
        var command = string.Format("$('#{0}').is(':visible');", element.Id);
        return browser.Eval(command) == "true";
    }
}

Usage:

            using (var browser = new IE("http://example.com/login.aspx"))
            {
                var page = browser.Page<Login>();

                page.LoginButton.Click();

                Assert.IsTrue(browser.IsElementVisible(page.UsernameRequiredMessage));
                Assert.IsTrue(browser.IsElementVisible(page.PasswordRequiredMessage));
            }

Leave a Reply

Your email address will not be published. Required fields are marked *