UI Unit Testing with Selenium – Part 2 – Webdriver Basics

Last time, I did a quick run-through of Selenium IDE for Firefox and got acquainted with the interface and commands associated with Selenium.  This time, I’m diving more into the meat of the framework and using Webdriver.

Webdriver is an API for that can be used by various languages (C#, Java, Python, & Ruby are some examples) that allows your tests to be maintained and run directly from code rather than using a recorder.  This allows us to take a more programmatical approach to testing rather than relying on the recorder exclusively.

We will be using Visual Studio for this example.  Firefox is supported right out of the box, but IE and Chrome require a special binary to be staged locally in order for those browsers to be used with Selenium.  This (and others) can be located via links at SeleniumHQ.

Since Chrome is my own preferred browser, I’m glad to move away from Firefox and experiment with something a bit more familiar.  I decided to start with rerunning the test that I created using Selenium IDE previously, then build on that a bit.

One feature that I didn’t mention about Selenium IDE, is the export function.  This creates source code in your chosen framework (C#, Java, etc.) from the Selenium markup code created by the recorder.

seleniumexport

One cool thing about the export is that it intersperses the markup within your code as comments.  This can be very useful when retracing what was done in a recording or for general documentation.

First off, I created a C# console project and installed the Selenium Webdriver plugin from NuGet.  With that in place, I’ve added the export from my previous post to a new project to test with Chrome.  My code is below:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace WebDriverDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new ChromeDriver(@”C:\Libraries\”);
            driver.Navigate().GoToUrl(“https://dev20556.service-now.com/navpage.do”);
            driver.SwitchTo().Frame(“gsft_main”);
            driver.FindElement(By.Name(“user_name”)).Click();
            driver.FindElement(By.Id(“user_name”)).Clear();
            driver.FindElement(By.Id(“user_name”)).SendKeys(“USERNAMEGOESHERE”);
            driver.FindElement(By.Id(“user_password”)).Clear();
            driver.FindElement(By.Id(“user_password”)).SendKeys(“PASSWORDGOESHERE”);
            driver.FindElement(By.Id(“sysverb_login”)).Click();
        }
    }
}

This sample using the export from my previous post results in a successful login.  Now to build on it a little bit and create a test Incident:

            driver.FindElement(By.LinkText(“Incidents”)).Click();
            driver.SwitchTo().Frame(“gsft_main”);
            driver.FindElement(By.Id(“new_incident”)).Click();
            SelectElement urgency = new SelectElement(driver.FindElement(By.Name(“IO:5a33d0ef0a0a0b9b007b906f6c589c57”)));
            urgency.SelectByText(“1 – High”);
          driver.FindElement(By.Name(“IO:3f272c500a0a0b990059c24380a2bc02”)).SendKeys(“Creating a test incident.”);

            driver.FindElement(By.Name(“submit_button”)).Click();

Just adding these few extra lines results in a repeatable New Incident test.

incidentsuccess

It’s easy to start to see how this framework can be used to build a library of repeatable tests to automate QA and regression testing.

Link to the Full .NET API : http://seleniumhq.github.io/selenium/docs/api/dotnet/

As I continue to progress through the Pluralsight course, my next post should cover advanced WebDriver concepts and possibly Selenium Server.

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!

UI Unit Testing with Selenium – Part 1


So, I’m making it through the Selenium Pluralsight course slowly but surely and I’m learning quite a bit so far.

The first part covers a browser plug-in called Selenium IDE.  Available only for Firefox, it’s essentially a session recording tool,  It records what we do in the browser then parses those into commands and then can be played back to act as tests.  To take this out for a spin, I went ahead and installed the plug-in and watched the demo on the course.  Now, we’ll need to find something suitable to test it on.

selenium

There’s a SaaS platform called ServiceNow that I’ve been spending a great deal of time in this year.  Automating testing against this platform from a central host is one of my goals for this experiment.  To test this out, I’ve created a test instance of ServiceNow and a test user.  Also, I’ve installed the Selenium IDE on Firefox so that I can record my session in the platform.

My demo is basically automating a login for my test user.  First I’ll need to create a fresh test case in Selenium.

newtest

Then, I’ll record the steps to the login to get a complete test.

finishedtest

Then I’ll run the test and see if it logs me into the system, which it does.

success

This is just scratching the surface of Selenium and I have more modules in the course to cover.  Next post, I’ll have some additional content from the next module (Selenium Web Driver) and possibly a little more.

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!

UI Unit Testing with Selenium – An Experiment

It’s been a while since I’ve posted.  Part writers block and part outside obligations.  I know you’ve all been despondent and lost in the meantime.  Sorry about that. 🙂

Something that’s a recurring puzzle for me is adapting unit testing to MVC and SaaS solutions.   The challenges from my point of view are that MVC implies a lot of its functionality.  For example, things in MVC behave in very specific manners based on where they’re located or what they’re named.  Also, MVC classes (models) generally don’t have or need default constructors, so a traditional approach of instantiating a class an running business logic is insufficient.

Then, for SaaS solutions, you may not have sufficient access to the plumbing to build your tests directly, so you’re stuck automating a session through the SaaS provider’s interface and (hopefully) capturing the results that you need.  An approach to MVC might look very similar.

Enter Selenium

Either my Google-fu is weak in this space or there aren’t a lot of options for automating tests in this manner.  One framework that gets a lot of mentions is Selenium, so I’m going to take a stab at it.   I’ve found a few good blog resources but also remember that John Sonmez did a Pluralsight course that seems to be right up my alley.

My plan for this week is to watch the course and play around with the framework against a solution or two, then post about the results.  If the path seems promising, I’ll follow up with some additional content.

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!