webdriverajax
Advanced tools
Comparing version 2.1.1 to 2.2.0
## webdriverajax changelog | ||
### [ [>](https://github.com/chmanie/webdriverajax/tree/v2.2.0) ] 2.2.0 / 23.01.2018 | ||
* Add possibility to assess request headers | ||
* Improve error messages | ||
* Add test for iframe assertions | ||
* Fix chrome issue on CI | ||
### [ [>](https://github.com/chmanie/webdriverajax/tree/v2.1.1) ] 2.1.1 / 15.12.2017 | ||
@@ -4,0 +10,0 @@ * Fix problems with newer versions of selenium |
27
index.js
@@ -36,6 +36,12 @@ 'use strict'; | ||
var expectations = wdInstance.__wdajaxExpectations; | ||
if (!expectations.length) { | ||
return Promise.reject(new Error( | ||
'No expectations found. Call .expectRequest() first' | ||
)); | ||
} | ||
return getRequest().then(function assertAllRequests (requests) { | ||
var expectations = wdInstance.__wdajaxExpectations; | ||
if (expectations.length !== requests.length) { | ||
@@ -109,3 +115,4 @@ return Promise.reject(new Error( | ||
if (!request.value) { | ||
return Promise.reject(new Error('Could not find request with index ' + index)); | ||
const message = index ? 'Could not find request with index ' + index : 'No requests captured'; | ||
return Promise.reject(new Error(message)); | ||
} | ||
@@ -127,2 +134,3 @@ if (Array.isArray(request.value)) { | ||
} | ||
return { | ||
@@ -132,4 +140,5 @@ url: req.url, | ||
body: parseBody(req.requestBody), | ||
headers: normalizeRequestHeaders(req.requestHeaders), | ||
response: { | ||
headers: parseHeaders(req.headers), | ||
headers: parseResponseHeaders(req.headers), | ||
body: parseBody(req.body), | ||
@@ -141,3 +150,11 @@ statusCode: req.statusCode | ||
function parseHeaders (str) { | ||
function normalizeRequestHeaders (headers) { | ||
var normalized = {}; | ||
Object.keys(headers).forEach(function (key) { | ||
normalized[key.toLowerCase()] = headers[key]; | ||
}); | ||
return normalized; | ||
} | ||
function parseResponseHeaders (str) { | ||
var headers = {}; | ||
@@ -144,0 +161,0 @@ var arr = str.trim().replace(/\r/g, '').split('\n'); |
@@ -20,5 +20,7 @@ 'use strict'; | ||
var originalSend = xhr.send; | ||
var originalSetRequestHeader = xhr.setRequestHeader; | ||
var lastMethod; | ||
var lastURL; | ||
var lastRequestBody; | ||
var lastRequestHeader = {}; | ||
xhr.open = function () { | ||
@@ -52,2 +54,6 @@ lastMethod = arguments[0]; | ||
}; | ||
xhr.setRequestHeader = function() { | ||
lastRequestHeader[arguments[0]] = arguments[1]; | ||
originalSetRequestHeader.apply(xhr, arguments); | ||
}; | ||
xhr.addEventListener('load', function () { | ||
@@ -58,2 +64,3 @@ var req = { | ||
headers: xhr.getAllResponseHeaders(), | ||
requestHeaders: lastRequestHeader, | ||
// IE9 comp: need xhr.responseText | ||
@@ -60,0 +67,0 @@ body: xhr.response || xhr.responseText, |
{ | ||
"name": "webdriverajax", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Capture and assert HTTP ajax calls in webdriver.io 🕸", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -114,2 +114,3 @@ # webdriverajax | ||
* `request.body`: payload/body data used in request | ||
* `request.headers`: request http headers as JS object | ||
* `request.response.headers`: response http headers as JS object | ||
@@ -116,0 +117,0 @@ * `request.response.body`: response body (will be parsed as JSON if possible) |
@@ -145,2 +145,47 @@ 'use strict'; | ||
it('can assess the request body using JSON data', function () { | ||
browser.url('/post.html').setupInterceptor(); | ||
browser.click('#buttonjson').pause(wait); | ||
var request = browser.getRequest(0); | ||
assert.equal(request.headers['content-type'], 'application/json'); | ||
}); | ||
it('can get initialised inside an iframe', function () { | ||
browser.url('/frame.html').setupInterceptor(); | ||
var ret = browser.execute(function checkSetup () { | ||
return window.__webdriverajax; | ||
}); | ||
assert.deepEqual(ret.value, { requests: [] }); | ||
browser.waitForExist('#getinframe'); | ||
var frame = browser.element('#getinframe'); | ||
browser.frame(frame.value); | ||
browser.setupInterceptor(); | ||
var frameRet = browser.execute(function checkSetup () { | ||
return window.__webdriverajax; | ||
}); | ||
assert.deepEqual(frameRet.value, { requests: [] }); | ||
browser.expectRequest('GET', '/get.json', 200); | ||
browser.click('#button').pause(wait); | ||
browser.assertRequests(); | ||
browser.frameParent(); | ||
assert.throws(() => { | ||
browser.assertRequests(); | ||
}); | ||
}); | ||
it('errors with no requests set up', function () { | ||
browser.url('/get.html').setupInterceptor(); | ||
assert.throws(() => { | ||
browser.assertRequests(); | ||
}, /No\sexpectations\sfound/); | ||
}); | ||
it('errors properly when no requests were captured', function () { | ||
browser.url('/get.html').setupInterceptor(); | ||
browser.expectRequest('GET', '/get.json', 200); | ||
assert.throws(() => { | ||
browser.assertRequests(); | ||
}, /No\srequests\scaptured/); | ||
}); | ||
}); |
@@ -43,3 +43,3 @@ 'use strict'; | ||
chromeOptions: { | ||
args: ['headless'] | ||
args: ['headless', 'no-sandbox'] | ||
} | ||
@@ -46,0 +46,0 @@ }], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100278
19
641
154