Skip to content

Selenium WebDriver

Selenium WebDriver is a code library that allows us to automate browser interactions.  It is the most preferred code library for UI test automation for web applications.

In this code note, we will cover following topics:

  • WebDriver Architecture
  • Connecting to Browsers
  • Changing Window Size
  • Visiting Specific Site



› WebDriver Architecture

All Selenium code you write will be sent to the driver software, where the conversion of your Java code into a command an actual browser understands takes place.  The way Selenium code interacts with the actual browser has changed with the latest library releases.

Selenium WebDriver 3

This version of Selenium uses JSON Wire Protocol to interact with drivers.  JSON Wire Protocol specifies how the automation Java code ( clicking, typing, etc.) should be relayed to the driver software.

Selenium WebDriver 4

With the latest version 4, your UI automation code can directly communicate with the drivers without using the bulky and slow JSON Wire Protocol.  As a result, automation code with version 4 is considerably faster.

Establishing Browser Connection

To send any automation commands to the actual browser, you must have the driver executable up and running with your code. Luckily for us, just making a WebDriver object will establish the connection between your Selenium code and the running driver software.

1
2
3
4
5
6
7
// 1) Install the driver software and make it up and running
WebDriverManager.chromedriver().setup();

// 2) Connect to the running chrome driver by making a WebDriver object.
//    This code will establish the Code -> Driver -> Browser connections.
//    Once the connection is established, it will open a blank browser window.
WebDriver driver = new ChromeDriver();

Now you are ready to write browser automation code using the driver interface variable.

Connecting to other Browsers

Here is a sample code for connecting to other browsers

1
2
3
4
5
6
7
// Connecting & Opening up Firefox browser 
WebDriverManager.firefoxdriver().setup();
WebDriver driver = new FirefoxDriver();

// Connecting & Opening up Microsoft Edge browser
WebDriverManager.edgedriver().setup();
WebDriver driver = new EdgeDriver();

Connecting to the browser with headless mode

The automation script must run as fast as possible when executing the test on a dedicated server. Every browser has a headless mode where the browser's display is turned off. This mode will make the browser hyper-fast and stable.

1
2
3
4
5
6
7
8
9
WebDriverManager.chromedriver().setup();

// Option object will allow us to create browser option
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");

// Note that we are injecting the options object to the 
// ChromDriver constructor method. 
WebDriver driver = new ChromeDriver(options);



WebDriver interface

All Selenium code you write must pass through the running driver software to reach the actual browser. Each browser has its corresponding driver software that sends automation commands to the actual browser. Selenium allows us to work with this driver software through its WebDriver interface.

This WebDriver interface has the following methods:

Method Detail Description
void close() Close the current window, quitting the browser if it's the last window currently open.
WebElement findElement(By by) Find the first WebElement using the given method.
List<WebElement> findElements(By by) Find all elements within the current page using the given mechanism.
void get(String url) Load a new web page in the current browser window.
String getCurrentUrl() Get a string representing the current URL that the browser is looking at.
String getPageSource() Get the source of the last loaded page.
String getTitle() Get the title of the current page.
String getWindowHandle() Return an opaque handle to this window that uniquely identifies it within this driver instance.
Set<String> getWindowHandles() Return a set of window handles which can be used to iterate over all open windows of this WebDriver
Options manage() Gets the Option interface
Navigation navigate() An abstraction allowing the driver to access the browser's history and to navigate to a given URL.
void quit() Quits this driver, closing every associated window.
TargetLocator switchTo() Send future commands to a different frame or window.

All of this method can be used for all the browsers. Let's take a look at usage example of a couple of these methods.



› Configuring Driver

Sometimes the default size of the browser window is not adequate for the screenshots. Selenium gives us various ways to determine the size of the browser window.

Opening Browser with Full-Screen Mode

1
2
3
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().fullscreen();

Opening Browser with Max Size ( screen fit ) Mode

1
2
3
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

Opening Browser with Specific Screen Size

1
2
3
4
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
Dimension ipad = new Dimension(700, 900);
driver.manage().window().setSize(ipad);  // browser window size is 700x900 pixels

Opening Browser with headless mode

Every browser has a headless mode where the browser will not be rendered to the user.  The browser and website are there and displayed, but they are not rendered to the user.  The headless mode is designed for processes or programs that use the browser.  With headless mode, the browser works faster, and almost all of our scheduled test execution is carried out with the headless model enabled.

1
2
3
4
5
WebDriverManager.chromedriver().setup();

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver browser = new ChromeDriver(options);



› Visiting Specific Site

Once the connections between your Selenium code, driver, and browser has been established. Then, we can start browser automation through the WebDriver interface.

For example, we can tell the browser to visit a specific webpage with the following two driver methods.

get( String url )

1
2
3
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://trello.com");

navigate( ).to( String url )

1
2
3
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://trello.com");

Please make sure to use get(String url) for visiting the webpage with Selenium. The difference between these two methods are as follows:

  • get(String url) method visits the specified page and it will wait until the entire webpage is loaded.
  • navigate().to(String url) method visits the webpage and it does not wait for page load.



› Summary

Please watch the following video for the summary of all the topics presented in this code note. If you want to follow along with the instructor, please clone the following template project.

GitHub Repo: AlphaleafTech/note_selenium

[email protected]:AlphaleafTech/note_selenium.git

Please watch the video with 1.25 playback speed

Note Summary Video