We've encountered some issues with our e2e tests. We tried checking if clicks on specific links behave as they should (which also meant, send a POST XHR request to a tracking server).
Since we couldn't find any package for that, we wrote one.
So - this package waits for XHR to complete and enables a callback with its values for assertion.
npm install nightwatch-xhr
yarn add nightwatch-xhr
In order for your project to be able to access these commands and assertions you need to include them in your projects nightwatch config.
... "custom_commands_path": ["./node_modules/nightwatch-xhr/src/commands"], "custom_assertions_path": ["./node_modules/nightwatch-xhr/src/assertions"], ...
... "custom_commands_path": ["./node_modules/nightwatch-xhr/es5/commands"], "custom_assertions_path": ["./node_modules/nightwatch-xhr/es5/assertions"], ...
trigger, waits for a
delay to complete, and then calls
callback with an array of all xhr requests corresponding to the given
trigger, and then calls
callback with the first xhr request corresponding to the given
urlPattern, failing if
timeout is exceeded.
Only set up listening. If it has already been called, it resets the requests list.
callback with all requests corresponding to the given
urlPattern. If given
delay waits that time before failing.
The function expects these parameters:
- urlPattern - a regex match for url pattern, will only listen to urls matching this, use '' for all urls.
- timeout - well, timeout
- trigger - activate a trigger in the browser after initiating the listener
- callback - use this to assert the request after it completes
waitForFirstXHR Without Trigger:
waitForXHR With Click Trigger:
"Listening" to a specific URL or Regex
The callback function returns an object containing the following properties :
- status (success/error/timeout)
- method (GET/POST)
- url (url of request)
- requestData (raw POSTed data)
- httpResponseCode (HTTP status response code in string, eg: "200", )
- responseData (raw response data)
When the anticipated XHR request has not occurred, it fails an assertion. Callback is not called.
With listenXHR / getXHR
With listenXHR / getXHR and some delay
Thanks goes to these wonderful people (emoji key):
💻 📖 💡 👀
💻 📖 💡 👀 ⚠️ 🔧
This project follows the all-contributors specification. Contributions of any kind welcome!