WebDriver emulates user actions on a page.

When the automation script is executed, the following steps happen:

  • for each Selenium command, a HTTP request is created and sent to the browser driver
  • the browser driver uses a HTTP server for getting the HTTP requests
  • the HTTP server determines the steps needed for implementing the Selenium command 
  • the implementation steps are executed on the browser
  • the execution status is sent back to the HTTP server
  • the HTTP server sends the status back to the automation script

 

Selenium Client & WebDriver Language Bindings


Multiple languages are available for writing test automation scripts using the Selenium WebDriver framework.

The most popular are Java, Ruby, C# and Python.

Test automation scripts use Selenium commands for interacting with the elements of a site:

 

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();

// And now use this to visit Google
driver.get(http://www.google.com”);
// Alternatively the same thing can be done like this
// driver.navigate().to(“http://www.google.com”);

// Find the text input element by its name
WebElement element = driver.findElement(By.name(“q”));

// Enter something to search for
element.sendKeys(“Cheese!”);

// Now submit the form. WebDriver will find the form for us from the element
element.submit();

// Check the title of the page
System.out.println(“Page title is: “ + driver.getTitle());

// Google’s search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith(“cheese!”);
}
});

// Should see: “cheese! – Google Search”
System.out.println(“Page title is: “ + driver.getTitle());

//Close the browser
driver.quit();
}
}

 


selenium free course

 

The browser driver implements Selenium commands that emulate all types of actions that users execute typically on a website:

  1. open a web page
  2. maximize the browser
  3. navigate through the browser history
  4. click buttons and links
  5. select values in lists and dropdown lists
  6. type text in textboxes
  7. get status of web elements
  8. get information of web elements (value, attributes, position)

The browser driver is the same regardless of the language used for automation.

So, in order to use the browser driver from any language, a language binding was created for each language so that

  • If you want to use the browser driver in Java, use the Java bindings for Selenium WebDriver
  • If you want to use the driver in C#, Ruby or Python, use the binding for that language

All language bindings are available for download on the Selenium HQ site on the Selenium WebDriver & Client Language Bindings section.

For Java, the bindings are included in the selenium-java-2.47.1.zip file:

selenium-java-2.47.1.jar (Java bindings)
selenium-java-2.47.1-srcs.jar

 

HTTP request is created for each Selenium command

 

For each Selenium command of the automation script, a http request with a specific path is created.

 

 

When the automation script is executed, the first http request generates a new session that is specific to the browser where the automation scripts run.

The session id will be used for the http requests that correspond to all other Selenium commands from the automation script.

The details of the Create New Session http request are below:

POST /session

Create a new session. The server should attempt to create a session that most closely matches the desired and required capabilities. Required capabilities have higher priority than desired capabilities and must be set for the session to be created.
JSON Parameters:
desiredCapabilities – {object} An object describing the session’s desired capabilities.
requiredCapabilities – {object} An object describing the session’s required capabilities (Optional).
Returns:
{object} An object describing the session’s capabilities.
Potential Errors:
SessionNotCreatedException – If a required capability could not be set.

 

1. HTTP REQUEST TYPES

HTTP requests are GET or POST requests

  • GET requests

    Get requests are generated usually for Selenium interrogation commands (commands that get information from web elements) such as

    • getText()
    • getAttribute()
    • isDisplayed()
    • isSelected()

 

EXAMPLEhttp request for checking if an element is displayed:

URL
GET /session/:sessionId/element/:id/displayed

Purpose
Determine if an element is currently displayed.

URL Parameters
:sessionId – ID of the session to route the command to.
:id – ID of the element to route the command to.

Returns
{boolean} Whether the element is displayed.

 

  • POST requests 

    Post requests are generated usually for Selenium manipulation commands (commands that interact with web elements) such as

    • get()
    • findElement()
    • findElements()
    • click()

EXAMPLE: http request for clicking an element:

URL
POST /session/:sessionId/element/:id/click

Purpose
Click on an element.

URL Parameters
:sessionId – ID of the session to route the command to.
:id – ID of the element to route the command to.

The communication between the client (computer that runs the test automation scripts) and the browser driver uses the HTTP and JSON wire protocol.

 


selenium free course

2. HTTP REQUEST PARAMETERS

The HTTP requests use the following types of parameters:

  • URL parameters

EXAMPLE: http request for clicking an element

URL
POST /session/:sessionId/element/:id/click

URL Parameters
:sessionId – ID of the session to route the command to.
:id – ID of the element to route the command to.

  • JSON parameters

EXAMPLE: http request for finding an element

URL
POST /session/:sessionId/element

URL Parameters
:sessionId – ID of the session to route the command to.

JSON Parameters
using – {string} The locator strategy to use.
value – {string} The The search target.

 

3. HTTP REQUEST RESULT

The result of a HTTP request can be:
  • a value

EXAMPLE: http request for getting the title of the current page

URL
GET /session/:sessionId/title

URL Parameters
:sessionId – ID of the session to route the command to.

Returns
{string} The current page title.

  • a JSON object

EXAMPLE: http request for finding an element

URL
POST /session/:sessionId/element

URL Parameters
:sessionId – ID of the session to route the command to.

JSON Parameters
using – {string} The locator strategy to use.
value – {string} The The search target.

Returns
{ELEMENT:string} A WebElement JSON object for the located element.

 

Browser Driver

 

The browser driver is implemented in a EXE file or a browser extension:

  • CHROME browser
    1. the browser driver is included in the chromedriver.exe file
    2. download it from here
  • INTERNET EXPLORER browser
    1. the browser driver is included in the internetexplorer.exe file
    2. download it from the Internet Explorer Driver Server section of the Selenium Downloads page
  • FIREFOX browser
    1. the browser driver is a firefox extension included in the Selenium Client & WebDriver Language binding file

 

 

The browser driver uses an HTTP SERVER which waits continuously for new Selenium commands.

It has the following purposes:

  • read HTTP requests coming from the client (client = computer that executes the test automation scripts)
  • determines the series of steps needed for implementing the Selenium command
  • sends the implementation steps to the browser
  • gets the execution status from the browser
  • send the execution status back to the client

For example, see below the implementation steps for the http request that corresponds to the getTitle() Selenium WebDriver command:

GET /session/{session id}/title

The Get Title command returns the document title of the current top-level browsing context, equivalent to calling window.top.document.title.

The remote end steps are:

  1. If the current top-level browsing context is no longer open, return error with error code no such window.
  2. Handle any user prompts and return its value if it is an error.
  3. Let title be the value of the current top-level browsing context’s active document’s title.
  4. Let data be a new JSON Object.
  5. Set the property “value” of data to the value of title.
  6. Return success with data data.

 


selenium free course

So, the process of executing the test scripts consists in


1. write the code in the programming language

2. use the language binding for including Selenium commands in the test scripts

3. the test automation script execution is started

4. the browser driver is created

5. the HTTP server starts listening for Selenium commands HTTP requests

6. for each Selenium command from the test script, a HTTP request is created

7. the HTTP request is sent to the HTTP server

8. the HTTP server determines the steps needed for implementing the Selenium command

9. the implementation steps are sent to the browser 

10. as soon as the execution status is returned from the browser, the HTTP server send the execution status back to the test script

11 Comments

Leave a comment