browser-UI-test
browser-UI-test is a JavaScript framework designed for headless browser-based UI testing, primarily focused on visual regression. It allows developers to define UI test scenarios using simple `.goml` script files, which are then executed in a headless browser environment, typically leveraging Puppeteer. After script execution, the framework takes a screenshot of the page and compares it against an expected baseline image, failing the test if differences are detected. The package is currently at version 0.23.3, and its development seems active, with Docker images being pushed on merges to the main branch. This tool differentiates itself by its `.goml` script-based, declarative approach to UI interactions and its integrated screenshot comparison functionality, aiming for ease of use for quick UI checks rather than complex end-to-end testing suites like Playwright or Cypress.
Common errors
-
Trouble installing puppeteer?
cause Puppeteer, a core dependency, can sometimes encounter installation issues due to its binary dependencies or restrictive npm permissions.fixTry installing Puppeteer specifically with `npm install puppeteer --unsafe-perm=true` if you encounter persistent installation failures, especially in CI environments or systems with strict permissions. Ensure you have necessary build tools installed for Chromium.
Warnings
- gotcha Font rendering differences across operating systems can cause visual regression tests to fail unexpectedly. The framework's default behavior makes text invisible to mitigate this, but `—show-text` option can be used if text visibility is crucial for a test.
- gotcha When using `browser-ui-test` with Docker, bind-mounting volumes requires absolute paths. Relative paths will not be correctly interpreted by Docker, leading to file not found errors or incorrect file output locations.
- gotcha The framework uses a custom `.goml` scripting language, which requires users to learn its specific syntax and command list. This can be a barrier to entry for those accustomed to JavaScript/TypeScript-based testing frameworks.
Install
-
npm install browser-ui-test -
yarn add browser-ui-test -
pnpm add browser-ui-test
Imports
- runTest
const { runTest } = require('browser-ui-test');import { runTest } from 'browser-ui-test'; - runTests
const { runTests } = require('browser-ui-test');import { runTests } from 'browser-ui-test'; - loadBrowser
const { loadBrowser } = require('browser-ui-test');import { loadBrowser } from 'browser-ui-test';
Quickstart
/* my-first-test.goml */
// Go to a simple web page
go-to: "https://example.com"
// Assert the title of the page
assert-title: "Example Domain"
// Click on a link and then assert new page title
click: ("a")
assert-title: "IANA — Example domains"
// Take a screenshot and compare it (default behavior if no explicit assert fails)
// If the page changes visually, this test will fail on subsequent runs.
/* run-test.js */
import { runTest } from 'browser-ui-test';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function executeTest() {
const testFilePath = path.join(__dirname, 'my-first-test.goml');
console.log(`Running test: ${testFilePath}`);
try {
const result = await runTest(testFilePath, {
// Optional arguments, e.g., to output screenshots to a specific folder
'failure-folder': path.join(__dirname, 'test-failures'),
'screenshot-folder': path.join(__dirname, 'test-screenshots'),
'save-expected': true // Set to true initially to generate expected screenshots
});
if (result.success) {
console.log('Test passed successfully!');
} else {
console.error('Test failed:', result.error);
}
} catch (e) {
console.error('An error occurred during test execution:', e);
}
}
executeTest();