A mock environment is an isolated test environment. You can use mock mode to test ScrapeGraphAI functionality in your application, and experiment with new features without affecting your live integration or consuming API credits. For example, when testing in mock mode, the scraping requests you create aren’t processed by our servers or counted against your credit usage.
Mock mode provides an environment for testing various functionalities and scenarios without the implications of real API calls. Below are some common use cases for mocking in your ScrapeGraphAI integrations:
Scenario
Description
Simulate scraping responses to test without real API calls
Use mock mode to test scraping functionality without real API calls. Create mock responses in your application to test data processing logic or use custom handlers to simulate various response scenarios.
Scale isolated testing for teams
Your team can test in separate mock environments to make sure that data and actions are completely isolated from other tests. Changes made in one mock configuration don’t interfere with changes in another.
Test without API key requirements
You can test your integration without providing real API keys, making it easier for external developers, implementation partners, or design agencies to work with your code without access to your live API credentials.
Test in development or CI/CD pipelines
Access mock mode from your development environment or continuous integration pipelines. Test ScrapeGraphAI functionality directly in your code or use familiar testing frameworks and fixtures.
You can simulate scraping responses and use mock data to test your integration without consuming API credits. Learn more about using mock responses to confirm that your integration works correctly.
Enable mock mode by setting mock=True when initializing the client:
Copy
from scrapegraph_py import Clientfrom scrapegraph_py.logger import sgai_logger# Set logging level for better visibilitysgai_logger.set_logging(level="INFO")def basic_mock_usage(): # Initialize the client with mock mode enabled client = Client.from_env(mock=True) print("\n-- get_credits (mock) --") print(client.get_credits()) print("\n-- markdownify (mock) --") md = client.markdownify(website_url="https://example.com") print(md) print("\n-- get_markdownify (mock) --") md_status = client.get_markdownify("00000000-0000-0000-0000-000000000123") print(md_status) print("\n-- smartscraper (mock) --") ss = client.smartscraper(user_prompt="Extract title", website_url="https://example.com") print(ss)if __name__ == "__main__": basic_mock_usage()
When mock mode is enabled, all API calls return predefined mock responses instead of making real HTTP requests. This ensures your tests run quickly and don’t consume API credits.
The JavaScript SDK supports per-request mocking via the mock parameter. Pass mock: true in the params object of any function to receive mock data instead of making a real API call.
import { smartScraper, scrape, searchScraper, getCredits } from 'scrapegraph-js';const API_KEY = 'your-api-key';// SmartScraper with mockconst smartResult = await smartScraper(API_KEY, { website_url: 'https://example.com', user_prompt: 'Extract the title', mock: true,});console.log('SmartScraper mock:', smartResult.data);// Scrape with mockconst scrapeResult = await scrape(API_KEY, { website_url: 'https://example.com', mock: true,});console.log('Scrape mock:', scrapeResult.data);// SearchScraper with mockconst searchResult = await searchScraper(API_KEY, { user_prompt: 'Find AI news', mock: true,});console.log('SearchScraper mock:', searchResult.data);
The JavaScript SDK does not have global mock functions like enableMock() or setMockResponses(). Mock mode is controlled per-request via the mock: true parameter. All functions return ApiResult<T> — errors are never thrown.