UI Unit Testing with Selenium – Part 3 – Advanced Webdriver & A Real World Example

Last time, I got into the basics of Webdriver and put together a couple of tests against ServiceNow.  For this post, I’m going to take a look at more advanced uses of Webdriver as well as putting together a framework of shared methods which can be hooked up to unit tests.  This involved going a little off script for the original course, but I think this series might be better served demonstrating application of the actual content rather than repeating it.

The goals that I have for this post will involve creating a new Solution with a Unit Test project as well as a Selenium framework class that can be reused within multiple tests.  Again, I’ll be testing against my private dev instance of ServiceNow, but I think it will be readily apparent how the framework could be extended as needed for other web frameworks.

I began by first creating a new testing project in Visual Studio, called SeleniumFrameworkTests.  Then I added the following tests based on the activities I demonstrated in the previous post.

LoginAsUser
LoginAsSuperUser
CreateIncidentAsUser
CreateIncidentAsSuperUser

I knew I’d want each test to be completely abstracted from the plumbing of ServiceNow, so I kept each test down to three arguments: environment, username and password.  The environment would hold the URL of the instance we’re connecting to.  And the username and password would contain those values for the account role needed to perform the tasks.  Lastly, I created an app.config file to hold the values.

testproject

Next, I created the main framework class called SeleniumFramework.  This framework class would need to include the Selenium and Selenium.Support packages from NuGet.  With that in place, I created the general Login method which accepts the environment, username and password arguments.  Then I copied the login steps from the previous post.  This allowed both the LoginAsUser and LoginAsSuperUser tests to pass.

loginmethod

The CreateIncident methods proved to be a little trickier.  First, each would require it’s own IWebDriver object with a valid login in order to proceed.  It made more sense to create a private method that returns an IWebDriver object for use by the CreateIncident methods.

privatelogin

With that in place, I copied in the CreateIncident steps from the previous post. The CreateIncidentAsUser test then passed but the SuperUser role has a different interface than the base User.  To get that test to pass, I had to create a separate method and tweak the UI names as needed for the test to then pass.

createincident

To wrap it all up, I created a new repo on Github called SeleniumFramework. Please feel free to take it for a spin yourself.   All you’ll need is a demo instance of ServiceNow as well as a copy of Visual Studio Community, both of which are free!

I know this series has taken much longer than I expected. However, I plan to close it out next time with an implementation of Selenium Server, most likely using the above testing project.

As always, if you have any questions or comments, please feel free to add them here or address them to john@benedettitech.com.

Thanks for looking in!