3

I have a question concerning Sinon testing. I am using mochacasper and I want to catch the XMLHttpRequest that is sent after I click a button. So far I am not able to catch the request...

casper.then(function () {
  this.fill(...);
});
casper.then(function() {
  this.click('#myButton');
});
casper.then(function () {
  //I want to catch the request/response here
});

Since I am using my testing environment database, I know that my request is sent, because my test data is added to my database. I have tried sinon's useFakeXMLHttpRequest, as well as sinon's fakeServer, but none is working.

So how can I catch the request?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Fabio
  • 103
  • 10
  • Not with SinonJS, but an own creation. Maybe it applies to your use case: [How can I catch and process the data from the XHR responses using casperjs?](http://stackoverflow.com/questions/24555370/how-can-i-catch-and-process-the-data-from-the-xhr-responses-using-casperjs) – Artjom B. May 05 '15 at 14:40

1 Answers1

0

I have developed a "feasible" result with Artjom B.'s comment. It is not exactly what I needed, but it is enough for the moment...

Here is how I did it:

casper.then(function () {
this.click('button[data-hook="submitButton"]');
});
casper.waitForResource(function (resource) {
 return resource.status === 201;
}, function () {
this.echo('Appointment has been created');
});

The only problem still is, that I cannot access the body of the response, but at least I can check if the status is correct.

Community
  • 1
  • 1
Fabio
  • 103
  • 10