axe-core icon indicating copy to clipboard operation
axe-core copied to clipboard

MUI contained button colour contrast false-positive

Open niall-nai opened this issue 1 year ago • 11 comments

Product

axe-core

Product Version

4.10.2

Latest Version

  • [x] I have tested the issue with the latest version of the product

Issue Description

Expectation

The '@axe-core/playwright' package reports the correct colour contrast on a button and matches the output from the axe Dev Tools chrome extension

Actual

The colour contrast on a component is reported as being lower than the required when a MUI button has variant="contained" selected. It seems the colours are being reported incorrectly

How to Reproduce

Create a simple component using MUI:

<Button variant="contained">
	Process
</Button>

This is themed with the following:

createTheme(palette: {
	mode: 'dark',
	background: {
		default: '#030303',
		paper: '#1e1e1e',
	},
	text: {
		primary: '#ffffff',
		secondary: '#b0b0b0',
	},
	primary: {
		main: '#90caf9',
		contrastText: '#000',
	},
})

Executing a check with the standard

await scanner.analyze()

reports the following message:

"Element has insufficient color contrast of 2.76 (foreground color: #39434b, background color: #668aa6, font size: 10.5pt (14px), font weight: normal). Expected contrast ratio of 4.5:1"

Additional context

The use of contrastText: '#000' is illustrative here- removing it instead reports a contrast of 1.34, however it gives evidence that the foreground colour is not a result of a missed alpha value

This issue has started to be reported by the testing framework recently; no changes have been made to the components themselves or the surroundings. Possibly an issue with a new browser version; reported in Chromium and Webkit but not Firefox

The axe Dev Tools Chromium extension reports no issues.

niall-nai avatar Jun 11 '25 09:06 niall-nai

@niall-nai Can you provide a URL to reproduce the issue on?

WilcoFiers avatar Jun 11 '25 10:06 WilcoFiers

Sorry @WilcoFiers this isn't on a public-facing website so that's not possible

niall-nai avatar Jun 11 '25 10:06 niall-nai

Would it be possible to set up a codepen or something similar? I'd like to make sure we're looking at the same issue you're seeing.

WilcoFiers avatar Jun 11 '25 10:06 WilcoFiers

Sure, I'll look into it. Might take a bit of time

niall-nai avatar Jun 11 '25 11:06 niall-nai

@WilcoFiers hopefully this is accessible for you:

https://codesandbox.io/p/devbox/material-ui-forked-fnclm7?workspaceId=ws_7cNVL1JV1fQaRVTtQniocp

as a minimum working example of the issue

niall-nai avatar Jun 11 '25 14:06 niall-nai

Unfortunately no, I'm not able to reproduce the issue from that. I ran axe against the sandbox page you provided. Didn't see the issue. Then I built your example on my local machine and ran axe on that. Still not getting the issue. That suggests either the repro doesn't have the issue you have locally, or there is something about the way you're running that test that's causing a problem.

Is it possible the component isn't fully rendered by the time you run axe? That's a somewhat common problem with things like these.

WilcoFiers avatar Jun 11 '25 17:06 WilcoFiers

Hm, I'm running the check both at the beforeEach and afterEach steps and it only seems to be triggering during the latter scan, however it is triggering on every occurrence of a contained button with the primary colour scheme

Is it possible that for some reason axe is 'seeing' the wrong colour? The buttons are off screen initially, but I'm seeing the same results if scrolling to them. The slightly darker colour looks similar to the colour change when the button is hovered over, but isn't quite right (I've done a manual check).

I've also tried:

  • adding an await new Promise(process.nextTick) command both before and after the call to the scanner
  • manually selecting another element (in case there was some 'hovering' behaviour)

and no luck. Is there anything else I could do to maybe see if I can resolve it? For the moment I've just added .MuiButton-containedPrimary to the scanner's excluded elements, which obviously isn't ideal

niall-nai avatar Jun 12 '25 08:06 niall-nai

I'm not sure. You'd need to tell me a bit more about your test setup.

You could try to add a longer wait time. Waiting a single ticket might not be enough since the render is happening in your browser, not in Node's event loop. Another thing to try is to take a screenshot right before or after you're running axe to check visually inspect what the page looks like.

I see nothing unusual about that button that axe wouldn't be able to process. Axe uses getComputedStyle, and this button has both an opaque text color and background color.

WilcoFiers avatar Jun 12 '25 09:06 WilcoFiers

Aha, adding the screenshot command immediately before running the scanner...

Image

It seems that the transition out from hovering over the button is what's being registered (which clearly has insufficient contrast). Adding that screenshot command actually delays the test long enough for it to have finished transitioning and to show the right value. Which would also explain why it's showing strange and inconsistent values and only fails for contained buttons

Using the suggested code at https://github.com/microsoft/playwright/issues/4055#issuecomment-1973905009 to wait for the animation to finish seems to resolve it

niall-nai avatar Jun 12 '25 11:06 niall-nai

Thanks for your help on this @WilcoFiers !

niall-nai avatar Jun 12 '25 11:06 niall-nai

Any time!

WilcoFiers avatar Jun 13 '25 11:06 WilcoFiers