Understanding the JSON wire protocol                                                            

In many places, we have mentioned that WebDriver uses the JSON wire protocol to communicate between client libraries and different driver (that is, Chrome Driver, IE Driver, Gecko 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 become an industry standard for various REST web services, offering 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 then 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 into the JSON format and JSON-formatted data into an object is called serialization and de-serialization, respectively, which is quite common in REST-based web services.

WebDriver uses the same approach to communicate between client libraries (language bindings) and drivers, such as Firefox Driver, IE Driver, and Chrome Driver. Similarly, the RemoteWebDriver client and Selenium Standalone Server use the JSON wire protocol to communicate among themselves. But all of these drivers use it under the hood, hiding all the implementation details from us and making our lives simpler. The list of APIs for various actions that we can take on a web page 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 at https://code.google.com/p/selenium/wiki/JsonWireProtocol. The client libraries will translate your test-script commands into 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's see that as an example. Suppose your test script has this code: driver.get("http://www.google.com");.

The client library will translate that into JSON by building a JSON payload (JSON document) and post the request to the appropriate API. In this case, the API that handles the driver. get(URL) method is /session/:sessionId/url.

The following code shows what happens in the client library layer behind the scenes 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);

Selenium Standalone 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 the dataflow at each stage:

The preceding diagram shows the following:

  • The first stage is communication between the test script and the client library. The data or command that flows between them is a call to the get() method of the driver: driver.get("http://www.google.com");.
  • The client library, as soon as it receives the preceding command, will convert it into the JSON format and communicate with Selenium Standalone Server.
  • Next, Selenium Standalone Server forwards the JSON payload request to the Chrome Driver. 
  • The Chrome Driver will communicate with the Chrome browser natively, and then the browser will send a request for the asked URL to load.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.13.70