Unstable VRT for buttons
Bug Description
The visual regression test for buttons has become somewhat unstable recently and fails intermittently. Specifically, the failure is related to the button in a hovered state.
Screenshots

Backstop reference image diff with test
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- Visual regression tests for buttons should consistently pass
Implementation Brief
Test Coverage
Visual Regression Changes
- Run visual regression tests and check that the buttons now pass reliably
QA Brief
Changelog entry
@Hazlitte we should not change the threshold for error to fix this, since that is essentially ignoring the problem and would reduce the effectiveness of all VRT tests. The same VRT test passes most of the time, so this issue is about figuring out why that happens and fixing this one case.
@aaemnnosttv I have spent some more time investigating this and I think that the problem is related to hoverSelector not working for the buttons stories using Storybook 6. I have tried passing the necessary parameters in a number of different ways. I think @asvinb said that he has been solving implementation problems with storybook 6, perhaps he knows a way to get this to work.
Feel free to unassign yourself @Hazlitte to let someone else take a crack at it 👍
I've thrown an hour at this and not got anywhere either, unfortunately.
For what it's worth, I reached out to @asvinb in case he had any insights but he believes this is unrelated to the issue he is tackling with storybook.
All I can see is that we are using backstop's hoverSelector parameter to get at the hover state of the button and that it seems to be working inconsistently for this test (there are other tests that use it that I haven't seen fail in the same way).
This failing VRT causes me wasted time every day :smile:
If we multiply that by every developer for every PR...
IMHO we should just disable this test until this issue is done. The cost of this test far outweighs the benefit!!
I've spent some time looking at this but have not managed to figure it out. I'm putting it back down again for now as it's starting to feels like a bit of a rabbit hole for time.
Ground covered:
Speed up debug feedback loop: I couldn't reproduce this locally, so was restricted to running the VRT tests via GitHub Actions to debug them. In order to not interfere with regular development I forked the repo to my own GitHub account. I then made some changes to get the workflow execution time down from ~6-8 mins to ~40-90 seconds (for a single run). After a while I realised it would be useful to get Backstop to just run in a loop until it failed, so did that too:
- Forked repo on GH to my own account.
- Changed default branch of repo order to tweak workflows on a new branch.
- Removed unused workflows (everything in
.github/workflowsapart fromvisual-regression.yml). - Removed
dist&node_modulesfrom.gitignore. Addednode_modules/puppeteer. - Built storybook and added
distto repo. - Added
node_modulesto repo (exceptpuppeteeras Chromium too large a file for GH).- I also reduced the dependencies in
package.jsonto the minimum required to run the tests, to keep the size ofnode_modulesdown:
- I also reduced the dependencies in
"dependencies": {
"lodash": "^4.17.21"
},
"devDependencies": {
"@babel/parser": "^7.14.8",
"@babel/traverse": "^7.14.8",
"@componentdriven/csf": "0.0.2-alpha.0",
"backstopjs": "^5.3.0",
"glob": "^7.1.7"
}
- Tweaked
visual-regression.ymlto runnpm i(to install puppeteer),npm run backstopjs -- --storybook-host=./dist/instead ofnpm run test:visualtest, in order to avoid building the Storybook every time. - Tweaks
tests/backstop/scenarios.jsto only run the Button tests:storyFiles.filter( f => f.includes( 'Button.stories' ) ).forEach ...module.exports = [ ...csfScenarios ];
- Run the tests in a loop until failure:
while npm run backstopjs -- --storybook-host=./dist/ ; do echo SUCCESS ; sleep 1 ; done ; exit 1. - See https://github.com/techanvil/site-kit-wp/tree/infra/3552-vrt-flake-experimental
Debugging: I tried the following tweaks/mods - unfortunately, the tests continued to succeed/fail randomly as per usual:
package.json:
- Bumped to
backstopjs v5.4.4(Chromium r901912).
tests/backstop/config.js:
- Checked Docker container runtime available memory - verified ~7GB.
dockerCommandTemplate: 'docker run --rm ubuntu free -m
- Checked Docker
shm-size, 64MB, tried bumping to 2GB - https://github.com/garris/BackstopJS#issues-with-chrome-headless-in-docker.dockerCommandTemplate: 'docker run --rm ubuntu df -hand check theshmline.- Then include
--shm-size=2Gin the realdockerCommandTemplatevalue: dockerCommandTemplate: `docker run --shm-size=2G --rm -i${...
- Reduced
asyncCaptureLimitandasyncCompareLimitto 1 to run the tests synchronously.
tests/backstop/viewports.js:
- Reduced to a single viewport (commented out the medium & large viewports).
tests/backstop/engine_scripts/puppet/clickAndHoverHelper.js:
- Replaced deprecated
waitForwithwaitForSelector/waitForTimeout- https://github.com/puppeteer/puppeteer/issues/6214. - Tried adding extra
waitForTimeout( 1000 )afterpage.waitForSelector( hoverSelectorIndex ). - Add
try/catcharound eachawaitto check for thrown errors - no error thrown on test failure. - Tried dumping the frame tree before all & after each await to see if there was any diff between success & failure cases but there wasn't - https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#class-frame.
- Tried waiting for the button with hover state after the call to
page.hover:page.waitForSelector('.googlesitekit-button--hover.mdc-button--raised:hover')
- Dumped button opacity and hover state after each
await, resulting opacity 0 for non-hover and 0.08 for hover. Regardless of success/failure the DOM was reporting the opacity to have changed after the call topage.hover- the opacity change is what makes the visual difference for the button.
async function dumpOpacity( page, msg ) {
const { opacity, isHover } = await page.evaluate(() => {
const opacity = window.getComputedStyle( document.querySelector('.googlesitekit-button--hover'), ':before' ).opacity
const isHover = !! document.querySelector('.googlesitekit-button--hover:hover');
return { opacity, isHover };
});
console.log('OPACITY, ISHOVER', msg, opacity, isHover);
}
Nice detective work @techanvil. This will be useful in next steps, thanks for the thorough write up 👍
This ticket won't be relevant anymore once #4184 is merged. That PR replaces the hover state with the icon button. So, once it's merged, we can close this ticket.
@eugene-manuilov that sounds good, for now at least. Let's hope we don't run into the same issue again :crossed_fingers:
With #4184 merged, this issue is no longer relevant and can be closed. 🙂