June 19, 2017
Selenium Webdrivers Explained: Text Version
Selenium webdrivers are the key element to launching and controlling the different browsers during test execution. All other interactions with the browser happen through these webdrivers.
What Is A Webdriver?
Selenium calls its central object a webdriver. These webdrivers are used to interact with a specific browser instance. There are 2 different versions of the webdriver available a local and a remote one.
How To Use A Local Webdriver
The local webdriver is used when the actual browser instance should run on the same machine as the test code. So if you want to run the tests alongside the actual browser on your local machine you can use a local webdriver.
Actually there is a specific local webdriver for each and every browser. They are mostly working the same though so you don’t need to write specific test code for each browser.
If your environment is setup correctly (see Local Selenium Setup) you can start and stop browser instances with a few lines of code:
-
[java]
// Add a reference to the Selenium client jar from
// http://www.seleniumhq.com
// import org.openqa.selenium.WebDriver;
// import org.openqa.selenium.chrome.ChromeDriver;
// import org.openqa.selenium.edge.EdgeDriver;
// import org.openqa.selenium.firefox.FirefoxDriver;
// import org.openqa.selenium.ie.InternetExplorerDriver;
// import org.openqa.selenium.safari.SafariDriver;// Start a new instance of Google Chrome
WebDriver chromeDriver = new ChromeDriver();// close the Chrome instance
chromeDriver.quit();// Start a new instance of Firefox
WebDriver firefoxDriver = new FirefoxDriver();// close the Firefox instance
firefoxDriver.quit();// Start a new instance of Internet Explorer
WebDriver ieDriver = new InternetExplorerDriver();// close the Internet Explorer instance
ieDriver.quit();// Start a new instance of Microsoft Edge
WebDriver edgeDriver = new EdgeDriver();// close the instance of Microsoft Edge
edgeDriver.quit();// Start a new instance of Safari
WebDriver safariDriver = new SafariDriver();// close the instance of Safari
safariDriver.quit();
[/java] -
[csharp]
// Add a reference to the nuget package Selenium.Webdriver
// Add using OpenQA.Selenium;
// Add using OpenQA.Selenium.Chrome;
// Add using OpenQA.Selenium.Edge;
// Add using OpenQA.Selenium.Firefox;
// Add using OpenQA.Selenium.IE;
// Add using OpenQA.Selenium.Safari;// Start a new instance of Google Chrome
IWebDriver chromeDriver = new ChromeDriver();// close the Chrome instance
chromeDriver.Quit();// Start a new instance of Firefox
IWebDriver firefoxDriver = new FirefoxDriver();// close the Firefox instance
firefoxDriver.Quit();// Start a new instance of Internet Explorer
IWebDriver ieDriver = new InternetExplorerDriver();// close the Internet Explorer instance
ieDriver.Quit();// Start a new instance of Microsoft Edge
IWebDriver edgeDriver = new EdgeDriver();// close the instance of Microsoft Edge
edgeDriver.Quit();// Start a new instance of Safari
IWebDriver safariDriver = new SafariDriver();// close the instance of Safari
safariDriver.Quit();[/csharp]
-
[ruby]
require "selenium-webdriver"# Start a new instance of Google Chrome
chromeDriver = Selenium::WebDriver.for :chrome# close the Chrome instance
chromeDriver.quit# Start a new instance of Firefox
firefoxDriver = Selenium::WebDriver.for :firefox# close the Firefox instance
firefoxDriver.quit# Start a new instance of Internet Explorer
ieDriver = Selenium::WebDriver.for :ie# close the Internet Explorer instance
ieDriver.quit# Start a new instance of Microsoft Edge
edgeDriver = Selenium::WebDriver.for :edge# close the instance of Microsoft Edge
edgeDriver.quit# Start a new instance of Safari
safariDriver = Selenium::WebDriver.for :safari# close the instance of Safari
safariDriver.quit
[/ruby] -
[python]
from selenium import webdriver# Start a new instance of Google Chrome
chromeDriver = webdriver.Chrome()# close the Chrome instance
chromeDriver.quit()# Start a new instance of Firefox
firefoxDriver = webdriver.Firefox()# close the Firefox instance
firefoxDriver.quit()# Start a new instance of Internet Explorer
ieDriver = webdriver.Ie()# close the Internet Explorer instance
ieDriver.quit()# Start a new instance of Microsoft Edge
edgeDriver = webdriver.Edge()# close the instance of Microsoft Edge
edgeDriver.quit()# Start a new instance of Safari
safariDriver = webdriver.Safari()# close the instance of Safari
safariDriver.quit()
[/python] -
[javascript]
async function Local() {
var webdriver = require(‘selenium-webdriver’)// Start a new instance of Google Chrome
var chromeDriver = new webdriver.Builder().forBrowser(‘chrome’).build();// close the Chrome instance
await chromeDriver.quit();// Start a new instance of Firefox
var firefoxDriver = new webdriver.Builder().forBrowser(‘firefox’).build();// close the Firefox instance
await firefoxDriver.quit();// Start a new instance of Internet Explorer
var ieDriver = new webdriver.Builder().forBrowser(‘ie’).build();// close the Internet Explorer instance
await ieDriver.quit();// Start a new instance of Microsoft Edge
var edgeDriver = new webdriver.Builder().forBrowser(‘MicrosoftEdge’).build();// close the instance of Microsoft Edge
await edgeDriver.quit();// Start a new instance of Safari
//var safariDriver = new webdriver.Builder().forBrowser(‘safari’).build();// close the instance of Safari
//await safariDriver.quit();
}Local();
[/javascript]
How To Use A Remote Webdriver
To work with the remote webdriver you will need to have a Selenium Grid running on some machine in your local network or use the testing grid of a third party provider. In this case you will need supply the url of the testing grid and a special settings object detailing which browser you would like to run. Your other test code that works against a local webdriver will also work with the remote webdriver, no code changes necessary. This is great if you are using the local webdriver during development and the remote webdriver on a build server.
-
[java]
// Add a reference to the Selenium client jar from
// http://www.seleniumhq.com
// import java.net.URL;
// import org.openqa.selenium.*;
// import org.openqa.selenium.remote.*;
URL gridUrl = new URL("http://TestGrid:4444/wd/hub");// You can select another browser by changing the DesiredCapabilities
Capabilities capabilities = DesiredCapabilities.firefox();// Launches a new Firefox instance on the TestGrid server
WebDriver driver = new RemoteWebDriver(gridUrl, capabilities);// Closes the remote browser
driver.quit();
[/java] -
[csharp]
// Add a reference to the nuget package Selenium.Webdriver
// Add using OpenQA.Selenium;
// Add using OpenQA.Selenium.Remote;
// Add using OpenQA.Selenium.Firefox;Uri gridUrl = new Uri("http://TestGrid:4444/wd/hub");
// You can select another browser by changing the DesiredCapabilities
ICapabilities capabilities = new FirefoxOptions().ToCapabilities();// Launches a new Firefox instance on the TestGrid server
IWebDriver driver = new RemoteWebDriver(gridUrl, capabilities);// Closes the remote browser
driver.Quit();[/csharp]
-
[ruby]
require ‘selenium-webdriver’gridUrl = "http://TestGrid:4444/wd/hub";
# You can select another browser by changing the Capabilities
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox();# Launches a new Firefox instance on the TestGrid server
driver = Selenium::WebDriver.for(:remote, :url =>gridUrl, :desired_capabilities => capabilities)# Closes the remote browser
driver.quit
[/ruby] -
[python]
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesgridUrl = ‘http://TestGrid:4444/wd/hub’
# You can select another browser by changing the DesiredCapabilities
capabilities = DesiredCapabilities.FIREFOX# Launches a new Firefox instance on the TestGrid server
driver = webdriver.Remote(command_executor = gridUrl, desired_capabilities = capabilities)# Closes the remote browser
driver.quit()
[/python] -
[javascript]
async function Remote() {var webdriver = require(‘selenium-webdriver’)
var gridUrl = ‘http://localhost:4444/wd/hub’;
// You can select another browser by changing the DesiredCapabilities
var capabilities = webdriver.Capabilities.firefox();// Launches a new Firefox instance on the TestGrid server
var driver = new webdriver.Builder().withCapabilities(capabilities)
.usingServer(gridUrl)
.build();// Closes the remote browser
await driver.quit();
};Remote();
[/javascript]
Doing Page Navigation And Refresh
Now that we are able to open and close a browser instance we might as well navigate the browser to a page we would like to test. For this the driver object contains a Navigate method. Using this method you can open a new Url and refresh the current page. This example opens Chrome navigates and refreshes the page once:
-
[java]
// Add a reference to the Selenium client jar from
// http://www.seleniumhq.com
// import org.openqa.selenium.*;
// import org.openqa.selenium.chrome.ChromeDriver;
// Starts a new instance of Google Chrome
WebDriver driver = new ChromeDriver();// Open a webpage (method will return after the page is fully loaded)
driver.navigate().to("http://selenium.academy");// Refresh the page
driver.navigate().refresh();// Closes the running Google Chrome instance
driver.quit();
[/java] -
[csharp]
// Add a reference to the nuget package Selenium.Webdriver
// Add using OpenQA.Selenium;
// Add using OpenQA.Selenium.Chrome;// Starts a new instance of Google Chrome
IWebDriver driver = new ChromeDriver();// Open a webpage (method will return after the page is fully loaded)
driver.Navigate().GoToUrl("http://selenium.academy");// Refresh the page
driver.Navigate().Refresh();// Closes the running Google Chrome instance
driver.Quit();[/csharp]
-
[ruby]
require "selenium-webdriver"# Start a new instance of Google Chrome
chromeDriver = Selenium::WebDriver.for :chrome# Open a webpage (method will return after the page is fully loaded)
chromeDriver.navigate.to "http://selenium.academy"# Refresh the page
chromeDriver.navigate.refresh# close the Chrome instance
chromeDriver.quit
[/ruby] -
[python]
from selenium import webdriver# Starts a new instance of Google Chrome
driver = webdriver.Chrome()# Open a webpage (method will return after the page is fully loaded)
driver.get(‘http://selenium.academy’)# Refresh the page
driver.refresh()# Closes the running Google Chrome instance
driver.quit
[/python] -
[javascript]
async function Navigation() {
var webdriver = require(‘selenium-webdriver’)// Starts a new instance of Google Chrome
var driver = new webdriver.Builder().forBrowser(‘chrome’).build();// Open a webpage (method will return after the page is fully loaded)
await driver.navigate().to(‘http://selenium.academy’);// Refresh the page
await driver.navigate().refresh();// Closes the running Google Chrome instance
await driver.quit();
}Navigation();
[/javascript]
Navigate The Browser History
Sometimes your tests need to use the browser history to navigate back or forward:
-
[java]
// Add a reference to the Selenium client jar from
// http://www.seleniumhq.com
// import org.openqa.selenium.*;
// import org.openqa.selenium.chrome.ChromeDriver;
// Starts a new instance of Google Chrome
WebDriver driver = new ChromeDriver();// Open a webpage (method will return after the page is fully loaded)
driver.navigate().to("http://selenium.academy");// Open a webpage (method will return after the page is fully loaded)
driver.navigate().to("http://google.com");// Navigate back
driver.navigate().back();// Navigate forward
driver.navigate().forward();// Closes the running Google Chrome instance
driver.quit();
[/java] -
[csharp]
// Add a reference to the nuget package Selenium.Webdriver
// Add using OpenQA.Selenium;
// Add using OpenQA.Selenium.Chrome;// Starts a new instance of Google Chrome
IWebDriver driver = new ChromeDriver();// Open a webpage (method will return after the page is fully loaded)
driver.Navigate().GoToUrl("http://selenium.academy");// Open a webpage (method will return after the page is fully loaded)
driver.Navigate().GoToUrl("http://google.com");// Navigate back
driver.Navigate().Back();// Navigate forward
driver.Navigate().Forward();// Closes the running Google Chrome instance
driver.Quit();[/csharp]
-
[ruby]
require "selenium-webdriver"# Start a new instance of Google Chrome
chromeDriver = Selenium::WebDriver.for :chrome# Open a webpage (method will return after the page is fully loaded)
chromeDriver.navigate.to "http://selenium.academy"# Open a webpage (method will return after the page is fully loaded)
chromeDriver.navigate.to "http://google.com"# Navigate back
chromeDriver.navigate.back# Navigate forward
chromeDriver.navigate.forward# close the Chrome instance
chromeDriver.quit
[/ruby] -
[python]
from selenium import webdriver# Start a new instance of Google Chrome
chromeDriver = webdriver.Chrome()# Open a webpage (method will return after the page is fully loaded)
chromeDriver.get(‘http://selenium.academy’)# Open a webpage (method will return after the page is fully loaded)
chromeDriver.get(‘http://google.com’)# Navigate back
chromeDriver.back()# Navigate forward
chromeDriver.forward()# close the Chrome instance
chromeDriver.quit()
[/python] -
[javascript]
async function History()
{
var webdriver = require(‘selenium-webdriver’)// Starts a new instance of Google Chrome
var driver = new webdriver.Builder().forBrowser(‘chrome’).build();// Open a webpage (method will return after the page is fully loaded)
await driver.navigate().to(‘http://selenium.academy’);// Open a webpage (method will return after the page is fully loaded)
await driver.navigate().to(‘http://google.com’);// Navigate back
await driver.navigate().back();// Navigate forward
await driver.navigate().forward();// Closes the running Google Chrome instance
await driver.quit();
}History();
[/javascript]
Next Steps
Now you know how to launch, close and navigate a browser instance during the test run. Move along to the next lesson where we will cover how you can identify elements on the page (like buttons, links …). Identifying the elements is a key step to be able to interact with the page.