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 :
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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); } } } } |