This is the second post in a series on creating automated UI tests for an ASP.NET app using C# and Selenium. My goal is to highlight and explain the key pieces required starting with Selenium WebDriver in 2017.
In the last post I introduced WebDriver and created a bare minimum example for getting WebDriver running and using it to open the Google homepage in Chrome. The Selenium docs provides examples of the WebDriver API including fetching pages, locating elements and getting text values. These are worth a look to get an understanding of some of the functionality available.
I was planning on providing some WebDriver examples this week, but there is no point in me rehashing the ones provided in the docs. There are a few things worth highlighting, but to do this I will need an example web app and this week I am going to explain how to run the application before executing the tests.
Testing your application with Selenium (and xUnit)
You could start your web application manually before executing tests, but it does become tedious, so it makes sense to setup the tests to execute the application automatically.
A lot of the code I added to start the application comes from Michael Whelan’s post “Black-Box Testing ASP.Net: IIS Express and Selenium WebDriver”, which in turn comes from Seleno (which Michael contributes to). Take a look at Seleno as it will simplify your UI testing. I have not introduced it into my examples, because I wanted to build up an understanding of how to UI test without a particular framework, however in a real UI test solution I would use Seleno.
This week I added a test (Run_Application_And_Navigate_To_Home_Page in _2_Running_A_Web_App) that opens the homepage of the example web app and checks for the element containing “Hi!”. I’ve kept the test simple and haven’t added any asserts or waits, which would there would normally be, but the test will still fail as an exception is thrown if the element is not found.
The code for starting the web app is located in the WebAppFixture class. It uses the Process object to start an IIS Express process running the example web application. As mentioned before it is based on code from Michael Whelan and Seleno.
To run this code before the tests, I have setup WebAppFixture as a test collection fixture. This is xUnit specific, but other test frameworks will have similar ways of configuring code to run before tests. This requires an intermediate class (WebAppFixtureTestCollection) to configure the test collection.
That’s all for this post. I had started some more specific examples with WebDriver, but realised I needed an example web app for the tests. That led to automating the startup of it, which became the focus for this week.
Thanks again to Michael Whelan and the other Seleno contributors. Next post I intend to add some more specific examples now that I have an example web application for the tests.