I would like to show you how easy it is to use unit test in Visual Studio and Selenium Web Driver to make automated UI test.
The plan is simple: host your app, investigate given page, make assertions. The first problem is how to host our application from inside a unit test? I want to host app using current code not from external source which is already delopyed – post deployment testing. To do that we can use IIS Express…
Knowing that IIS Express can be run from command line:
iisexpress.exe /path:"<path to my application directory>" /port:<port number>
This abstract class will help us to to run our application before all tests methods. It holds reference to IIS Express process and has two methods: GetApplicationPath and GetAbsoluteUrl. GetApplicationPath method return absolute path to our application - based on given project name. GetAbsoluteUrl method is only a helper that make absolute path from relative path.
Another cool tool is Selenium WebDriver. We can use that tool to communicate with web browser and investigate given page. We can fill a form, click on links or try to find an elelemnts simply trough id or XPath query. Below is yet another abstract class that inherits from IISExpressTest and is responsible for launching a web browser. In this example I used Chrome driver but you can use IE or Firefox:
As you can see its very simple class that is only responsible for launching and disposing the WebDriver object. WebDriver allows you to send commands to the web browser such as read DOM elements, send text to input fields, perform clicks and much more!
Here is example how to use above classes:
Above code will navigate to the main page and will try to login by filling a login form. Next, just to be sure, we find element that holds user name which confirms successful login.
The only thing you should do is to create a subclass of SeleniumTest, provide name of app we would like to test and write a test methods. Thats it!