All this while, in many places, we have mentioned that WebDriver uses the JSON wire protocol to communicate between client libraries and different drivers (that is, Firefox Driver, IE Driver, Chrome Driver, and so on) implementations. In this section, we will see exactly what it is and which different JSON APIs a client library should implement to talk to the drivers.
JavaScript Object Notation (JSON) is used to represent objects with complex data structures. It is used primarily to transfer data between a server and a client on the web. It has very much become an industry standard for various REST web services, playing a strong alternative to XML.
A sample JSON file, saved as a
.json
file, will look as follows:{ "firstname": "John", "lastname": "Doe", "address": { "streetnumber":"678", "street":"Victoria Street", "city":"Richmond", "state":"Victoria", "country":"Australia" } "phone":"+61470315430" }
A client can send a person's details to a server in the preceding JSON format, which the server can parse and create an instance of the
Person
object for use in its execution. Later, the response can be sent back by the server to the client in the JSON format, the data of which the client can use to create an object of a class. This process of converting an object's data to the JSON format and JSON-formatted data to an object is named serialization and de-serialization, respectively, which is quite common in REST web services these days.
Our WebDriver uses the same approach to communicate between client libraries (language bindings) and drivers, such as Firefox Driver, IE Driver, Chrome Driver, and so on. Similarly, the
RemoteWebDriver
client and the RemoteWebDriver
server use the JSON wire protocol to communicate among themselves. But, all of these drivers use it under the hood, hiding all of the implementation details from us and making our lives simpler. For any existing or new client library, they should provide implementations for building all of the WebDriver JSON APIs, and any existing or new WebDriver should handle these requests and provide implementations for them. The list of APIs for various actions that we can take on a webpage is as follows:/status /session /sessions /session/:sessionId /session/:sessionId/timeouts /session/:sessionId/timeouts/async_script /session/:sessionId/timeouts/implicit_wait /session/:sessionId/window_handle /session/:sessionId/window_handles /session/:sessionId/url /session/:sessionId/forward /session/:sessionId/back /session/:sessionId/refresh /session/:sessionId/execute /session/:sessionId/execute_async /session/:sessionId/screenshot /session/:sessionId/ime/available_engines /session/:sessionId/ime/active_engine . . . . . . /session/:sessionId/touch/flick /session/:sessionId/touch/flick /session/:sessionId/location /session/:sessionId/local_storage /session/:sessionId/local_storage/key/:key /session/:sessionId/local_storage/size /session/:sessionId/session_storage /session/:sessionId/session_storage/key/:key /session/:sessionId/session_storage/size /session/:sessionId/log /session/:sessionId/log/types /session/:sessionId/application_cache/status
The complete documentation is available athttps://code.google.com/p/selenium/wiki/JsonWireProtocol.
The client libraries will translate your test script commands to the JSON format and send the requests to the appropriate WebDriver API. The WebDriver will parse these requests and take necessary actions on the web page.
Let us see that with an example. Suppose your test script has a the following code:
driver.get("http://www.google.com");
The client library will translate that to JSON by building a JSON payload and post the request to the appropriate API. In this case, the API that handles the
driver.get(URL)
method is as follows:/session/:sessionId/url
The following code shows what happens in the client library layer before the request is sent to the driver; the request is sent to the
RemoteWebDriver
server running on 10.172.10.1:4444
:HttpClient httpClient = new DefaultHttpClient(); HttpPost postMethod = new HttpPost("http://10.172.10.1:4444/wd/hub/session/"+sessionId+"/url"); JSONObject jo=new JSONObject(); jo.put("url","http://www.google.com"); StringEntity input = new StringEntity(jo.toString()); input.setContentEncoding("UTF-8"); input.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); postMethod.setEntity(input); HttpResponse response = httpClient.execute(postMethod);
The
RemoteWebDriver
server will forward that request to the driver; the driver will execute the test script commands that arrive in the preceding format on the web application under the test that is loaded in the browser.
The following diagram shows what data flows at each stage:

The following table shows which command is executed at each stage:
Stage in the preceding diagram
|
Command executed at that stage
|
---|---|
a
| driver.get("http://www.google.com"); |
b
| "http://10.172.10.1:4444/wd/hub/session/"+sessionId+"/url" { "url": "http://www.google.com" } |
c
| "http://localhost:7705/ { "url": "http://www.google.com" } |
Native Code
|
Talks natively to the browser
|
d
| "http://www.google.com" |
In the previous diagram, the first stage is communication between your test script and client library. The data or command that flows between them is represented as a in the image; a is nothing but the following code:
driver.get("http://www.google.com");
The client library, as soon as it receives the preceding command, will convert it to the JSON format and communicate with the
RemoteWebDriver
server, which is represented as b.
Next, the
RemoteWebDriver
server forwards the JSON payload request to the Firefox Driver (in this case), and the data that flows through is represented as c.
Firefox Driver will speak to the Firefox browser natively, and then the browser will send a request for the asked URL to load, which is represented as d.
No comments:
Post a Comment