Tests/, with a directory for each library.
Every feature or bug fix added to LibWeb should have a corresponding test in Tests/LibWeb. The test should be either a Text, Layout, Ref, or Screenshot test depending on the feature. Tests of internal C++ code go in their own TestFoo.cpp file in Tests/LibWeb.
Running tests
To reproduce a CI failure, see the section on Running with sanitizers.
ladybird.py script. The LibWeb tests are registered with CMake as a test in UI/CMakeLists.txt. Using the built-in test filtering, you can run all tests with Meta/ladybird.py test or run just the LibWeb tests with Meta/ladybird.py test LibWeb. The second way is to invoke the test-web test runner directly with Meta/ladybird.py run test-web.
A third way is to invoke ctest directly. The simplest method is to use the Release preset from CMakePresets.json:
Note that some tests require the
LADYBIRD_SOURCE_DIR environment variable to be set to the root of the ladybird source tree.CTEST_OUTPUT_ON_FAILURE to 1.
Running with sanitizers
CI runs host tests with Address Sanitizer and Undefined Sanitizer instrumentation enabled. These tools catch many classes of common C++ errors, including memory leaks, out of bounds access to stack and heap allocations, and signed integer overflow. For more info on the sanitizers, check out the Address Sanitizer wiki page, or the Undefined Sanitizer documentation from clang. The sanitizers can be enabled with the-DENABLE_FOO_SANITIZER set of flags.
The simplest way to enable sanitizers is to use the Sanitizer preset.
Running the Web Platform Tests
The Web Platform Tests can be run with theWPT.sh script. This script can also be used to compare the results of two test runs.
Example usage:
Importing Web Platform Tests
You can import certain Web Platform Tests (WPT) tests into your Ladybird clone (if they’re tests of type that can be imported - and especially if any code changes you’re making cause Ladybird to pass any WPT tests it hasn’t yet been passing). Here’s how:./Meta/WPT.sh import the path part of any http://wpt.live/ URL for a WPT test you want to import. It will then download both that test and any of its JavaScript scripts, copy those to the Tests/LibWeb/<test-type>/input/wpt-import directory, run the test, and then in the Tests/LibWeb/<test-type>/expected/wpt-import directory, it will create a file with the expected results from the test.
Writing tests
Running the following python script to create new test files with correct boilerplate:test_type values are “Text”, “Layout”, “Ref”, and “Screenshot”.
This will create a new test HTML file in Tests/LibWeb/<test_type>/input with along with a corresponding expectations file in the appropriate directory in Tests/LibWeb/<test_type>/expected.
After you update/replace the generated boilerplate in your your-new-test-name.html test file with your actual test, you will need to regenerate the corresponding expectations file to match the actual output from your updated test.
Rebaselining tests
For Text or Layout tests, you can “rebaseline” the tests to regenerate the expectation file:Test types
Text tests
Text tests are intended to test Web APIs that don’t have a visual representation. They are written in JavaScript and run in a headless browser. Each test has a test function in a script tag that exercises the API and prints expected results using theprintln function. println calls are accumulated into an output test file, which is then compared to the expected output file by the test runner.
Text tests can be either sync or async. Async tests should use the done callback to signal completion. Async tests are not necessarily run in an async context, they simply require the test function to signal completion when it is done. If an async context is needed to test the API, the lambda passed to test can be async.
Layout tests
Layout tests compare the layout tree of a page with an expected one. They are best suited for testing layout code, but are also used for testing some other features that have an observable effect on the layout. No JavaScript is needed - once the page loads, the layout tree will be dumped automatically.Ref tests
Reference or “ref” tests compare a screenshot of the test page with one of a reference page. The test passes if the two are identical. These are ideal for testing visual effects such as background images or shadows. If you’re finding it difficult to recreate the effect in the reference page, (such as for SVG or canvas,) consider using a Screenshot test instead. Each Ref test includes a special<link rel="match" href="../expected/my-test-ref.html" /> tag, which the test runner uses to locate the reference page. In this way, multiple tests can use the same reference.
Screenshot tests
Screenshot tests can be thought of as a subtype of Ref tests, where the reference page is a single<img> tag linking to a screenshot of the expected output. In general, try to avoid using them if a regular Ref test would do, as they are sensitive to small rendering changes, and won’t work on all platforms.
Like Ref tests, they require a <link rel="match" href="../expected/my-test-ref.html" /> tag to indicate the reference page to use.