JSON protocol protocol monitoring

According to the documentation for selenium, the interaction between the webdriver client and the browser is via the JSON Wire Protocol . Basically, a client written in python, ruby, java sends JSON messages to a web browser, and the web browser also responds to JSON.

Is there a way to view / catch / write these JSON messages during selenium testing?

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 go between the pelon selenium webdriver client and the browser when creating the driver instance (in this case Chrome): webdriver.Chrome() when I get the page: driver.get('http://google.com') and when I close it: driver.close() .

FYI, in the tutorial #SFSE: Stripping Down Remote WebDriver, this is done by capturing network traffic between the local computer on which the script is running and the remote selenium server.

I put the question as Python specific, but would really be pleased with any pointer.

+6
source share
2 answers

When you use Chrome, you can direct the chromedriver instance that will control Chrome to log more information than what is available through the logging package. This information includes commands sent to the browser and the responses it receives. Here is 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 above code will print the log to /tmp/log . The part of the log corresponding to the call to find_element_... 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, commands and responses accurately represent what happens between the client and the server. I submitted bug reports and corrections for the Selenium project based on what I saw in these logs.

+4
source

One option was found that almost fits my needs.

Simple linking the registrar with stdout allows you to see the basic requests made:

 import logging import sys from selenium import webdriver # pipe logs to stdout logger = logging.getLogger() logger.addHandler(logging.StreamHandler(sys.stdout)) logger.setLevel(logging.NOTSET) # selenium specific code driver = webdriver.Chrome() driver.get('http://google.com') driver.close() 

He prints:

 POST http://127.0.0.1:56668/session {"desiredCapabilities": {"platform": "ANY", "browserName": "chrome", "version": "", "javascriptEnabled": true, "chromeOptions": {"args": [], "extensions": []}}} Finished Request POST http://127.0.0.1:56668/session/5b6875595143b0b9993ed4f66f1f19fc/url {"url": "http://google.com", "sessionId": "5b6875595143b0b9993ed4f66f1f19fc"} Finished Request DELETE http://127.0.0.1:56668/session/5b6875595143b0b9993ed4f66f1f19fc/window {"sessionId": "5b6875595143b0b9993ed4f66f1f19fc"} Finished Request 

I do not see the answers, but this is already progress.

+4
source

Source: https://habr.com/ru/post/975198/


All Articles