In this post I will give the briefest of demonstrations of using the WebAii testing framework by showing how to carry out a simple search on google and verify the results – but first, a brief introduction to WebAii.
WebAii is a web automation testing framework, originally developed by ArtOfTest, now provided as a free framework by Telerik. It is similar in idea to other existing browser automation tools such as Selenium, WatiN (even QTP). However, unlike Selenium/WatiN, it is not an open source tool – it is developed and supported by Telerik. The tool is completely free to use – and a paid for version of the product is also available, which offers further functionality, such as the “Test Studio”, which provides test record and playback capabilities amongst other things. However, in this and future posts I will only be talking about the free version of WebAii. This means that there will be no mention of the Test Studio or the record or playback capabilities of the commercial version. All my posts will demonstrate test creation in Visual Studio.
Firstly, installing WebAii is straightforward. It is listed under the free products section on Telerik’s site. After downloading, simply run the msi file, and WebAii will be installed.
Before going any further, I recommend reading the “Configuring your browser” section from the WebAii documentation site. This will ensure that your chosen browser/browsers are ready for use with WebAii.
As mentioned, the first example I want to demonstrate is simply opening google and carrying out a basic search. I will be using Visual Studio and NUnit along with WebAii to demonstrate this.
To get started, create a new C# Library project. Once created, add a reference to “ArtOfTest.Common” and “ArtOfTest.WebAii” which can be found in the WebAii installation directory (in program files). You will also need to reference “nunit.framework” for our unit tests. Finally, use the following code to create our first simple test.
using System.Threading;
using ArtOfTest.WebAii.Controls.HtmlControls;
using ArtOfTest.WebAii.Core;
using NUnit.Framework;
namespace WebAiiTestExamples
{
public class WebAiiTestExamples
{
private Settings mySettings;
private Manager myManager;
[SetUp]
public void SetUp()
{
mySettings = new Settings(BrowserType.InternetExplorer, @"c:\log\");
myManager = new Manager(mySettings);
myManager.Start();
}
[TearDown]
public void TearDown()
{
myManager.Dispose();
}
[Test]
public void SimpleTest()
{
myManager.LaunchNewBrowser();
myManager.ActiveBrowser.NavigateTo("http://www.google.com");
myManager.ActiveBrowser.Find.ByName("q").Text = "cheese";
Thread.Sleep(1000);
myManager.ActiveBrowser.RefreshDomTree();
Assert.That(myManager.ActiveBrowser.ContainsText("www.cheese.com"), Is.True);
}
}
}
The google test can be run directly from Visual Studio using Resharper, TestDriven.Net etc. or directly from NUnit. You should see Internet explorer open, navigate to google, type “cheese” and then a bunch of results appear on the google search results page. Finally, the browser should close. Let’s break down the code above and examine what is happening at each stage.
If you are familiar with NUnit, or testing frameworks in general, then the above test class should be self explanatory. When tests are run, the method decorated with the SetUp
attribute will be called first. Anything which needs to happen prior to the tests, such as setting up the environment, should be placed here.
mySettings = new Settings(BrowserType.InternetExplorer, @"c:\log\");
myManager = new Manager(mySettings);
myManager.Start();
In our example, we first create an instance of the Settings
class. This is configuration which will be used by the Manager
object, as is demonstrated when we instantiate the Manager
class on the next line by passing in the Settings
instance. As you can see, the Settings
class defines the type of browser we want to use in the test, and also the location where WebAii log messages will be logged to. Configuration settings can be provided using this programmatic style or by using configuration specified in a .config file. More information on WebAii configuration can be found on the WebAii Documentation appConfig topic page.
The final line in our setup method is the Manager.Start()
method. According to the WebAii 2.0 API reference, this method is used to “Initialize the communication pipelines”. Since we will be reusing our Manager class in all tests (I know there is only one at the moment), this initialisation is carried out in the SetUp
method.
Next, any methods with a Test
attribute will be run. These are the actual tests which we want to carry out.
myManager.LaunchNewBrowser();
myManager.ActiveBrowser.NavigateTo("http://www.google.com");
myManager.ActiveBrowser.Find.ByName("q").Text = "cheese";
Thread.Sleep(1000);
myManager.ActiveBrowser.RefreshDomTree();
Assert.That(myManager.ActiveBrowser.ContainsText("www.cheese.com"), Is.True);
The first line launches a new instance of the configured browser, in our case, Internet Explorer. The Manager
class can actually launch multiple instances of the same browser, or different browsers, however in this example we will only be using a single instance of Internet Explorer. Next, we navigate to google using the active, and only, browser. We then type our search criteria, “cheese”, into the google search textbox.
Lets break down the command on line 3 to understand exactly what is happening with this line of code.
Firstly, we are using the Find
property on the active Browser
object. The Find
class provides methods to search for elements on our Browsers DOM. The are numerous methods available on the Find
class, which are well documented in the WebAii Documentation element identification page.
In this example we are using the ByName()
method, and providing the “q” parameter, which is the name of the search textbox on google. Also, notice the <HtmlInputText> after the method name. This specifies the TControl type of the elements returned by the Find method. Simply, it provides a wrapper around the the elements, to ensure the appropriate properties and methods are available on the element. For example, it allows access to the Text
property on our google search box, but doesn’t provide a Checked
property, which would be redundant on a text field.
Finally, we set the text of our input text box to “cheese”.
The next 2 lines of our test are curious, and a few years ago would have been unnecessary. Google now automatically provides search results as soon as you begin to type search criteria using AJAX. As a result, you don’t need to click the search button at all. However, for our test, we will need to give Google time to return our results, hence the Sleep
for 1000 milliseconds. This is not an ideal way to deal with AJAX results, and for anything other than our simple demonstration, something more deterministic would be highly recommended. Secondly, we need to inform WebAii that the DOM has actually been updated by the AJAX request, otherwise we will not be able to find the results of the google search. This is done with the RefreshDomTree()
method.
Finally, after our search has been completed, we can use the Browser objects ContainsText()
method to ensure that our results contain the text “www.cheese.com”. If it does, then our unit test will pass. If it does not, then our unit test will fail.
Finally, the method decorated with the TearDown
attribute will be run.
myManager.Dispose();
This should clean up the environment and dispose of anything which created during the SetUp
method which needs to be disposed of, in our case, the Manager
class.
So, if all has gone well, then you have just seen your first simple WebAii test open google, carry out a search, and verify the results contain what you expect. This is just the tip of the iceberg in terms of WebAii functionality, and in future posts I will further explore the functionality WebAii offers.
Good luck..