I have written tests with Selenium2/WebDriver and want to test if HTTP Request returns an HTTP 403 Forbidden.
Is it possible to get the HTTP response status code with Selenium WebDriver?
I have written tests with Selenium2/WebDriver and want to test if HTTP Request returns an HTTP 403 Forbidden.
Is it possible to get the HTTP response status code with Selenium WebDriver?
You could try Mobilenium (https://github.com/rafpyprog/Mobilenium), a python package that binds BrowserMob Proxy and Selenium.
An usage example:
For those people using Python, you might consider Selenium Wire, a library I recently developed for inspecting requests on a work project.
The library is designed to be lightweight, simple to use with minimal external dependencies and configuration. It provides your tests with a Pythonic API for accessing headers, status code, body content, as well as the ability to modify headers and rewrite URLs.
Prints:
In a word, no. It's not possible using the Selenium WebDriver API. This has been discussed ad nauseum in the issue tracker for the project, and the feature will not be added to the API.
You can use BrowserMob proxy to capture the requests and responses with a
HttpRequestInterceptor
. Here is an example in Java:It is not possible to get HTTP Response code by using Selenium WebDriver directly. The code can be got by using Java code and that can be used in Selenium WebDriver.
To get HTTP Response code by java:
Now you can write your Selenium WebDriver code as below:
I was also having same issue and stuck for some days, but after some research i figured out that we can actually use chrome's "--remote-debugging-port" to intercept requests in conjunction with selenium web driver. Use following Pseudocode as a reference:-
create instance of chrome driver with remote debugging
make a get call to http://127.0.0.1:freePort
Extract chrome's webSocket Url to listen, you can see response and figure out how to extract
Connect to this socket, u can use asyncHttp
Enable network capture
Now chrome will send all network related events and captures them as follows
you can do everything mentioned in dev tools site. see https://chromedevtools.github.io/devtools-protocol/ Note:- use chromedriver 2.39 or above.
I hope it helps someone.
reference : Using Google Chrome remote debugging protocol