Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nicolassiver/http-probe
Utility for HTTP validation. Implementation is based on the Chrome debugging protocol.
https://github.com/nicolassiver/http-probe
chrome-debugging-protocol http-probe test-automation webdriver
Last synced: 2 months ago
JSON representation
Utility for HTTP validation. Implementation is based on the Chrome debugging protocol.
- Host: GitHub
- URL: https://github.com/nicolassiver/http-probe
- Owner: NicolasSiver
- License: mit
- Created: 2017-01-24T03:36:54.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T04:02:07.000Z (almost 2 years ago)
- Last Synced: 2024-09-19T01:28:59.318Z (3 months ago)
- Topics: chrome-debugging-protocol, http-probe, test-automation, webdriver
- Language: JavaScript
- Size: 757 KB
- Stars: 8
- Watchers: 3
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTP Probe
Utility for HTTP validation. Implementation is based on the Chrome debugging protocol.
![Version](https://img.shields.io/npm/v/http-probe.svg)
![Dependencies](https://david-dm.org/NicolasSiver/http-probe.svg)
![Code Climate](https://codeclimate.com/github/NicolasSiver/http-probe/badges/gpa.svg)
[![Coverage Status](https://coveralls.io/repos/github/NicolasSiver/http-probe/badge.svg?branch=master)](https://coveralls.io/github/NicolasSiver/http-probe?branch=master)## Table of Contents
- [Motivation](#motivation)
- [API](#api)
- [`HttpProbe`](#httpprobe)
- [`constructor(provider)`](#constructorprovider)
- [`getRequest(search)`](#getrequestsearch)
- [`RequestResult`](#requestresult)
- [`getResponse(search)`](#getresponsesearch)
- [`ResponseResult`](#responseresult)
- [`NetworkInspector`](#networkinspector)
- [`constructor(eventTarget)`](#constructoreventtarget)
- [`dispose()`](#dispose)
- [`getLogs(deplete)`](#getlogsdeplete)
- [Snapshots](#snapshots)
- [Links](#links)## Motivation
While Selenium and other end-to-end solutions provide a good set of tools to check UI feedback and states, they lack tools for HTTP validation.
HTTP Probe tries to solve an issue with HTTP testing by providing API to work and analyze Performance (in particular Network) logs in the modern browsers like Chromium.## API
Create an instance of the HTTP Probe. Don't forget to teardown an instance, otherwise `http-probe` will accumulate HTTP requests from every consecutive `getRequest` or `getResponse` invocation.
### `HttpProbe`
#### `constructor(provider)`
- `provider ` should return an array of performance logs
Example:
```js
const {HttpProbe} = require('http-probe');let httpProbe = new HttpProbe(() => myMethodToExtractPerformanceLogs());
```Extended example for `WebdriverIO`.
First of all you should activate performance logs for Google Chrome.
```json
{
"loggingPrefs": {
"browser": "ALL",
"performance": "ALL"
}
}
```Now in `before` hook you can create an instance of HTTP Probe:
```js
before(() => {
httpProbe = new HttpProbe(() => {
return browser.log('performance').value;
});
});
```You should use single test case per spec if you don't want fight with cache.
#### `getRequest(search)`
- `search ` a pattern which will be executed against an URL
Returns a `Request` entity with several properties:
- `length `, - total number of matched requests
- `executed `, - if request was executed at least once
- `executedOnce `, - if request was executed exactly _once_
- `executedTwice `, - if request was executed exactly _twice_
- `executeThrice `, - if request was executed exactly _thrice_
- `first `, - a result object for the _first_ request
- `second `, - a result object for the _second_ request
- `third `, - a result object for the _third_ request
- `last `, - a result object for the _last_ request##### `RequestResult`
- `headers `, - request's headers
- `method `, - HTTP method, 'GET', 'POST', etc.
- `postData `, - request's POST parameters
- `url `, - request's fully qualified URLExample:
```js
expect(httpProbe.getRequest('accounts/8').executed).to.be.true;
```#### `getResponse(search)`
- `search ` a pattern which will be executed against an URL
Returns a `Response` entity with several properties:
- `length `, - total number of matched responses
- `received `, - if response was delivered at least once
- `receivedOnce `, - if response was delivered exactly _once_
- `receivedTwice `, - if response was delivered exactly _twice_
- `receivedThrice `, - if response was delivered exactly _thrice_
- `first `, - a result object for the _first_ response
- `second `, - a result object for the _second_ response
- `third `, - a result object for the _third_ response
- `last `, - a result object for the _last_ response##### `ResponseResult`
- `encodedDataLength `, - Total number of bytes received for this request so far.
- `fromDiskCache `, - Specifies that the request was served from the disk cache.
- `fromServiceWorker `, - Specifies that the request was served from the ServiceWorker.
- `headers `, - HTTP response headers.
- `requestHeaders `, - (Optional) Refined HTTP request headers that were actually transmitted over the network.
- `status `, - HTTP response status code.
- `statusText `, - HTTP response status text.
- `url `, - Response URL. This URL can be different from CachedResource.url in case of redirect.Example:
```js
expect(httpProbe.getResponse('total/cart').last.status).to.be.equal(200);
```### `NetworkInspector`
Captures network events through the Chrome debugging protocol for the later use in HttpProbe for analysis.
Specifically designed for the solutions that can not provide performance logs or it's more convenient to use listener abstraction for network logs.#### `constructor(eventTarget)`
- `eventTarget ` entity that satisfies EventEmitter interface at least for ability to subscribe (`on`) and unsubscribe (`removeListener`) for the events
Example:
```js
const {NetworkInspector} = require('http-probe');let inspector = new NetworkInspector(myEmitter);
console.log(inspector.getLogs());
inspector.dispose();
```Extended example for `WebdriverIO` with the use of `before` and `after` hooks.
```js
const {HttpProbe, NetworkInspector} = require('http-probe');let inspector;
before(() => {
browser.cdp('Network', 'enable');
inspector = new NetworkInspector(browser);
httpProbe = new HttpProbe(() => inspector.getLogs());
});after(() => {
inspector.dispose();
});
```#### `dispose()`
Resets internal resources and listeners.
After this point, the instance of Network Inspector is not usable.Example:
```js
networkInspector.dispose();
```#### `getLogs(deplete)`
- `deplete ` an optional parameter, by default it's always `true`. If the parameter is `false` logs will be preserved before the next `getLogs` invocation.
Returns a list of messages formatted to comply with Chrome debugging protocol.
Example:
```js
let myLogs = networkInspector.getLogs();
console.log(myLogs);
```## Snapshots
Tests are working with snapshots. Snapshots are picked randomly and recorded for 30 seconds.
To create a snapshot, instance of the Chrome should be active, if yor are using Mac, it could be done via:```shell
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
```or run Chrome Browser in the container:
```shell
$ docker pull justinribeiro/chrome-headless
$ docker run -it --rm -p 9222:9222 justinribeiro/chrome-headless
```Now it's possible to make a snapshot:
```shell
URL=http://some-domain.com node create-snapshot.js// or visit multiple websites
URL="http://domain1.com http://domain2.com" node create-snapshot.js
```## Links
- [Protocol Viewer](https://github.com/ChromeDevTools/debugger-protocol-viewer)
- [Performance Log](https://sites.google.com/a/chromium.org/chromedriver/logging/performance-log)