CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-storybook--addon-storyshots-puppeteer

Image snapshots addition to StoryShots based on puppeteer

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration Management

Shared configuration interfaces and default values for browser management, navigation options, and test execution control.

Capabilities

Common Configuration Interface

Base configuration options shared across all test types for consistent browser management and navigation.

interface CommonConfig {
  /** URL to the Storybook instance (default: 'http://localhost:6006') */
  storybookUrl: string;
  /** Path to Chrome executable (overrides SB_CHROMIUM_PATH env var) */
  chromeExecutablePath?: string;
  /** Function to provide page navigation options */
  getGotoOptions: (options: Options) => DirectNavigationOptions | undefined;
  /** Function to customize page before navigation */
  customizePage: (page: Page) => Promise<void>;
  /** Function to provide custom browser instance */
  getCustomBrowser?: () => Promise<Browser>;
  /** Puppeteer browser launch options */
  browserLaunchOptions: LaunchOptions;
  /** Browser setup timeout in milliseconds (default: 15000) */
  setupTimeout: number;
  /** Individual test timeout in milliseconds (default: 15000) */
  testTimeout: number;
}

interface Options {
  context: Context;
  url: string;
}

interface Context {
  kind: string;
  story: string;
  parameters: { [key: string]: any };
}

Navigation Configuration

Control page navigation behavior and timing for reliable test execution.

interface DirectNavigationOptions {
  /** HTTP referer header value */
  referer?: string;
  /** Navigation timeout in milliseconds */
  timeout?: number;
  /** Conditions to wait for before considering navigation complete */
  waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
}

type PuppeteerLifeCycleEvent = 'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2';

/**
 * Function type for providing navigation options
 * @param options - Test context and URL information
 * @returns Navigation options for page.goto()
 */
type GetGotoOptions = (options: Options) => DirectNavigationOptions | undefined;

Usage Examples:

// Wait for network idle before proceeding
const getGotoOptions = ({ context, url }) => ({
  waitUntil: 'networkidle0',
  timeout: 30000
});

// Different wait strategies per story type  
const getGotoOptions = ({ context }) => {
  if (context.kind.includes('Loading')) {
    return { waitUntil: 'networkidle2' };
  }
  return { waitUntil: 'domcontentloaded' };
};

// Custom referer for authentication
const getGotoOptions = () => ({
  referer: 'https://authenticated-domain.com',
  waitUntil: 'load'
});

Browser Launch Configuration

Configure Puppeteer browser instance with custom launch options and executable paths.

/**
 * Puppeteer browser launch options interface
 */
interface LaunchOptions {
  /** Browser executable path */
  executablePath?: string;
  /** Ignore HTTPS certificate errors */
  ignoreHTTPSErrors?: boolean;
  /** Run browser in headless mode */
  headless?: boolean | 'new';
  /** Browser launch arguments */
  args?: string[];
  /** Ignore default arguments */
  ignoreDefaultArgs?: boolean | string[];
  /** Default viewport settings */
  defaultViewport?: Viewport | null;
  /** Slow down operations by specified milliseconds */
  slowMo?: number;
  /** Timeout for browser launch */
  timeout?: number;
}

interface Viewport {
  width: number;
  height: number;
  deviceScaleFactor?: number;
  isMobile?: boolean;
  hasTouch?: boolean;
  isLandscape?: boolean;
}

Usage Examples:

// SSL testing configuration
const browserLaunchOptions = {
  ignoreHTTPSErrors: true,
  args: ['--ignore-certificate-errors']
};

// Mobile testing viewport
const browserLaunchOptions = {
  defaultViewport: {
    width: 375,
    height: 667,
    deviceScaleFactor: 2,
    isMobile: true,
    hasTouch: true
  }
};

// Debug mode configuration
const browserLaunchOptions = {
  headless: false,
  slowMo: 250,
  devtools: true
};

Page Customization

Customize page settings before navigation to stories.

/**
 * Function type for page customization
 * @param page - Puppeteer page instance to customize
 * @returns Promise for async customization operations
 */
type CustomizePage = (page: Page) => Promise<void>;

Usage Examples:

// Device emulation
const customizePage = async (page) => {
  const iPhone = require('puppeteer/DeviceDescriptors')['iPhone 6'];
  await page.emulate(iPhone);
};

// Custom user agent and viewport
const customizePage = async (page) => {
  await page.setUserAgent('Custom Test Agent 1.0');
  await page.setViewport({ width: 1920, height: 1080 });
  
  // Disable images for faster loading
  await page.setRequestInterception(true);
  page.on('request', (req) => {
    if(req.resourceType() == 'image'){
      req.abort();
    } else {
      req.continue();
    }
  });
};

// Authentication setup
const customizePage = async (page) => {
  // Set authentication cookies
  await page.setCookie({
    name: 'auth-token',
    value: 'test-token-123',
    domain: 'localhost'
  });
  
  // Add custom headers
  await page.setExtraHTTPHeaders({
    'Authorization': 'Bearer test-token'
  });
};

Custom Browser Provider

Provide your own browser instance for advanced scenarios like remote browsers or browser pools.

/**
 * Function type for providing custom browser instance
 * @returns Promise resolving to Puppeteer Browser instance
 */
type GetCustomBrowser = () => Promise<Browser>;

Usage Examples:

// Connect to remote browser
const getCustomBrowser = () => {
  return puppeteer.connect({ 
    browserWSEndpoint: 'ws://remote-browser:9222' 
  });
};

// Use existing browser instance
let sharedBrowser;
const getCustomBrowser = async () => {
  if (!sharedBrowser) {
    sharedBrowser = await puppeteer.launch({
      headless: false,
      args: ['--no-sandbox']
    });
  }
  return sharedBrowser;
};

// Docker container browser
const getCustomBrowser = () => {
  return puppeteer.connect({
    browserURL: 'http://chrome-container:9222'
  });
};

Default Configuration Values

Pre-configured default values for all common configuration options.

const defaultCommonConfig: CommonConfig = {
  storybookUrl: 'http://localhost:6006',
  chromeExecutablePath: process.env.SB_CHROMIUM_PATH,
  getGotoOptions: () => undefined,
  customizePage: () => Promise.resolve(),
  getCustomBrowser: undefined,
  browserLaunchOptions: {},
  setupTimeout: 15000,
  testTimeout: 15000,
};

// Note: When no custom browser is provided, the addon automatically includes
// security arguments for Linux systems: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage']

Timeout Configuration

Control timing for different phases of test execution.

interface TimeoutConfig {
  /** Browser initialization timeout (default: 15000ms) */
  setupTimeout: number;
  /** Individual test execution timeout (default: 15000ms) */
  testTimeout: number;
}

Usage Examples:

// Extended timeouts for slow environments
const timeoutConfig = {
  setupTimeout: 30000,  // 30 seconds for browser launch
  testTimeout: 25000    // 25 seconds per test
};

// Quick timeouts for fast CI
const timeoutConfig = {
  setupTimeout: 10000,  // 10 seconds
  testTimeout: 8000     // 8 seconds per test
};

Environment Variable Support

Automatic integration with environment variables for common configuration needs.

interface EnvironmentVariables {
  /** Chrome executable path from environment */
  SB_CHROMIUM_PATH?: string;
}

// Usage in configuration
const chromeExecutablePath = process.env.SB_CHROMIUM_PATH || '/usr/bin/google-chrome';

Install with Tessl CLI

npx tessl i tessl/npm-storybook--addon-storyshots-puppeteer

docs

axe-test.md

configuration.md

image-snapshot.md

index.md

puppeteer-test.md

tile.json