What is @web/test-runner-commands?
@web/test-runner-commands is a package that provides a set of commands to facilitate testing in web environments. It allows you to execute commands in the browser context, making it easier to interact with the page and perform various testing tasks.
What are @web/test-runner-commands's main functionalities?
Execute Commands in Browser Context
This feature allows you to execute custom commands in the browser context. The code sample demonstrates how to use the `executeCommand` function to run a command named 'myCommand' with some data, and then log the result.
import { executeCommand } from '@web/test-runner-commands';
(async () => {
const result = await executeCommand('myCommand', { someData: 123 });
console.log(result);
})();
Screenshot Testing
This feature enables you to take screenshots of the current page state during tests. The code sample shows how to use the `takeScreenshot` function to capture a screenshot and log the result.
import { takeScreenshot } from '@web/test-runner-commands';
(async () => {
const screenshot = await takeScreenshot();
console.log('Screenshot taken:', screenshot);
})();
Evaluate Expressions
This feature allows you to evaluate JavaScript expressions in the browser context. The code sample demonstrates using the `evaluate` function to get the page title and log it.
import { evaluate } from '@web/test-runner-commands';
(async () => {
const result = await evaluate(() => document.title);
console.log('Page title:', result);
})();
Other packages similar to @web/test-runner-commands
puppeteer
Puppeteer is a Node library that provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It offers similar functionalities like executing commands in the browser context, taking screenshots, and evaluating expressions. However, Puppeteer is more comprehensive and can be used for broader automation tasks beyond testing.
selenium-webdriver
Selenium WebDriver is a popular tool for automating web applications for testing purposes. It provides capabilities to control a browser and perform actions like clicking, typing, and taking screenshots. Compared to @web/test-runner-commands, Selenium WebDriver is more established and supports multiple browsers, but it may require more setup and configuration.
cypress
Cypress is a front-end testing tool built for the modern web. It provides an all-in-one testing framework, assertion library, and mocking/stubbing capabilities. While it offers similar features like executing commands and taking screenshots, Cypress is more focused on end-to-end testing with a user-friendly interface and real-time reloading.
Test Runner Commands
Commands for executing code server-side during your tests in the browser. To control the browser page, access the file system, or execute NodeJS libraries.
Built-in commands can change the viewport, emulate media queries or set the user agent. You can create custom commands to implement your own functionalities.
See our website for full documentation.