grvyard
grvyard

Reputation: 23

how to track ajax requests and responses through selenium webdriver?

I have a use-case where, I will fill the career form on the given site through selenium web-driver, after that when i will click on the submit button, I want to know, whether the form is successfully submitted or not. Basically there are 2 cases possible in this case,
a) site populate error through java-script on career page
b) front-end make a call to server in form of ajax request, and get some response.

Is there any way, by which i can identify, whether browser is making a ajax call on click or not, and if it is making ajax call, response status code of that call?

Upvotes: 2

Views: 4235

Answers (1)

tomasz.myszka
tomasz.myszka

Reputation: 111

In my project I'm using something like this:

object response = ((IJavaScriptExecutor) Driver.WebDriver).ExecuteAsyncScript(
            "var url = arguments[0];" +
            "var callback = arguments[arguments.length - 1];" +
            "var xhr = new XMLHttpRequest();" +
            "xhr.open('GET', url, true);" +
            "xhr.onreadystatechange = function() {" +
            "  if (xhr.readyState == 4) {" +
            "    callback(xhr.getAllResponseHeaders());" +
            "  }" +
            "};" +
            "xhr.send();", url);

You can change xhr.getAllResponseHeaders() to get all response not only header. After that you can serialize response, or even parse it and try to find text

Upvotes: 3

Related Questions