Get Started With Selenium 3 and Selenium Grid
In this post we will go over everything you need to know to run your first web automation tests with Selenium 3.
What is Selenium?
Selenium is a free and open-source browser automation framework. This means with Selenium you can automate almost anything in any popular web browser. The great thing about Selenium is that it is backed by all major browser vendors including Google, Apple, Mozilla and Microsoft.
How does it work?
In your testing code you always use the same commands to trigger actions in the web browsers. Actions can be anything the user would be able to do like for example clicks, text input or navigation. Each browser vendor provides the Selenium project with a custom driver for their browser which translates the Selenium commands to real actions inside the browser. This approach has a few key advantages:
- You can run the same test code against all different browsers
- The drivers are provided by the browser vendor and therefor are mostly up to date
- All tests run against the unmodified release version of the browser to give you most accurate results
Which browsers are supported?
Currently you can run Selenium tests in all major browsers like Chrome, Firefox, Safari, Internet Explorer, Edge and Opera. With a little more effort you can even test mobile browsers like Safari on iOS and Chrome on Android but the setup for this is a little more complicated and will be topic of a future post.
Which programming languages are supported?
Selenium and third-parties like Facebook provide language bindings for a great number of programming languages. This means you will be able to write Selenium tests with any of the below languages:
- Java
- C#
- Ruby
- Python
- JavaScript
- Perl (third-party)
- PHP (third-party)
Setting up Selenium
Now that we know how powerful Selenium is let us take a look at what you need to do in order to run a first simple test case. The below example will show all steps to setup Selenium on Windows and Firefox, Chrome, Edge and Internet Explorer but the process is the same for any operating system and supported browser.
Requirements
- Install Firefox: https://www.mozilla.org
- Install Google Chrome: https://www.google.com/chrome/browser/desktop/
- Create a new folder for all Selenium related files and add it to the PATH environment variable (I will be using c:\Selenium)
Installing the Drivers
As mentioned earlier you need a specific driver for every browser you would like to use in your automated testing. You can get the drivers as described below:
Internet Explorer Driver:
- Download (32bit or 64bit) http://www.seleniumhq.org/download/
- Extract to c:\Selenium
Edge Driver:
- Download https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
- Copy to c:\Selenium
Chrome Driver:
- Download https://sites.google.com/a/chromium.org/chromedriver/
- Copy to c:\Selenium
Firefox Driver:
- Download https://github.com/mozilla/geckodriver/releases
- Extract to c:\Selenium
Safari Driver:
Now you are all set to create your first test case.
A Simple Selenium Test
In this example we will be using C# to create our tests but the process is similar for every other programming language.
To run any Selenium test in C# and Visual Studio you need to create a new Testing Project and add references to the Selenium language bindings to your project. The language bindings can be found on the Selenium homepage or in case of C# as a nuget package (Selenium.Webdriver and Selenium.Support).
In our simple test example we will:
- Open Firefox
- Navigate to the Google homepage
- Type ‘Selenium’ into the query field
- Submit the search
- Wait on the search results generated by JavaScript
- Assert that the search has been sucessful
- Close the browser
The code looks like this:
[TestMethod]
public void GoogleForSelenium()
{
// Launch new instance for Firefox
IWebDriver driver = new FirefoxDriver();
// Navigate to google
driver.Navigate().GoToUrl("https://www.google.com");
// Find the input field for the search query
IWebElement inputField = driver.FindElement(By.Name("q"));
// Add some text to the input field
inputField.SendKeys("Selenium");
// Submit the search
inputField.Submit();
// Google uses JS to render the results page so we need to wait
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(o => o.Title.StartsWith("Selenium", StringComparison.OrdinalIgnoreCase));
// Use asserts like you would in unit tests
Assert.IsTrue(driver.Title.StartsWith("Selenium", StringComparison.OrdinalIgnoreCase));
// close down the browser
driver.Quit();
}
If you run this test you will see the Firefox window executing the steps above.
While this test works it is really not browser independent as the first line creates an object called FirefoxDriver which cannot be used to run the test in other browsers. So let us modify the code to run a separate test case for Firefox, Chrome, Internet Explorer and Edge.
[TestMethod]
public void GoogleForSelenium_Firefox()
{
IWebDriver driver = new FirefoxDriver();
GoogleForSelenium(driver);
}
[TestMethod]
public void GoogleForSelenium_Chrome()
{
IWebDriver driver = new ChromeDriver();
GoogleForSelenium(driver);
}
[TestMethod]
public void GoogleForSelenium_Edge()
{
IWebDriver driver = new EdgeDriver();
GoogleForSelenium(driver);
}
[TestMethod]
public void GoogleForSelenium_InternetExplorer()
{
IWebDriver driver = new InternetExplorerDriver();
GoogleForSelenium(driver);
}
public void GoogleForSelenium(IWebDriver driver)
{
// Navigate to google
driver.Navigate().GoToUrl("https://www.google.com");
// Find the input field for the search query
IWebElement inputField = driver.FindElement(By.Name("q"));
// Add some text to the input field
inputField.SendKeys("Selenium");
// Submit the search
inputField.Submit();
// Google uses JS to render the results page so we need to wait
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(o => o.Title.StartsWith("Selenium", StringComparison.OrdinalIgnoreCase));
// Use asserts like you would in unit tests
Assert.IsTrue(driver.Title.StartsWith("Selenium", StringComparison.OrdinalIgnoreCase));
// close down the browser
driver.Quit();
}
Running this will execute the test case in all 4 browsers.
If you are running a 64 bit version of Windows you might notice that Internet Explorer executes the test much slower than the other browsers. For this reason it is recommend that you always use 32 bit version of Windows / Internet Explorer to run Selenium tests in Internet Explorer.
Selenium Grid
Not bad so far. But what do you do if you need to run the tests against browsers that are not available on your local machine? You use Selenium Grid!
What is Selenium Grid?
Selenium Grid can be used to run Selenium tests parallel and on multiple machines. This can help with:
- Reducing the overall time of test execution
- Running tests against browsers on different operating systems
- Creating a dedicated test rig
- Executing tests as part of the build process
How does it work?
Selenium Grid works a little bit different than executing tests on the local machine. While you can use the same testing code to execute tests against a Selenium Grid you will need to use the RemoteDriver instead of the browser specific drivers like FirefoxDriver or ChromeDriver.
This remote driver will execute the tests against a Selenium Hub which automatically delegates the test execution to one or more Selenium Nodes. These Selenium Nodes can support different browsers and can be run on different operating systems so that you can execute tests against browsers on Windows, macOS and Linux with the same testing code.
Setup a Selenium Hub
Running a Selenium Hub is quite simple, all you need to do is follow these 4 steps.
1. Download Java: https://www.java.com/en/download/
2. Download Selenium Standalone Server: http://www.seleniumhq.org/download/
- Copy to c:\Selenium
3. Start the Hub with the command line:
- java -jar selenium-server-standalone-3.0.1.jar -role hub
4. Grid console available at: http://localhost:4444/grid/console
Now a Selenium Hub without any Nodes is not that useful so we will need to setup at least one Selenium Node. Keep in mind that the Hub and Node can run on different machines.
Setup a Selenium Node
1. Download Java: https://www.java.com/en/download/
2. Create a node configuration file:
{ “capabilities”: [{
“browserName”: “firefox”,
“platform”: “WINDOWS”,
“maxInstances”: 1
}],
“maxSession”: 5,
“port”: 5555,
“register”: true
}
3. Start the Node with the command line:
- java -jar selenium-server-standalone-3.0.1.jar -role node -hub http://localhost:4444/grid/register -nodeConfig node.json
In the node configuration file you can specify which browsers the node is able to execute tests in. This will tell the hub which tests it can execute on a given node.
Running a Test
To run the above test against the Selenium Grid we only need to change the driver object all the other test code will stay exactly the same.
[TestMethod]
public void GoogleForSeleniumOnGrid()
{
DesiredCapabilities capabilities = new DesiredCapabilities("internet explorer", "11", new Platform(PlatformType.Windows));
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
// Navigate to google
driver.Navigate().GoToUrl("https://www.google.com");
// Find the input field for the search query
IWebElement inputField = driver.FindElement(By.Name("q"));
// Add some text to the input field
inputField.SendKeys("Selenium");
// Submit the search
inputField.Submit();
// Google uses JS to render the results page so we need to wait
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(o => o.Title.StartsWith("Selenium", StringComparison.OrdinalIgnoreCase));
// Use asserts like you would in unit tests
Assert.IsTrue(driver.Title.StartsWith("Selenium", StringComparison.OrdinalIgnoreCase));
// close down the browser
driver.Quit();
}
This test case will execute against the Selenium Grid. Of course you need to modify the URL if your Grid runs on a separate machine. To run the test against a different browsers simply change the capabilities object as necessary.
BrowseEmAll Grid
As you have seen a Selenium Grid can be a powerful asset to your automated web testing. But unfortunately it is quite a task to setup if you need to test against different operating systems and different versions of Internet Explorer. Multiple machines, different driver version and keeping everything up to date is quite a task.
Because of this we have integrated a Selenium Grid in our cross browser testing solution BrowseEmAll (14-day free trial) to simplify this process.
To start a Selenium Grid with BrowseEmAll all you need to do is switch to the Selenium tab, select the browsers you want in your Grid and hit the Start button. BrowseEmAll will automatically create and start virtual machines as necessary to execute your tests in all selected browsers. Of course everything runs directly on your local machine!
And the best part? You don’t need to change any of your test code to run your Selenium tests against the BrowseEmAll Grid. Just use the Remote driver as shown above and you will be all set.
Sounds good? Give it a try or request a custom demo to learn more.