TestCafe Browser Tools

2.0.26 · active · verified Sun Apr 19

TestCafe Browser Tools is a JavaScript utility library designed to perform platform-dependent actions on web browser windows, crucial for automated testing frameworks like TestCafe. It abstracts away the complexities of interacting with browser APIs across different operating systems (Windows, macOS, Linux) by encapsulating pre-built native binaries and JavaScript wrappers. This approach eliminates the need for users to perform post-install build steps, ensuring a smoother setup. The current stable version is 2.0.27, with frequent patch and minor updates primarily addressing bug fixes, dependency vulnerabilities, and binary stability. Its key differentiator is the provision of robust, pre-compiled native tools that handle browser window manipulation, a task that often requires direct OS interaction and platform-specific code. It's an essential component for scenarios requiring reliable programmatic control over browser windows' positions, sizes, and focus during automated tests.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to find a browser window by title, retrieve its information, and maximize it.

import { findWindow, getWindowInfo, maximizeWindow } from 'testcafe-browser-tools';

async function automateBrowserActions() {
  // In a real scenario, a browser would already be open, e.g., by TestCafe
  console.log('Searching for a browser window by title (e.g., ' + process.env.BROWSER_WINDOW_TITLE + ')...');

  // Note: For Microsoft Edge, ensure document.title is not just a URL.
  // e.g., document.title = 'My Test Page: ' + document.location.toString();
  const windowTitle = process.env.BROWSER_WINDOW_TITLE ?? 'Google'; // Replace with an actual window title

  try {
    const windowId = await findWindow(windowTitle);
    if (windowId) {
      console.log(`Found window with title '${windowTitle}', ID: ${windowId}`);

      const info = await getWindowInfo(windowId);
      console.log('Window Info:', info);

      console.log(`Maximizing window ID: ${windowId}`);
      await maximizeWindow(windowId);
      console.log('Window maximized.');

      // Example: Restore window (or other actions)
      // await restoreWindow(windowId);
      // console.log('Window restored.');
    } else {
      console.log(`Window with title '${windowTitle}' not found.`);
    }
  } catch (error) {
    console.error('An error occurred:', error.message);
  }
}

automateBrowserActions();

view raw JSON →