JS Client

Stoobly JavaScript Client Library - Questions & Answers

The Stoobly JavaScript library (stoobly) provides convenient access to stoobly-agent from Playwright and Cypress E2E tests. It enables recording, mocking, and testing HTTP requests directly from your test framework.

📚 Related Documentation:


Installation and Setup

Q: How do I install the Stoobly JavaScript library?

A: Install the library as a dev dependency using npm or yarn.

Example:

# Install with npm
npm install stoobly --save-dev

# Install with yarn
yarn add -D stoobly

Q: What are the requirements for using the Stoobly JavaScript library?

A: The library requires Node.js 18 or higher and works with Playwright and Cypress test frameworks.

Example:

Q: How do I import the Stoobly library?

A: Import the library using ES modules or CommonJS syntax.

Example:


Playwright Integration

Q: How do I integrate Stoobly with Playwright tests?

A: Create a Stoobly instance and Playwright interceptor, then call withPage() and apply() in your beforeEach hook.

Example:

Q: Why do I need to call withPage() and withTestTitle() in Playwright?

A: Playwright doesn't provide a global API to auto-detect the current page or test title, so you must explicitly set them in beforeEach.

Example:

Q: When should I use withContext() instead of withPage()?

A: Use withContext() when you need to intercept requests from all pages in a browser context, including new pages created during tests. Use withPage() when you only want to intercept requests from a specific page.

Key differences:

  • withPage() - Intercepts requests only from the specified page. New pages created in the same context will NOT be intercepted.

  • withContext() - Intercepts requests from all pages in the browser context, including pages created with context.newPage(), browser extensions, and service workers.

  • Both together - You can use both withContext() and withPage() to ensure all pages are intercepted.

Example:

Use withContext() when:

  1. Your tests create multiple pages (popups, new tabs)

  2. You're testing browser extensions that make background requests

  3. You're testing service workers

  4. You want consistent interception across all pages without calling withPage() for each

Use withPage() when:

  1. You only work with the test fixture page

  2. You want fine-grained control over which pages are intercepted

  3. You're following the typical Playwright test pattern with a single page

Q: How do I record requests in Playwright tests?

A: Use applyRecord() instead of apply() to enable recording mode.

Example:


Cypress Integration

Q: How do I integrate Stoobly with Cypress tests?

A: Create a Stoobly instance and Cypress interceptor, then call apply() in your beforeEach hook.

Example:

Q: Why must I call apply() in beforeEach for Cypress?

A: Cypress automatically clears all intercepts before every test, so you must reapply the Stoobly interceptor in beforeEach.

Example:

Q: How do I record requests in Cypress tests?

A: Use applyRecord() instead of apply() to enable recording mode.

Example:

Q: What's the warning about synchronous requests in Cypress?

A: Synchronous XMLHttpRequest calls will cause Cypress to hang. Avoid using synchronous requests when Stoobly interceptor is active.

Example:


Configuring URL Patterns

Q: How do I specify which URLs to intercept?

A: Use the urls array with strings or regular expressions to filter which requests Stoobly intercepts.

Example:

Q: How do I change the intercepted URLs dynamically?

A: Pass a new urls array to the apply() method to update which URLs are intercepted.

Example:


Scenarios and Sessions

Q: How do I specify a scenario for my tests?

A: Use scenarioKey or scenarioName in the interceptor options to associate requests with a scenario.

Example:

Q: How do I change the scenario dynamically?

A: Use withScenarioKey() or withScenarioName() to update the scenario.

Example:

Q: What is a session ID and how do I use it?

A: A session ID groups requests together within a scenario. It defaults to the current timestamp but can be customized for test reproducibility.

Example:


Recording Configuration

Q: What record policies are available?

A: Stoobly supports three record policies: All (record everything), Found (record only if request exists), and NotFound (record only new requests).

Example:

Q: What's the difference between record orders?

A: Overwrite replaces existing requests with the same signature, while Append always creates new request records.

Example:

Note: Overwrite is sent only once per session. Subsequent requests use Append behavior.

Q: What record strategies are available?

A: Stoobly supports Full (record complete request/response) and Minimal (record only essential data).

Example:

Q: How do I change record settings dynamically?

A: Use withRecordPolicy(), withRecordOrder(), and withRecordStrategy() methods.

Example:


Controlling Interception

Q: How do I stop recording requests?

A: Use clearRecord() to stop recording while keeping the interceptor active for mocking.

Example:

Q: How do I completely remove the interceptor?

A: Use clear() to remove all interception and clear all headers.

Example:

Q: What's the difference between clear() and clearRecord()?

A: clearRecord() stops recording but keeps mocking active, while clear() removes all interception.

Example:


Advanced Configuration

Q: How do I set a custom Stoobly UI URL?

A: Pass the UI URL to the Stoobly constructor if your agent is running on a different port or host.

Example:

Q: How do I use test titles for request grouping?

A: Set test titles using withTestTitle() to group requests by test name in the Stoobly UI.

Example:

Q: Can I use the interceptor without a test framework?

A: Yes, use the generic interceptor() method for vanilla JavaScript applications.

Example:


Troubleshooting

Q: Why aren't my requests being intercepted?

A: Verify the URL patterns match your requests, the interceptor is applied in beforeEach, and stoobly-agent is running.

Example:

Q: How do I debug interceptor issues?

A: Check the browser's network tab for Stoobly headers and verify the agent is receiving requests.

Example:

Q: Why do I get "page is not defined" errors in Playwright?

A: Ensure you call withPage(page) in beforeEach before apply().

Example:

Q: How do I handle TypeScript errors?

A: The library includes TypeScript definitions. Ensure your tsconfig.json includes the library.

Example:


Complete Examples

Q: What's a complete Playwright example with recording and mocking?

A: Here's a complete example showing recording and mocking workflows with an environment variable to toggle recording mode.

Example:

Usage:

Workflow:

  1. First run (recording): Set STOOBLY_RECORD=true to capture all API requests and responses

  2. Subsequent runs (mocking): Leave STOOBLY_RECORD unset to use recorded responses for fast, reliable tests

  3. Update recordings: Set STOOBLY_RECORD=true again when APIs change to refresh the recorded data

Q: What's a complete Cypress example with recording and mocking?

A: Here's a complete example showing recording and mocking workflows with an environment variable to toggle recording mode.

Example:

Usage:

Workflow:

  1. First run (recording): Set STOOBLY_RECORD=true to capture all API requests and responses

  2. Subsequent runs (mocking): Leave STOOBLY_RECORD unset to use recorded responses for fast, reliable tests

  3. Update recordings: Set STOOBLY_RECORD=true again when APIs change to refresh the recorded data


Quick Reference

Q: What are the key methods for the interceptor?

A: Here's a quick reference of the most common interceptor methods.

Example:

Q: What constants are available?

A: Stoobly provides enums for record policies, orders, and strategies.

Example:

Last updated