Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏗 Add new features to visual diff tests #17028

Merged

Conversation

danielrozenberg
Copy link
Member

In the visual-tests config file, add two new features for webpages:

  • "viewport": { "width": 123, "height": 456 } - set this test to have an exact viewport size. On Percy, only that explicit viewport size will be set. Wraps the document in an iframe after it's "settled" (all CSS classes are verified) because Percy doesn't understand explicit viewport sizes
  • "enable_dirty_javascript": true - allows running "dirty" (non-AMP) JS inlined in the page. This will execute that code twice, once locally when preparing the page to render and once again on Percy.

See added documentation in the PR's text for further explanation.

I tested this with @zhouyx's PR #16829, modified to scroll the page via code enabled by enable_dirty_javascript and constrained with viewport. Looks pretty good! https://percy.io/ampproject/danielrozenberg/builds/881708
(the only issue remaining for that PR to work with these changes is that the ad doesn't show up when running in --headless mode, which we should investigate later)

@danielrozenberg
Copy link
Member Author

/to @newmuis @zhouyx

Copy link
Contributor

@rsimha rsimha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, with a couple comments to consider.

document.body.appendChild(iframe);

document.body.style.margin = '0'; // eslint-disable-line amphtml-internal/no-style-property-setting
iframe.style.border = '0'; // eslint-disable-line amphtml-internal/no-style-property-setting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using eslint-disable-line, you could add this file to .eslintignore, since I don't think it makes sense to lint it.

In addition, I think there's a check somewhere that requires the AMP copyright at the top of each JS file, that you can skip for this file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* // AMP page, and enable JavaScript execution on the Percy side.
* // The code being executed *must* be inlined inside a <script> tag, not
* // be an externally loaded script via a <script src="..."> tag.
* // The code *WILL BE EXECUTED TWICE*! Once locally, before the DOM is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the past, double execution of JS code was the reason behind bugs like percy/percy-capybara#51. I assume something has changed since then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing the AMP library from the page before uploading it to percy, we can avoid a lot of unexpected behaviour. Of course, the authors of those inlined scripts should still be aware of the possibility that things would happen unexpectedly, hence why I suggest using a guard here. I'd like to see how people use this feature before we refine it (insert generic quote about premature optimization)

@codecov-io
Copy link

codecov-io commented Jul 24, 2018

Codecov Report

Merging #17028 into master will increase coverage by 0.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master   #17028      +/-   ##
==========================================
+ Coverage    77.9%   77.92%   +0.01%     
==========================================
  Files         562      562              
  Lines       41131    41131              
==========================================
+ Hits        32043    32051       +8     
+ Misses       9088     9080       -8
Flag Coverage Δ
#integration_tests 36.16% <ø> (-0.02%) ⬇️
#unit_tests 76.97% <ø> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1e1828c...ed635a8. Read the comment docs.

@danielrozenberg
Copy link
Member Author

After discussing this with @newmuis offline, renamed enable_dirty_javascript to enable_percy_javascript and clarified the doc a bit more

@danielrozenberg danielrozenberg merged commit 333e4d6 into ampproject:master Jul 24, 2018
@danielrozenberg danielrozenberg deleted the visual-diff-features branch July 24, 2018 15:38
@newmuis
Copy link
Contributor

newmuis commented Jul 25, 2018

@danielrozenberg Is it reasonable to allow multiple explicit viewport sizes? Like:

{
  "viewport": [
    { "width": 320,  "height": 480  },
    { "width": 1920, "height": 1080 }
  ]
}

@danielrozenberg
Copy link
Member Author

@newmuis I can definitely look into that. Since I freeze the page and wrap it in an iframe, this would mean a full reload of the page in the new viewport size, so it's essentially like adding a new test

@newmuis
Copy link
Contributor

newmuis commented Jul 25, 2018

@danielrozenberg whatever you prefer -- I can also just add a new test of the same page with the same params except for the viewport

@danielrozenberg
Copy link
Member Author

Looking at what you did in #17110, I think it makes sense to keep them separate, especially since the way amp-storys are rendered on mobile vs. desktop is so different. When visual diffs are hidden behind the width selector on Percy, it makes it more likely to miss a difference that matters. I'll keep the idea of adding multiple viewport sizes for the same test in the back of my head, but right now it doesn't seem like a top priority

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants