Slide 1

Slide 1 text

Visual Regression Testing with PhantomCSS Pavels Jelisejevs, C.T.Co

Slide 2

Slide 2 text

By "visual regression testing" (VRT) I mean: Tools that allow to automatically check if the UI is displayed correctly, including the dimensions of elements, colors, images and etc.

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Why would we want to automate this? Because people are bad at detecting visual changes. This phenomenon is referred to as "change blindness".

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Major use cases for VRT are: •Testing the appearance of UI elements on the page: headers, footers, button etc for visual defects •Testing responsive designs •Testing complex visualizations (think charts, diagrams, graphs) which cannot be easily covered with unit or functional tests

Slide 14

Slide 14 text

How can we test visual aspects? We can try to retrieve certain CSS properties from elements and compare them to the benchmark: assert(element.getCss("color") === "red") Hardly an option due to the amount of code need to be written and complex maintenance.

Slide 15

Slide 15 text

How can we test visualizations? Ain't no testing framework gonna help your here

Slide 16

Slide 16 text

Introducing PhantomCSS PhantomCSS is a tool that allows you to: 1. Open a page and take a screenshot of an element 2. Store the screenshot as a baseline 3. After a change is done, take another screenshot and compare it to the baseline

Slide 17

Slide 17 text

What's under the hood? PhantomCSS uses: •PhantomJS 2 or SlimerJS for browsing •CasperJS for interacting with the browser •Resemble.js for screenshot comparison •Python required for CasperJS

Slide 18

Slide 18 text

How does the test look like? casper.
 start( url )
 .then(function() {
 // Take a screenshot of the UI component
 phantomcss.screenshot('#my-elemenmt', 'a screenshot of my element');
 
 })
 .then(function() {
 // compare screenshots
 phantomcss.compareAll();
 })
 .run(function() {
 casper.test.done();
 });

Slide 19

Slide 19 text

Where can I try it out? You can try out a demo available at GitHub Run: npm install casperjs test demo/testsuite.js

Slide 20

Slide 20 text

Now what? New screenshot at C:/PhantomCSS/screenshots\coffee machine dialog_1.png

Slide 21

Slide 21 text

What if I change something? FAIL Visual change found for screenshot C:/PhantomCSS/screenshots\coffee machine dialog_1.png (21.81% mismatch) # type: fail # file: demo/testsuite.js # subject: false

Slide 22

Slide 22 text

How do I accept the change? casperjs test demo/testsuite.js --rebase

Slide 23

Slide 23 text

Additional configuration are supported You can set pass additional parameters to CasperJS PhantomCSS. Most popular ones are: - Resolution - Tolerance - Delays

Slide 24

Slide 24 text

A word of advice •PhantomCSS is not a replacement for functional tests •Take screenshots of certain elements rather than whole pages •Use your VCS system to store baseline screenshots

Slide 25

Slide 25 text

A word of advice •Set up a testing environment: •If you’re testing a whole website, mock all dynamic data •If you’re testing components - showcase them on a separate page (style guide)

Slide 26

Slide 26 text

Problems Running PhantomCSS on CI environment occasionally leads to unexpected errors.

Slide 27

Slide 27 text

Problems Changes in the dynamic content provided by the backend will cause the tests to break. How do we easily decouple them?

Slide 28

Slide 28 text

Popular questions Q: At what project stages should we use VRT? A: When you have something to take pictures of. Q: How to organise VRT in a team? A: In the same manner as all other tests.

Slide 29

Slide 29 text

Thank you! • PhantomCSS on GitHub - https://github.com/ Huddle/PhantomCSS • The phenomenon of Change Blindness - https:// en.wikipedia.org/wiki/Change_blindness

Slide 30

Slide 30 text

You’re welcome to join our team. Contact [email protected]