Skip to content

Now that you’ve got a sense of what Selenium framework is and why you need it. We’ll naturally start with the basic things to help you set up Selenium testing environment and get started with the framework. Depending on which programming language you are using, we’ve provided everything here, step-by-step, for those 4 main languages.

To execute Selenium WebDriver scripts, you need to ensure that you have the initial configuration done. Setting up the environment involves the following steps that we will go into below. Some of the tools you might have already installed.

The WebDriver consists of three separate parts, take Chrome as an example, the browser itself (“i.e. Chrome“), the language bindings provided (“the Driver”) and an executable ChromeDriver downloaded from the Chromium project which acts as a bridge between “chrome” and the “driver”. All of them will be discussed later.

Setting Up the Environment

To make use of the existing samples you should have Git installed. If you are new to Git, there is a very good guide on how to install Git on a popular operating system here. Below is an abbreviated version of the installation procedures.

For macOS

# Download the latest git command line tool from 
# http://git-scm.com/download/mac

For Linux

$ sudo apt-get install git

For Windows

# The eaisert way is to install the Github Windows application.

Programming Languages for Selenium Testing

As mentioned above, how you get started with and install Selenium depends on your programming language and your development environment. Here we won’t go into how the different languages are installed, as you probably are already familiar with your language of choice and have it installed. Just jump to the section you are interested in. If you are new to all of this, you can check out the install procedures for the languages here.

Python

To add Selenium to your Python environment, run the following command from a command line. Selenium is in this example installed with Pip, package-management system for Python.

For Mac OS X

$ sudo easy_install pip3

For Linux

$ sudo apt-get install python3-pip

For Windows

# it is included in the newer versions of Python
# to check your version write “python” in CMD

or

# https://bootstrap.pypa.io/get-pip.py
# Then use command ‘python get-pip.py’ to install pip

Now moving onto installing Selenium itself.

For macOS and Linux

# Installing Selenium for Python
$ pip3 install selenium

On Windows CMD (with Python 3.5)

C:\Python35\Scripts\pip.exe install selenium

Java

An easy way of setting up a Java Selenium project is to use Maven. Maven will download the java bindings (the Selenium Java client library) and all its dependencies and it will create the project using a maven pom.xml (project configuration) file. After that, you are able to import the maven project into your IDE of choice, IntelliJ IDEA or Eclipse.

Alternatively, you can extract the .zip file you downloaded from the URL below, and add the Selenium Java bindings (a JAR file) and add all the dependent libraries (JAR files in the /libs folder) to your classpath.

In Eclipse, right click project -> Properties -> Java Build Path -> Classpath -> add external JARs

Click here to download the Selenium Client Language Bindings for Java.

C#

Here we look at how we get Selenium installed for Visual Studio and C#. Add the Selenium references in your selected C# project.

  1. Open the Solution Explorer
  2. Expand the project
  3. References -> select Manage Nuget Packages
  4. Search for the Selenium.WebDriver and install it

The above references can provide all classes and properties required to integrate the Selenium WebDrivers. If you wish to use other browsers such as Internet Explorer or Firefox, then you’ll need to install those drivers as well.

The C# bindings are distributed as part of a set of signed DLLs and dependency DLLs. Then, you’ll need to add a reference to each unzipped DLL to your project in whatever IDE you’re using.

Or you can manually download the Selenium Client Language Bindings for C# by clicking here.

Ruby

To add Selenium to your Ruby environment, run the following command from a command line. Here we use RubyGems, a sophisticated package manager for Ruby. This gem provides Ruby bindings for WebDriver. That’s it.

Installing Selenium on Ruby

$ gem install selenium-webdriver

Click here to download the Selenium Client Language Bindings for Ruby.

Installing WebDriver

Now when we have installed Selenium, it’s time to install the WebDriver. Selenium requires a driver to interface with the chosen browser (yes, you also need the specific browser). As this is a separate component released by the browser manufacturers themselves. Chrome, for example, requires ChromeDriver which needs to be installed before the example below can be run.

Please make sure that this is available on your $PATH (or %PATH% on Windows) in order for the Driver to work as expected.

macOS and Linux

# Adding to $Path and path file
$ echo "export PATH=$PATH:$HOME/bin" >> $HOME/.bash_profile

Windows

Open the Start Search, type in “env”, and choose “Edit the system environment variables”:
Then Environment Variables… -> Edit Path -> add you variable

Other browsers have their own drivers, here are links to the most popular once (if manually downloaded path needs to be set).

Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox: https://github.com/mozilla/geckodriver/releases
Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/

These can also be installed via terminal.

Mac users with Homebrew installed:

brew tap homebrew/cask && brew cask install chromedriver
# this installs chromedriver in $HOME/bin
# Or you can manually download download chromedriver and move it with
$ mv chromedriver $HOME/bin
# Adding to $Path
$ echo "export PATH=$PATH:$HOME/bin" >> $HOME/.bash_profile

Debian based Linux distributions:

sudo apt-get install chromium-chromedriver
Or via NPM: $ sudo npm -g install chromedriver

Windows users with Chocolatey installed:

choco install chromedriver
Download Chromedriver or other driver and put it in a specific folder,
Remember to add this folder, i.e. “C:\webdrivers” to PATH as a new environment variable.

Sample Selenium Tests

Here we’ve also prepared some very simple tests to get you started with the framework and see if everything works fine for more advanced Selenium testing later!

Run either of the following Python or Java scripts to verify that Selenium and ChromeDriver installed correctly. If your Chrome Browser is opened and shortly after closed and the printout in Terminal reads “Unleash the Power of Bitbar for Your Enterprise”, then congratulations, you have successfully installed Selenium!

Python Sample

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.bitbar.com/enterprise/")
line = driver.find_elements_by_css_selector(".b-cta__content > h2:nth-child(1)")
for line in line:
print(line.text.strip())
driver.quit()

Java Sample

package SimpleJavaSelenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class test {

public static void main(String[] args) {

WebDriver webDriver = new ChromeDriver();
webDriver.navigate().to("http://www.bitbar.com/enterprise");

WebElement element = webDriver.findElement(By.cssSelector(".b-cta__content > h2:nth-child(1)"));
System.out.println(element.getText());

webDriver.quit();
}
}

Ruby Sample

require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome

driver.navigate.to "http://www.bitbar.com/enterprise"
puts driver.find_element(:css, ('.b-cta__content > h2:nth-child(1)')).text

driver.quit

In the next chapter, we will dive into the topic of writing your first test. Stay tuned and until next time!

Image by lmonk72 via [source link] (copyright-free)

Wille Kreutzman

Solution Consultant