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();
}
}
The browser driver implements Selenium commands that emulate all types of actions that users execute typically on a website:
- open a web page
- maximize the browser
- navigate through the browser history
- click buttons and links
- select values in lists and dropdown lists
- type text in textboxes
- get status of web elements
- 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()
EXAMPLE: http 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.
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
- 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
- the browser driver is included in the chromedriver.exe file
- download it from here
- INTERNET EXPLORER browser
- the browser driver is included in the internetexplorer.exe file
- download it from the Internet Explorer Driver Server section of the Selenium Downloads page
- FIREFOX browser
- 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:
- If the current top-level browsing context is no longer open, return error with error code no such window.
- Handle any user prompts and return its value if it is an error.
- Let title be the value of the current top-level browsing context’s active document’s title.
- Let data be a new JSON Object.
- Set the property “value” of data to the value of title.
- Return success with data data.
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
Awesome post, thanks for sharing…
LikeLike
You are welcome!
I am glad that you like it.
Alex
LikeLike
Selenium |Training|Job Support|+91-741-626-7887 Selenium a Web based automation tool that automates anything and everything available on a Web page. http://laymanlearning.com/selenium-training/ — Send Enquiry — hr@laymanlearning.com
LikeLike
Hi,
Do you have a specific comment about the article?
Thanks.
Alex
LikeLike
Amazing Post…! Thanks for publishing.
LikeLike
you are welcome!
I am glad that you like it.
LikeLike
Awesome…Sharing it as much as possible …This is must read for all Automation Experts.
LikeLike
Thanks
LikeLike
this is excellent! Thanks so much for writing this up!
LikeLike
thanks for for sharing this kind of content which regarding to selenium it help me too goood
LikeLike
[…] Selenium WebDriver Flow […]
LikeLike