Getting started with Selenium

While preparing for a demo where I run Selenium tests during a release, I decided to document what it takes to get the test to run in IE, Chrome, and Firefox.

I began by spinning up a Windows Server 2012 R2 virtual machine with Visual Studio 2015 Enterprise in Microsoft Azure.  I then installed the latest versions of Chrome (44) and Firefox (39).  I also downloaded the Selenium IDE plugin for Firefox.  It is an optional component and you do not have to install it.  However, it can be a way to jumpstart your test.

When working with Selenium, I can begin with a simple class library; however, I prefer to begin with a Coded UI Test Project if I am going to be using MSTest.  When I intend on using xUnit or NUnit, I can start with a simple class library because I am going to have to add references for those frameworks anyway. Using this project template will bring in some other references for attributes, such as TestCategory, which I like to use.

Now we need to add the Selenium assemblies and drivers.  Right click on your test project and select Manage NuGet Packages… Add the following packages to your project:

·         Selenium.WebDriver

·         Selenium.Support

·         Selenium.WebDriver.ChromeDriver

·         Selenium.WebDriver.IEDriver

I installed only Chrome and IE drivers because the Firefox driver is part of the core framework and does not have to be installed separately.  The drivers are how I select the browser I want to use to execute my test.

At the top of the file add the following using statements:

using OpenQA.Selenium.Firefox;

using OpenQA.Selenium.Chrome;

using OpenQA.Selenium.IE;

using OpenQA.Selenium;

I am going to use the commented out methods in the Additional test attributes region to prepare my driver for use in my test.  By creating the driver here, I will be able to change it in one place for all the tests I create in this class.  Each of the browser drivers implement the IWebDriver interface, so I am going to declare a private member to hold the driver for my tests.  I am also going to start the URL to test in a private member named baseURL.

private IWebDriver driver;

private string baseURL;

Now, in the MyTestInitialize method, I am going to add the following code:

driver = new FirefoxDriver();

baseURL = "http://[TheURLToTest]/";

Be sure to replace [TheURLToTest] with the URL of the site undergoing tests.  The last piece of plumbing code I am going to add will be in the MyTestCleanup method.  I am going to use this method to clean up the driver.  In the method I add the following code:

try

{

   driver.Quit();

}

catch (Exception)

{

   // Ignore errors if unable to close the browser

}

With all this code in place, I can now start working on my test.  The first thing I want to do is simply open the browser and navigate to the URL under test.  To do that I add the following code to the CodedUITestMethod1 method:

driver.Navigate().GoToUrl(this.baseURL);

At this point I can run the test and see FireFox load, navigate to the URL under test, then close.

Before I continue with the test, I want to add the other drivers.  To use the Chrome browser, I replace the FirefoxDriver with the ChromeDriver class instead.  When I run the test now, I get the following error: OpenQA.Selenium.DriverServiceNotFoundException.  This is because where the tests are executed is not where the project currently resides.  I have to tell the testing framework to copy the chromedriver.exe file that was added to my project to the folder where the test will be executed.  This is done with a DeploymentItem attribute applied to the test class.  Right below the CodedUITest attribute I add the following code to deploy both the Chrome and IE drivers.

[CodedUITest]

[DeploymentItem("chromedriver.exe")]

[DeploymentItem("IEDriverServer.exe")]

public class CodedUITest1

At this point I can run the test again and see the Chrome browser open, navigate to the URL under test and close.

The final driver I am going to configure in this post is the IE driver. I simply change the ChromeDriver class to the InternetExplorerDriver.  However, when I run the test now I get the following error:

Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

To correct this issue, I need to complete the following steps.

1.       Start IE

2.       Click the gear and select Internet Options

3.       Click the Security tab

4.       Click each zone and make sure the Enable Protected Mode check box is in the same state for all the zones.

Now I am able to run the test and see the Internet Explorer browser open, navigate to the URL under test, and close.

In future posts I will complete the test and show how I got it to run in the new Release Management.

Comments (9) -

  • Another great way I like is to use the SoecFlow extension in conjunction with the F# Canopy DSL to work with Selenium that way you get a nice bdd gherkin syntax which generates mstest tests with the Selenium heavey lifting handled by F#
  • Hi Donovan,

    Thanks for the post. I wanted to get clarification on Coded UI vs Selenium. In this post, you're only using the Coded UI Project Type, but then using Selenium for all of your actual test logic and web drivers correct?

    Whereas, you could have used Coded UI for the whole thing if you wanted?
    • You are correct. I could have also just created a class library and added the Selenium Nuget packages and ended up in the same place.  I like using Coded UI projects because it adds additional references.
      • Do you have information or instruction on adding a class library?
  • Thanks for so much info!  I'm new to Coded UI projects and recently came upon an issue that I can't find info to resolve.  I have tests set up to run in IE and in Chrome.  I utilized your info to maximize my Chrome browser as it was opening too small to find all elements. (Very Helpful!)  I attempted to run my automated tests today and the code is opening Chrome but launching the URL in IE.  I'm using VS 2013 with C# coded UI tests.  I have Selenium Nuget packages downloaded.  It was working prior to today so I'm not quite sure why it's behaving so differently.  Any ideas/suggestions as to how to resolve?
    • That is a strange one. I would have to guess you have to drivers running. I would check your code again.
  • Are you using the selenium packages because they interact better with web browsers than Coded UI alone?
    • Selenium is cross-platform.  You can use it on a Mac, Linux or PC. It also runs in more browsers than CodedUI. I still use CodedUI for Windows Forms or WPF testing but Selenium for the web.

Add comment

Loading