Skip to content

Commit

Permalink
fix(compatibility): restore compatibility with WDIO < 6.10
Browse files Browse the repository at this point in the history
- use the global browser instance if one is not passed into the `before` hook.
- add unit test for this scenario
  • Loading branch information
tehhowch committed Feb 27, 2024
1 parent d15bb9d commit 210fdfc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## wdio-intercept-service changelog

### [ [>](https://github.com/webdriverio-community/wdio-intercept-service/tree/v.next) ] v.next / <DATE>
- Upgrade WDIO test dependencies to minimum of 8.14 for auto-driver management
- Update repo GH action dependencies
- Update WDIO to v8.32 to fix Chromedriver download URL (thanks @seanpoulter)
- Fix property check that broke compatibility with WDIO versions before v6.10

### [ [>](https://github.com/webdriverio-community/wdio-intercept-service/tree/v4.3.0) ] 4.4.0 / 24.02.2023
- Promptly throw if the user passes an invalid "index" parameter to `getRequest`
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WebdriverIO Intercept Service

This is a plugin for [webdriver.io](http://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 wdio-intercept-service 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.
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 wdio-intercept-service 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 responses 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.

Expand All @@ -19,7 +19,7 @@ There's one catch though: you can't intercept HTTP calls that are initiated on p

## Installation

```
```shell
npm install wdio-intercept-service -D
```

Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ class WebdriverAjax {
this._wdajaxExpectations = [];
}

before(_, __, browser) {
before() {
// WebdriverIO before v6.10 does not pass in the browser instance
/** @type {import('webdriverio').BrowserBase} */
const browser =
typeof arguments[2] === 'undefined' ? globalThis.browser : arguments[2];

/**
* instance need to have addCommand method
*/
if (typeof browser.addCommand !== 'function') {
if (!browser || typeof browser.addCommand !== 'function') {
throw new Error(
"you can't use WebdriverAjax with this version of WebdriverIO",
);
Expand Down
8 changes: 4 additions & 4 deletions lib/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
var interceptor = {
disableInterceptor: function disableInterceptor(done) {
var NAMESPACE = '__webdriverajax';
if (window[NAMESPACE] != undefined) {
if (typeof window[NAMESPACE] !== 'undefined') {
window[NAMESPACE].interceptorDisabled = true;
}
done(window[NAMESPACE]);
},

excludeUrls: function excludeUrls(urls, done) {
var NAMESPACE = '__webdriverajax';
if (window[NAMESPACE] != undefined) {
if (typeof window[NAMESPACE] !== 'undefined') {
// Convert the strings to regular expressions once.
var exprs = urls.map(function (obj) {
return new RegExp(obj.source, obj.flags);
Expand All @@ -32,15 +32,15 @@ var interceptor = {
};

// Some browsers don't support FormData.entries(), so we polyfill that (sigh)
if (typeof FormData.prototype.entries == 'undefined') {
if (typeof FormData.prototype.entries === 'undefined') {
polyfillFormDataEntries();
}

if (supportsSessionStorage()) {
window.sessionStorage.removeItem(NAMESPACE);
}

if (typeof window.fetch == 'function') {
if (typeof window.fetch === 'function') {
replaceFetch();
if (
typeof window.Promise === 'undefined' ||
Expand Down
52 changes: 35 additions & 17 deletions test/spec/plugin_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,41 @@ describe('webdriverajax', function testSuite() {
);
};

it('sets up the interceptor', async function () {
describe('before hook', function () {
it('registers service methods with specific browser instance when provided', async function () {
const browser = await remote({
capabilities: {
browserName: 'chrome',
'goog:chromeOptions': {
args: ['--headless', '--disable-gpu'],
},
},
});

new WebdriverAjax().before(null, null, browser);

assert.strictEqual(typeof browser.setupInterceptor, 'function');
});

it('registers service methods on the global browser when not provided a specific instance', async function () {
const originalBrowser = globalThis.browser;
const commands = [];
globalThis.browser = { addCommand: (name) => commands.push(name) };
new WebdriverAjax().before();

try {
assert.strictEqual(
commands.some((name) => name === 'setupInterceptor'),
true,
'should add commands to global browser',
);
} finally {
globalThis.browser = originalBrowser;
}
});
});

it('setupInterceptor - configures window state properly', async function () {
assert.equal(typeof browser.setupInterceptor, 'function');
await browser.url('/get.html');
await browser.setupInterceptor();
Expand All @@ -52,22 +86,6 @@ describe('webdriverajax', function testSuite() {
});
});

it('sets up the interceptor in standalone mode', async function () {
const browser = await remote({
capabilities: {
browserName: 'chrome',
'goog:chromeOptions': {
args: ['--headless', '--disable-gpu'],
},
},
});

const webdriverAjax = new WebdriverAjax();
webdriverAjax.before(null, null, browser);

assert.equal(typeof browser.setupInterceptor, 'function');
});

it('disables and enables interceptor', async function () {
await browser.url('/get.html');
assert.equal(await browser.disableInterceptor(), null);
Expand Down

0 comments on commit 210fdfc

Please sign in to comment.