{"id":18926,"library":"wdio-intercept-service","title":"WDIO Intercept Service","description":"A WebdriverIO plugin for capturing and asserting HTTP AJAX calls (XHR and fetch) during e2e tests. Current stable version is 4.4.1, targeting WebdriverIO v8+. It works by injecting a script into the browser after page load to intercept requests initiated by user actions within test code. Key differentiators: supports both XMLHttpRequest and fetch APIs, allows asserting request method, URL (string or regex), status code, and response details. Cannot intercept requests made on initial page load.","status":"active","version":"4.4.1","language":"javascript","source_language":"en","source_url":"https://github.com/webdriverio-community/wdio-intercept-service","tags":["javascript","selenium","webdriver","webdriverio","wdio-service","wdio-plugin","service","webdriverjs","fetch","typescript"],"install":[{"cmd":"npm install wdio-intercept-service","lang":"bash","label":"npm"},{"cmd":"yarn add wdio-intercept-service","lang":"bash","label":"yarn"},{"cmd":"pnpm add wdio-intercept-service","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; must be installed in the project with a compatible version (v5+ for current plugin, v8+ recommended).","package":"webdriverio","optional":false}],"imports":[{"note":"ESM default export. Do not use .default with require() – you will get an object with a default property instead of the function.","wrong":"const WebdriverAjax = require('wdio-intercept-service').default","symbol":"default (function)","correct":"import WebdriverAjax from 'wdio-intercept-service'"},{"note":"Case-sensitive command added to browser object after service is initialized. Must be called before making requests you want to intercept.","wrong":"browser.setupinterceptor()","symbol":"setupInterceptor","correct":"browser.setupInterceptor()"},{"note":"Third argument (statusCode) is required. Can use string or RegExp for URL.","wrong":"browser.expectRequest('GET', '/api/foo')","symbol":"expectRequest","correct":"browser.expectRequest('GET', '/api/foo', 200)"},{"note":"assertRequests does not accept options. Use separate pause() or wait strategies before calling it.","wrong":"browser.assertRequests({ timeout: 2000 })","symbol":"assertRequests","correct":"browser.assertRequests()"}],"quickstart":{"code":"import { remote } from 'webdriverio';\nimport WebdriverAjax from 'wdio-intercept-service';\n\nasync function run() {\n  const browser = await remote({\n    logLevel: 'warn',\n    capabilities: { browserName: 'chrome' }\n  });\n\n  const serviceLauncher = WebdriverAjax();\n  serviceLauncher.before(null, null, browser);\n  serviceLauncher.beforeTest();\n\n  await browser.url('https://example.com');\n  await browser.setupInterceptor();\n  await browser.expectRequest('GET', '/api/data', 200);\n  await browser.click('#load-data');\n  await browser.pause(1000);\n  await browser.assertRequests();\n\n  const request = await browser.getRequest(0);\n  console.log('Request method:', request.method);\n\n  await browser.deleteSession();\n}\nrun().catch(console.error);","lang":"typescript","description":"Demonstrates setting up the service manually with WebdriverIO standalone, intercepting an XHR/fetch request, and asserting it."},"warnings":[{"fix":"Ensure that setupInterceptor() is called before the user action that triggers the desired HTTP request.","message":"Requests initiated on page load cannot be intercepted. Only requests triggered within test code after calling setupInterceptor() are captured.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Upgrade to WebdriverIO v8 or use wdio-intercept-service v3.x (if available) for older versions.","message":"Version 4.x requires WebdriverIO v8. Compatibility with WDIO v5-v7 is dropped.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Use CORS-enabled endpoints or switch to XMLHttpRequest if interception is required.","message":"When using fetch, requests with 'no-cors' mode may not be intercepted due to browser limitations.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Check the return value or use getRequests() to iterate.","message":"The getRequest(index) method returns undefined if the index is out of range. No error is thrown.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Use browser.pause() or browser.waitUntil() before calling assertRequests() to ensure requests have completed.","message":"assertRequests() does not accept options for timeout or retries. Use manual pauses or WebdriverIO waits instead.","severity":"deprecated","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure the service is initialized with `const serviceLauncher = WebdriverAjax();` and then call `serviceLauncher.before(null, null, browser);` passing the browser instance.","cause":"Calling serviceLauncher.before before the service is initialized or with incorrect arguments.","error":"TypeError: Cannot read properties of undefined (reading 'before')"},{"fix":"Use `browser.expectRequest('GET', '/api/foo', 200);` – all three arguments are required.","cause":"Calling expectRequest with missing or invalid arguments (e.g., no statusCode).","error":"Error: expectRequest requires a method, url, and statusCode."},{"fix":"Ensure browser.setupInterceptor() is called before the action that triggers the request.","cause":"Request was not intercepted because setupInterceptor() was not called or the request was made before it was called.","error":"AssertionError: expected 0 to equal 1"},{"fix":"Use `const WebdriverAjax = require('wdio-intercept-service');` or configure your project for ESM.","cause":"Using ES module import syntax in a CommonJS environment (e.g., Node.js without 'type': 'module' or .mjs extension).","error":"SyntaxError: Unexpected token 'export'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}