5.12. Automating Cross-Browser Tests

One of the main problems with web testing is dealing with different browsers and rendering engines. Cross-browser testing ends up being a manual process most of the time because it's very difficult to test exactly how a browser will render the page. One technique that could be applied is taking a screenshot for each test in each browser, but this calls for a manual process that includes looking at the screen captures.

A great use of automated cross-browsers is in testing JavaScript. With the popularity of AJAX in recent years, developers have found that JavaScript does not behave the same way between browsers in all situations. Creating automated UI tests with WatiN or Selenium can help ensure that your application works in all major browsers. The following code snippet is a cross-browser test using WatiN:

public void Should_Click_Search_On_Google_And_Return_Results_For_AspNet()
        {
            IBrowser browser = BrowserFactory.Create(BrowserType.InternetExplorer);
            Return_Results_For_ASP_Net(browser);

            browser = BrowserFactory.Create(BrowserType.FireFox);
            Return_Results_For_ASP_Net(browser);
        }

        private void Return_Results_For_ASP_Net(IBrowser browswer)
        {
            browswer.GoTo("http://www.google.com");
            browswer.TextField(Find.ByName("q")).Value = "asp.net";
            browswer.Button(Find.ByName("btnG")).Click();
            Assert.IsTrue(browswer.ContainsText("The Official Microsoft ASP.NET
Site"));
        }

Remember, to run WatiN tests in Firefox, the jssh.xpi add-in must be installed on the ...

Get Testing ASP.NET Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.