Selenium and testing WebInterfaces
On some cases you need to test the whole web interface. Why ? Suppose you have some Ajax call. You can test the call on server, but how do you ensure that user experience is OK ?
There are several testing projects for Web – for example selenium and Watin
I will show how to test with Selenium + NUNIT
- Download selenium from http://seleniumhq.org/ and download Firefox add-on from http://seleniumhq.org/projects/ide/
- Start selenium with java -jar selenium-server.jar
- Record the test with FF.
- Create a usual NUnit test project and add a reference to ThoughtWorks.Selenium.Core.dll
- The code can be like this :
using System; using System.Text; using System.Text.RegularExpressions; using System.Threading; using NUnit.Framework; using Selenium; namespace InvoiceTest { [TestFixture] public class TestWeb { private ISelenium selenium; private StringBuilder verificationErrors; [SetUp] public void SetupTest() { //java -jar selenium-server.jar //selenium = new DefaultSelenium("localhost", 4444, @"*firefox d:\Program Files\Mozilla Firefox\firefox.exe","<a href="http://localhost/");">http://localhost/");</a> selenium = new DefaultSelenium("localhost", 4444, @"*iexplore", "<a href="http://localhost/");">http://localhost/");</a> //selenium = new DefaultSelenium("localhost", 4444, @"*iexploreproxy", "<a href="http://localhost/");">http://localhost/");</a> selenium.Start(); verificationErrors = new StringBuilder(); } [TearDown] public void TeardownTest() { try { selenium.Stop(); } catch (Exception) { // Ignore errors if unable to close the browser } Console.WriteLine(verificationErrors.ToString()); Assert.AreEqual("", verificationErrors.ToString()); } [Test] public void FindTextAfterClickOnButton() { selenium.Open("/<strong>your application</strong>"); selenium.Focus(""); selenium.WindowMaximize(); selenium.Type("the textbox", "the text"); selenium.Click("button "); selenium.WaitForPageToLoad("30000"); try { Assert.IsTrue(selenium.IsTextPresent("<strong>new text from your application</strong>")); } catch (AssertionException e) { verificationErrors.Append(e.Message); Console.WriteLine(e.Message); } } } }