DEV Community

Cover image for TestFly Part 7: Bundled axe-core accessibility checks and VisualAssert pixel matching
Hakan GÜL
Hakan GÜL

Posted on Edited on Originally published at hakangul.lovable.app

TestFly Part 7: Bundled axe-core accessibility checks and VisualAssert pixel matching

Accessibility and visual regression in TestFly

The TestFly core JAR bundles axe-core 4.10.2 and VisualAssert, a pixel-comparison engine.

Accessibility scans with accessibility()

Inside a BaseTest, a WCAG scan takes one line and no external dependency. The example scans the checkout page, fails on SERIOUS or CRITICAL issues and skips a third-party widget.

package io.testfly.examples.a11y;

import io.testfly.accessibility.Impact;
import io.testfly.test.BaseTest;
import org.testng.annotations.Test;

public class AccessibilityTest extends BaseTest {

    @Test
    public void checkoutPassesWcagAA() {
        open("/checkout");

        accessibility()
            .withTags("wcag2a", "wcag21aa", "wcag22aa")
            .withLevel(Impact.SERIOUS) // fails on SERIOUS or CRITICAL
            .excluding("#third-party-widget")
            .run();
    }
}
Enter fullscreen mode Exit fullscreen mode

Visual regression with VisualAssert

VisualAssert compares the full page or a single element against a baseline image.

package io.testfly.examples.visual;

import io.testfly.test.BaseTest;
import io.testfly.visual.VisualAssert;
import io.testfly.visual.VisualTolerance;
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class VisualRegressionTest extends BaseTest {

    @Test
    public void homepageVisualMatchesBaseline() {
        open("/");

        // Full-page comparison with 1% tolerance
        VisualAssert.assertScreenshot("homepage", VisualTolerance.of(1));

        // Element-scoped comparison
        VisualAssert.assertScreenshot("login-box", By.id("login-card"));
    }
}
Enter fullscreen mode Exit fullscreen mode

On the first run, TestFly generates the baseline screenshot automatically. Later runs compare against it and save any diffs to target/visual-diffs/. To refresh the baselines, run with -DupdateBaselines=true.

Top comments (0)