Testing Area/Visual regression testing: Difference between revisions
From Mahara Wiki
< Testing Area
(Updated documentation) |
|||
Line 1: | Line 1: | ||
===Visual Regression Testing with BackstopJS=== | ===Visual Regression Testing with BackstopJS=== | ||
https://github.com/garris/BackstopJS | |||
This is some documentation of my investigation into using BackstopJS with Mahara. In the end, the fact that the Mahara UI has so many different states that are behaviour driven, means that it would be incredibly time consuming to set up the conditions for these states in the backstop.json config. '''My conclusion was that, if we are to implement automated visual regression testing for Mahara, that it should be integrated with our current BDD test suite, i.e. Behat.''' | |||
'''Set up:''' | |||
*'''Create a new directory''' e.g. backstopjsmahara and cd into it | |||
*'''Run <code>npm i backstopjs</code>''' | |||
*'''Run <code>backstop init</code>''' | |||
*'''Get the list of urls from your local site''' with this formula in the console in devtools: '''<code>n=$$('a');for(u in n)console.log(n[u].href),</code>''' or use a web scraper (I used this tutorial to make a simple webscraper with puppeteer: [https://www.digitalocean.com/community/tutorials/how-to-scrape-a-website-using-node-js-and-puppeteer https://www.digitalocean.com/community/tutorials/how-to-scrape-a-website-using-node-js-and-puppeteer)] | |||
*'''Create a file called urls.js with the following contents:''' | |||
Then | <code>const fs = require('fs-extra'); // writes json files</code> | ||
<code>const urls = [url1, url2, etc ...]; // The list of urls</code> | |||
<code>let scenarios = [];</code> | |||
<code>urls.map(string => {</code> | |||
<code>let jsonstring = { | |||
"label": string, | |||
"url": "<your domain>" + string, | |||
} | |||
scenarios.push(jsonstring);</code> | |||
<code>})</code> | |||
<code>fs.writeJson('scenarios.json', scenarios, (err) => {</code> | |||
<code>if (err) throw err;</code> | |||
<code>});</code> | |||
* '''Run this file on cli''' with <code>node urls.js</code> | |||
*'''Copy and paste the json array from the generated scenarios.json file into the backstop.json file''' as the value for '''<code>"scenarios": (json array)</code>''' | |||
*'''Make sure your backstop.json file looks like this:''' | |||
<code>{</code> | |||
<code>"viewports": [ { | |||
"name": "Below-480", | |||
"width": 478, | |||
"height": 730</code> | |||
<code>}, | |||
{</code> | |||
<code>"name": "Below-768", | |||
"width": 766, | |||
"height": 730</code> | |||
<code>}, | |||
{</code> | |||
<code>"name": "Below-992", | |||
"width": 990, | |||
"height": 730</code> | |||
<code>}, | |||
{</code> | |||
<code>"name": "Below-1200", | |||
"width": 1198, | |||
"height": 730</code> | |||
<code>}, | |||
{</code> | |||
<code>"name": "Below-1440", | |||
"width": 1438, | |||
"height": 730</code> | |||
<code>}],</code> | |||
<code>"onReadyScript": "puppet/onReady.js", | |||
"scenarios": [{"label":"/module/multirecipientnotification/inbox.php","url":"http://mahara2/module/multirecipientnotification/inbox.php"},{"label":"/user/view.php?id=1", ...], | |||
"paths": { | |||
"bitmaps_reference": "backstop_data/bitmaps_reference", | |||
"bitmaps_test": "backstop_data/bitmaps_test", | |||
"casper_scripts": "backstop_data/casper_scripts", | |||
"html_report": "backstop_data/html_report", | |||
"ci_report": "backstop_data/ci_report" | |||
}, | |||
"casperFlags": [], | |||
"engine": "puppet", | |||
"engineOptions": { | |||
"headless": true, | |||
"browser": "chromium" | |||
}, | |||
"asyncCaptureLimit": 1, | |||
"debug": true, | |||
"report": ["browser"]</code> | |||
<code>}</code> | |||
*'''And make sure your puppet/onReady.js file looks like this:''' | |||
<code>module.exports = async (page, scenario, vp) => {</code> | |||
<code>console.log('SCENARIO > ' + scenario.label); | |||
// await require('./clickAndHoverHelper')(page, scenario); | |||
// add more ready handlers here... | |||
// check for login element | |||
let loggedin; | |||
await Promise.all([ | |||
page.goto(scenario.url), | |||
page.waitForSelector('input[name="login_username"]', {timeout: 3000}), | |||
]).then(() => { | |||
loggedin = false; | |||
}).catch(() => { | |||
loggedin = true; | |||
}) | |||
if (!loggedin) { | |||
await page.type('input[name="login_username"]', 'admin'); | |||
await page.type('input[name="login_password"]', 'a'); | |||
// let element = await page.$('#login_submit') | |||
// console.log(element); | |||
await Promise.all([ | |||
page.waitForNavigation({waitUntil: 'networkidle2'}), | |||
page.click('#login_submit'), | |||
]); | |||
await page.waitForTimeout(1000); | |||
// await page.goto(scenario.url); | |||
} else { | |||
return; | |||
}</code> | |||
<code>};</code> | |||
*'''Start by running <code>backstop reference</code>''' - This will create reference screenshots for you to compare with when you run your test. | |||
* '''Then''' '''make your changes or checkout your test patch''' | |||
*'''Then run <code>backstop test</code>''' - this will take screenshots and compare them with your reference screenshots | |||
For some reason the report tries to open in Matrix... But you can view the report results by opening the generated backstop_data/html_report/index.html in a browser. | For some reason the report tries to open in Matrix... But you can view the report results by opening the generated backstop_data/html_report/index.html in a browser. | ||
If you're happy with one of the test images, then you can run <code>backstop approve</code> and pass a <code>--filter=<image_filename_regex></code> argument to promote that specific test capture matching your scenario filename. The filenames (which by default include scenario and viewport names) are displayed in the visual report. |
Revision as of 10:26, 19 July 2022
Visual Regression Testing with BackstopJS
https://github.com/garris/BackstopJS
This is some documentation of my investigation into using BackstopJS with Mahara. In the end, the fact that the Mahara UI has so many different states that are behaviour driven, means that it would be incredibly time consuming to set up the conditions for these states in the backstop.json config. My conclusion was that, if we are to implement automated visual regression testing for Mahara, that it should be integrated with our current BDD test suite, i.e. Behat.
Set up:
- Create a new directory e.g. backstopjsmahara and cd into it
- Run
npm i backstopjs
- Run
backstop init
- Get the list of urls from your local site with this formula in the console in devtools:
n=$$('a');for(u in n)console.log(n[u].href),
or use a web scraper (I used this tutorial to make a simple webscraper with puppeteer: https://www.digitalocean.com/community/tutorials/how-to-scrape-a-website-using-node-js-and-puppeteer) - Create a file called urls.js with the following contents:
const fs = require('fs-extra'); // writes json files
const urls = [url1, url2, etc ...]; // The list of urls
let scenarios = [];
urls.map(string => {
let jsonstring = {
"label": string,
"url": "<your domain>" + string,
}
scenarios.push(jsonstring);
})
fs.writeJson('scenarios.json', scenarios, (err) => {
if (err) throw err;
});
- Run this file on cli with
node urls.js
- Copy and paste the json array from the generated scenarios.json file into the backstop.json file as the value for
"scenarios": (json array)
- Make sure your backstop.json file looks like this:
{
"viewports": [ {
"name": "Below-480",
"width": 478,
"height": 730
},
{
"name": "Below-768",
"width": 766,
"height": 730
},
{
"name": "Below-992",
"width": 990,
"height": 730
},
{
"name": "Below-1200",
"width": 1198,
"height": 730
},
{
"name": "Below-1440",
"width": 1438,
"height": 730
}],
"onReadyScript": "puppet/onReady.js",
"scenarios": [{"label":"/module/multirecipientnotification/inbox.php","url":"http://mahara2/module/multirecipientnotification/inbox.php"},{"label":"/user/view.php?id=1", ...],
"paths": {
"bitmaps_reference": "backstop_data/bitmaps_reference",
"bitmaps_test": "backstop_data/bitmaps_test",
"casper_scripts": "backstop_data/casper_scripts",
"html_report": "backstop_data/html_report",
"ci_report": "backstop_data/ci_report"
},
"casperFlags": [],
"engine": "puppet",
"engineOptions": {
"headless": true,
"browser": "chromium"
},
"asyncCaptureLimit": 1,
"debug": true,
"report": ["browser"]
}
- And make sure your puppet/onReady.js file looks like this:
module.exports = async (page, scenario, vp) => {
console.log('SCENARIO > ' + scenario.label);
// await require('./clickAndHoverHelper')(page, scenario);
// add more ready handlers here...
// check for login element
let loggedin;
await Promise.all([
page.goto(scenario.url),
page.waitForSelector('input[name="login_username"]', {timeout: 3000}),
]).then(() => {
loggedin = false;
}).catch(() => {
loggedin = true;
})
if (!loggedin) {
await page.type('input[name="login_username"]', 'admin');
await page.type('input[name="login_password"]', 'a');
// let element = await page.$('#login_submit')
// console.log(element);
await Promise.all([
page.waitForNavigation({waitUntil: 'networkidle2'}),
page.click('#login_submit'),
]);
await page.waitForTimeout(1000);
// await page.goto(scenario.url);
} else {
return;
}
};
- Start by running
backstop reference
- This will create reference screenshots for you to compare with when you run your test.
- Then make your changes or checkout your test patch
- Then run
backstop test
- this will take screenshots and compare them with your reference screenshots
For some reason the report tries to open in Matrix... But you can view the report results by opening the generated backstop_data/html_report/index.html in a browser.
If you're happy with one of the test images, then you can run backstop approve
and pass a --filter=<image_filename_regex>
argument to promote that specific test capture matching your scenario filename. The filenames (which by default include scenario and viewport names) are displayed in the visual report.