Selenium Webdriver is a browser automation tool, widely used to implement functional and acceptance tests. It supports a variety of browsers, including headless ones like PhantomJS.
But how easy is it to setup a project and run Selenium?
We’ll discover in this article. I prepared a full working project that you can clone from Github: selenium-windows-quickstart.
I will introduce binding for Java language.
Firefox is the easiest browser to set up. After installing it, the following example code will launch Firefox and open Selenium website:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; WebDriver driver = new FirefoxDriver(); driver.get("http://docs.seleniumhq.org/");
There is nothing else to do, Selenium will take care of everything.
Chrome requires a little bit of extra work. After installing the browser itself, you need to download a small executable called ChromeDriver. This is a bridge between Selenium java driver and Chrome itself.
Download and unzip the latest version in a folder on your hard drive. Next, you have to tell Selenium driver where the ChromeDriver is located by setting
webdriver.chrome.driver Java system property to ChromeDriver’s path.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; System.getProperties().put("webdriver.chrome.driver", "c:\\Users\\you\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://docs.seleniumhq.org/");
Selenium will start the ChromeDriver, connect to it and then control Chrome browser. The
webdriver.chrome.driver can also be set in command line using
-Dwebdriver.chrome.driver=c:\\Users\\you\\chromedriver.exe when invoking Java. In my quickstart project I set it in pom.xml and let Maven pass it to JVM.
The procedure for Opera is the same as Chrome: download the OperaDriver and unzip in the same folder as ChromeDriver. Set the
webdriver.opera.driver Java system property using your preferred way and you’re ready to go.
import org.openqa.selenium.opera.OperaDriver; System.getProperties().put("webdriver.opera.driver", "c:\\Users\\you\\operadriver.exe"); WebDriver driver = new OperaDriver();
Internet Explorer is no different from the other browsers this time. Download and unzip its IEDriverServer, set
webdriver.ie.driver, instantiate the InternetExplorerDriver in your code.
import org.openqa.selenium.ie.InternetExplorerDriver; System.getProperties().put("webdriver.ie.driver", "c:\\Users\\you\\IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver();
HtmlUnit is a special kind of browser: it is “headless”, meaning that it uses no screen. It’s purspose is to be runned on machines that have no GUI (like most Linux servers) or no users logged in.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; WebDriver driver = new HtmlUnitDriver(true); driver.get("http://docs.seleniumhq.org/");
Setting up Selenium with Java binding requires only a few lines of code. You can clone this selenium-windows-quickstart project and start driving your browser of choice!
Don’t like Java? Python quickstart is coming soon! =)