[Software Review] Selenium : Functional Testing of Web Applications

Selenium, a framework that supports functional testing of web applications, simulates a user clicking links, filling out forms and submitting them. Selenium comes in two flavours, Selenium Core, which runs directly and only in the browser, which provides support for simple sequence of actions and tests. Selenium Remote Control is essentially a server written in Java with two functions: it acts as a proxy server for the browser and takes commands via HTTP that will be executed in the browser. The proxy server is needed because browsers rightfully don’t allow Javascript from one site to run on another site.
You can use Selenium in two modes: test runner and driven. The two modes differ in complexity and the way they are written. Driven test scripts tend to be more complex to write since they are written in a programming language. The difference in complexity is minimal if you use a high-level dynamic programming language like Python or Ruby. The biggest difference between the two modes is that the tests are partly run from outside the browser if using driven scripts in which test runner scripts are run completely inside the browser. Both test runner and driven test cases can be integrated with continuous integration tools.
Example Test Case Automation Using Selenium:
- Consider a simple test case wherein we open the google home page search for "Selenium" and ensure that the result page contains the text "OpenQA:Selenium".
The HTML code for this would look like this:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>testcase1</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">testcase1</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>q</td>
<td>Selenium</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>//input[@name='btnG']</td>
<td></td>
</tr>
<tr>
<td>verifyTextPresent</td>
<td>OpenQA: Selenium</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>


The C# code for the same test case using Selenium Remote Control would look like this:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;

namespace SeleniumTests
{
[TestFixture]
public class NewTest
{
private ISelenium selenium;
private StringBuilder verificationErrors;

[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:4444");
selenium.Start();
verificationErrors = new StringBuilder();
}

[TearDown]
public void TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}

[Test]
public void TheNewTest()
{
selenium.Open("/");
selenium.Type("q", "Selenium");
selenium.Click("//input[@name='btnG']");
selenium.WaitForPageToLoad("30000");
try
{
Assert.IsTrue(selenium.IsTextPresent("OpenQA: Selenium"));
}
catch (AssertionException e)
{
verificationErrors.Append(e.Message);
}
}
}
}


1 comment:

  1. Hi,
    Just came across your blog. Good work I must say. I was wondering if you know of any tools for GUI concurrency testing.

    ReplyDelete