Taiko Browser Automation

repository·master·Indexed 25 days ago

https://github.com/getgauge/taiko

Taiko is a Node.js library for browser automation optimized for testing modern web applications. It features a human-readable API with smart selectors to minimize complex CSS/XPath, an interactive REPL for real-time browser control, and network interception capabilities for stubbing and mocking requests. It supports Chromium and experimental Firefox support, and integrates with the Gauge testing framework.

Tokens
13.2K
Snippets
53
Records
112
Agent score
85%

What's inside Taiko

  1. Understand Taiko's implicit assertions

    master
    Taiko performs implicit assertions on all Page actions, browser actions, selectors, and proximity selectors. Before performing an action, Taiko checks if the target element is visible on the page and not covered by other elements (e.g., a modal). If the element is not visible, hidden, or covered, Taiko will throw a JavaScript Error and the test will fail.
  2. Available Taiko example scenarios

    master

    The examples folder demonstrates several common automation tasks using Taiko against 'The Internet Express' application:

    • File Upload: 01-file_upload.js
    • File Download: 02-file_download.js
    • Working with frames: 03-work-with-frames.js
    • Handling windows/tabs: 04-windows-tabs.js
    • Dropdowns: 05-dropdown.js
    • Basic Auth: 06-basic-auth.js
    • Dynamic Content: 07-dynamic-loading.js
    • Contenteditable elements: 08-contenteditable.js
  3. Use community Taiko plugins

    master

    Taiko supports various community-developed plugins to extend its functionality, including accessibility testing, Android device support, performance diagnostics, screencasting, storage interaction, and automated screenshots.

    Available community plugins include:

    • taiko-accessibility: Test site accessibility.
    • taiko-android: Run web tests on Android devices and emulators.
    • taiko-diagnostics: Measure speedindex and performance metrics.
    • taiko-screencast: Record a GIF video of a Taiko script run.
    • taiko-storage: Interact with browser storages.
    • taiko-screeny: Capture a screenshot on every action.
    • taiko-video: Save screencasts as compressed MP4 videos.
  4. Ignore implicit assertions using try/catch

    master

    If you want to attempt an action without failing the test when the implicit assertion fails (e.g., when an element is not visible), wrap the action in a JavaScript try...catch block.

    const { openBrowser, goto, click, closeBrowser } = require('taiko');
    (async () => {
      await openBrowser();
      await goto("google.com");
      try {
        await click("Google Search");
      } catch(e) {
        // Ignore or log the error.
      } finally {
        closeBrowser();
      }
    })();
  5. Use Smart Selectors for element interaction

    master

    Taiko uses smart selectors to mimic user behavior, reducing the need for brittle CSS or XPath selectors.

    • Text-based selection: click("Button Text") clicks an element containing that text.
    • Focus-based writing: write("text") writes into the currently focused element.
    • Targeted writing: write("text", into(textBox({placeholder: "Username"}))) targets a specific field.
    • Proximity selectors: click(checkBox(near("Username"))) clicks the checkbox nearest to the element with text "Username".
    • Standard selectors: Supports CSS ($("#id")) and XPath ($("//xpath")).
  6. Explore the Taiko API in the REPL

    master

    The Taiko REPL includes built-in commands to discover available APIs and their usage:

    • .api: Lists all available API categories (e.g., Browser actions, Page actions).
    • .api <api_name>: Provides detailed information for a specific API, including a description, parameters, return values, and an example usage.
    > .api
    > .api click
  7. Load plugins at runtime

    master

    You can load plugins using the following methods:

    1. Taiko CLI/REPL: Use the --plugins option.
      taiko code.js --plugin 'android'
    2. Environment Variable: Use TAIKO_PLUGINS to load plugins when using other runners.
    3. Automatic Loading: Taiko automatically loads plugins found in the user's package.json dependencies by calling their init method.
  8. Configure Taiko example run options

    master

    You can modify how the examples are executed using CLI flags passed to npm test:

    • Observe mode: Use --observe to run the examples in a headed browser so you can visually monitor the actions.
    • Run a specific example: Pass the file prefix (e.g., 05) to run only that specific test file.
    • Screencast: Use --screencast to run all examples and record the session using the taiko-screencast plugin. Recordings are saved in the captures directory.