Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring JSON wire protocol logs

According to the selenium documentation, interactions between the webdriver client and a browser is done via JSON Wire Protocol. Basically the client, written in python, ruby, java whatever, sends JSON messages to the web browser and the web browser responds with JSON too.

Is there a way to view/catch/log these JSON messages while running a selenium test?

For example (in Python):

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://google.com')

driver.close()

I want to see what JSON messages are going between the python selenium webdriver client and a browser when I instantiate the driver (in this case Chrome): webdriver.Chrome(), when I'm getting a page: driver.get('http://google.com') and when I'm closing it: driver.close().

FYI, in the #SFSE: Stripping Down Remote WebDriver tutorial, it is done via capturing the network traffic between the local machine where the script is running and the remote selenium server.

I'm tagging the question as Python specific, but really would be happy with any pointers.

like image 888
alecxe Avatar asked Sep 12 '14 16:09

alecxe


People also ask

Is JSON a wire protocol?

All implementations of WebDriver that communicate with the browser, or a RemoteWebDriver server shall use a common wire protocol. This wire protocol defines a RESTful web service using JSON over HTTP.

How is JSON sent over the wire?

Json Wire Protocol: A client has an object that has to be sent to a server. The client converts this object into a JSON object and sends it to the server. The server parses the JSON object and converts it back to object for use. The server converts the response object into a JSON object and sends it back to the client.

How does JSON wire protocol work?

The JSON wire protocol is a transport mechanism. We can interact with it using a REST API. We can control different browsers/mobile devices and their behavior over a session. JSON is used to represent objects with complex data structures.

What is WebDriver wire protocol?

WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol as a way for out-of-process programs to remotely instruct the behavior of web browsers.


1 Answers

When you use Chrome you can direct the chromedriver instance that will drive Chrome to log more information than what is available through the logging package. This information includes the commands sent to the browser and the responses it gets. Here's an example:

from selenium import webdriver

driver = webdriver.Chrome(service_log_path="/tmp/log")
driver.get("http://www.google.com")
driver.find_element_by_css_selector("input")
driver.quit()

The code above will output the log to /tmp/log. The part of the log that corresponds to the find_element_... call looks like this:

[2.389][INFO]: COMMAND FindElement {
   "sessionId": "b6707ee92a3261e1dc33a53514490663",
   "using": "css selector",
   "value": "input"
}
[2.389][INFO]: Waiting for pending navigations...
[2.389][INFO]: Done waiting for pending navigations
[2.398][INFO]: Waiting for pending navigations...
[2.398][INFO]: Done waiting for pending navigations
[2.398][INFO]: RESPONSE FindElement {
   "ELEMENT": "0.3367185448296368-1"
}

As far as I know, the commands and responses faithfully represent what is going on between the client and the server. I've submitted bug reports and fixes to the Selenium project on the basis of what I saw in these logs.

like image 51
Louis Avatar answered Nov 15 '22 23:11

Louis