Search This Blog

Tuesday, October 7, 2014

Understanding the JSON wire protocol

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, theRemoteWebDriver 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:
Understanding the JSON wire protocol
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.

Tuesday, September 9, 2014

The Power of Imagination

Imagination is the ability to form a mental image of something that is not perceived through the five senses. It is the ability of the mind to build mental scenes, objects or events that do not exist, are not present, or have happened in the past.
Everyone possesses a certain of imagination ability. In some, it is highly developed, and in others, it manifests in a weaker form. Imagination manifests in various degrees in various people.
Imagination makes it possible to experience a whole world inside the mind. It gives the ability to look at any situation from a different point of view, and to mentally explore the past and the future.
This ability manifests in various forms, one of which is daydreaming. Though too much idle daydreaming may make one impractical, a certain degree daydreaming, while not being engaged in something that requires attention, provides temporary happiness, calmness and relief from stress.
In your imagination, you can travel anywhere in the speed of light, without any obstacles. It can make you feel free, though temporarily, and only in the mind, from tasks, difficulties and unpleasant circumstances.
Imagination is not limited only to seeing pictures in the mind. It includes all the five senses and the feelings. One can imagine a sound, taste, smell, a physical sensation or a feeling or emotion. For some people it is easier to see mental pictures, others find it easier to imagine a feeling, and some are more comfortable imagining the sensation of one of the five senses. Training of the imagination gives the ability to combine all the senses.
A developed and strong imagination does not make you a daydreamer and impractical. On the contrary, it strengthens your creative abilities, and is a great tool for recreating and remodeling your world and life.
This is a great power that can change your whole life. It is used extensively in magick, creative visualization and affirmations. It is the creator of circumstances and events. When you know how to work with it, you can make your hearts' desires come true.
Imagination has a great role and value in each one's life. It is much more than just idle daydreaming. We all use it, whether consciously or unconsciously, in most of our daily affairs. We use our imagination whenever we plan a party, a trip, our work or a meeting. We use it when we describe an event, explain how to arrive to a certain street, write, tell a story or cook a cake.
Imagination is a creative power that is necessary for inventing an instrument, designing a dress or a house, painting a picture or writing a book. The creative power of imagination has an important role in the achievement of success in any field. What we imagine with faith and feelings comes into being. It is the important ingredient of creative visualization, positive thinking and affirmations.
Visualizing an object or a situation, and repeating often this mental image, attracts the object or situation we voisualize into our lives. This opens for us new, vast and fascinating opportunities.
This means that we should think only in a positive manner about our desires, otherwise, we might attract into our lives events, situations and people that we don't really want. This is actually what most of us do, because we don't use the power of imagination correctly.
If you do not recognize the importance of the power of the imagination, and let it run riot, your life may not be as happy and successful as you would have wanted it to be.
Lack of understanding of the power of the imagination is responsible for the suffering, incompetence, difficulties, failures and unhappiness people experience. For some reason, most people are inclined to think in a negative way. They do not expect success. They expect the worst, and when they fail, they believe that fate is against them. This attitude can be changed, and then life will improve accordingly.
Understanding, how to use your imagination correctly, and putting this knowledge into practice, for your own and others' benefit, will put you on the golden path to success, satisfaction and happiness.

My Profile

My photo
can be reached at 09916017317