site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Unstable VRT for buttons

Open aaemnnosttv opened this issue 5 years ago • 10 comments

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

image

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

aaemnnosttv avatar Jun 10 '21 10:06 aaemnnosttv

@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 avatar Jun 15 '21 08:06 aaemnnosttv

@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.

Hazlitte avatar Jun 15 '21 16:06 Hazlitte

Feel free to unassign yourself @Hazlitte to let someone else take a crack at it 👍

aaemnnosttv avatar Jun 16 '21 08:06 aaemnnosttv

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).

johnPhillips avatar Jun 17 '21 12:06 johnPhillips

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!!

danielgent avatar Aug 11 '21 11:08 danielgent

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/workflows apart from visual-regression.yml).
  • Removed dist & node_modules from .gitignore. Added node_modules/puppeteer.
  • Built storybook and added dist to repo.
  • Added node_modules to repo (except puppeteer as Chromium too large a file for GH).
    • I also reduced the dependencies in package.json to the minimum required to run the tests, to keep the size of node_modules down:
	"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.yml to run npm i (to install puppeteer), npm run backstopjs -- --storybook-host=./dist/ instead of npm run test:visualtest, in order to avoid building the Storybook every time.
  • Tweaks tests/backstop/scenarios.js to 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 -h and check the shm line.
    • Then include --shm-size=2G in the real dockerCommandTemplate value:
    • dockerCommandTemplate: `docker run --shm-size=2G --rm -i${ ...
  • Reduced asyncCaptureLimit and asyncCompareLimit to 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 waitFor with waitForSelector/waitForTimeout - https://github.com/puppeteer/puppeteer/issues/6214.
  • Tried adding extra waitForTimeout( 1000 ) after page.waitForSelector( hoverSelectorIndex ).
  • Add try/catch around each await to 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 to page.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);
}

techanvil avatar Nov 03 '21 15:11 techanvil

Nice detective work @techanvil. This will be useful in next steps, thanks for the thorough write up 👍

aaemnnosttv avatar Nov 03 '21 15:11 aaemnnosttv

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 avatar Nov 17 '21 18:11 eugene-manuilov

@eugene-manuilov that sounds good, for now at least. Let's hope we don't run into the same issue again :crossed_fingers:

techanvil avatar Nov 17 '21 19:11 techanvil

With #4184 merged, this issue is no longer relevant and can be closed. 🙂

tofumatt avatar Nov 18 '21 21:11 tofumatt