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.