npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webdriverajax-extra

v2.0.6

Published

Capture and assert HTTP ajax calls in webdriver.io 🕸

Downloads

8

Readme

webdriverajax

Build Status Join the chat at https://gitter.im/chmanie/webdriverajax Capture and assert HTTP ajax calls in webdriver.io

This is a plugin for webdriver.io. If you don't know it yet, check it out, it's pretty cool.

Although selenium and webdriver are used for e2e and especially UI testing, you might want to assess HTTP requests done by your client code (e.g. when you don't have immediate UI feedback, like in metrics or tracking calls). With webdriverajax you can intercept ajax HTTP calls initiated by some user action (e.g. a button press, etc.) and make assertions about the request and corresponding resposes later.

There's one catch though: you can't intercept HTTP calls that are initiated on page load (like in most SPAs), as it requires some setup work that can only be done after the page is loaded (due to limitations in selenium). That means you can just capture requests that were initiated inside a test. If you're fine with that, this plugin might be for you, so read on.

Prerequisites

  • webdriver.io v4.x.

Heads up! If you're still using webdriver.io v3, please use the v1.x branch of this plugin!

Installation

Use yarn:

yarn add webdriverajax -D

npm works as well:

npm install webdriverajax -D

Usage

Using with wdio

If you use the integrated test-runner (wdio) it's as easy as adding webdriverajax to your wdio.conf.js:

plugins: {
  webdriverajax: {}
}

and you're all set.

Once initialized, some related functions are added to your browser command chain (see API).

Quickstart

Example usage:

browser.url('http://foo.bar');
browser.setupInterceptor(); // capture ajax calls
browser.expectRequest('GET', '/api/foo', 200); // expect GET request to /api/foo with 200 statusCode
browser.expectRequest('POST', '/api/foo', 400); // expect POST request to /api/foo with 400 statusCode
browser.expectRequest('GET', /\/api\/foo/, 200); // can validate a URL with regex, too
browser.click('#button'); // button that initiates ajax request
browser.pause(1000); // maybe wait a bit until request is finished
browser.assertRequests(); // validate the requests

Get details about requests:

browser.url('http://foo.bar')
browser.setupInterceptor();
browser.click('#button')
browser.pause(1000);

var request = browser.getRequest(0);
assert.equal(request.method, 'GET');
assert.equal(request.response.headers['content-length'], '42');

Supported browsers

It should work with somewhat newer versions of all browsers.

Sauce Test Status

API

browser.setupInterceptor()

Captures ajax calls in the browser. You always have to call the setup function in order to assess requests later.

browser.expectRequest(method, url, statusCode)

Make expectations about the ajax requests that are going to be initiated during the test. Can (and should) be chained. The order of the expectations should map to the order of the requests being made.

  • method (String): http method that is expected. Can be anything xhr.open() accepts as first argument.
  • url (String|RegExp): exact URL that is called in the request as a string or RegExp to match
  • statusCode (Number): expected status code of the response

browser.assertRequests()

Call this method when all expected ajax requests are finished. It compares the expectations to the actual requests made and asserts the following:

  • Count of the requests that were made
  • The order of the requests
  • The method, the URL and the statusCode should match for every request made

browser.getRequest(index)

To make more sophisticated assertions about a specific request you can get details for a specific request after it is finished. You have to provide the index of the request you want to access in the order the requests were initiated (starting with 0).

  • index (Number): number of the request you want to access

Returns request object:

  • request.url: requested URL
  • request.method: used HTTP method
  • request.response.headers: response http headers as JS object
  • request.response.body: response body (will be parsed as JSON if possible)
  • request.response.statusCode: response status code

browser.getRequests()

Get all captured requests as an array.

Returns array of request objects.

Running the tests

A compatible browser (Firefox, Chrome) has to be installed. Also install selenium standalone via:

node_modules/.bin/selenium-standalone install

then

yarn test # npm test works as well :)

Contributing

I'm happy for every contribution. Just open an issue or directly file a PR.

License

MIT