garris / BackstopJS

Catch CSS curve balls.
http://backstopjs.org
MIT License
6.67k stars 605 forks source link

Trying to build a custom config file that pareses the sitemap xml of the site for a list of paths #1442

Open thesquaremedia opened 1 year ago

thesquaremedia commented 1 year ago

im getting TypeError: Cannot set properties of undefined (setting 'tempCompareConfigFileName') when I set in the config file module.exports = getPaths(); within that function im attempting to build the config properties scenerios being an array that gets paths push into through a for loop above before returning.

return { "id": "prod_test", "viewports": [ { "name": "small", "width": 320, "height": 480 }, { "name": "medium", "width": 760, "height": 1024 }, { "name": "xlarge", "width": 1440, "height": 900 } ], "scenarios": scenarios , "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" }, "engine": "puppeteer", "engineOptions": { "args": ["--no-sandbox"] }, "onBeforeScript": "puppet/onBefore.js", "asyncCaptureLimit": 5, "asyncCompareLimit": 50, "debug": false, "debugWindow": false };

MatthewSbar commented 1 year ago

What I've done to achieve a similar effect:

  1. Get a list of URLs you want to test against, in my case this is in a file called allpages.js, I put this in the backstop_data folder
exports.pages = [
    "/blog/",
    "/blog/some-post",
    "/",
 ]
  1. Create a backstop-base-config.json which includes your intended backstop config, not including scenarios, I put this in the backstop_data folder
  2. Create a generate-backstop.js file which looks something like this, I put this in the backstop_data folder
const pages = require('./allpages').pages;
const fs = require("fs");
const baseConfig = require('./backstop-base-config.json');

const scenarios = pages.map(page => {
    // Remove leading and trailing slashes for the label
    const label = page.replace(/^\//, "").replace(/\/$/, "");

    return {
        "label": label,
        "referenceUrl": `https://www.referenceUrl.com${page}`,
        "url": `https://www.stage.referenceUrl.com${page}`,
        "hideSelectors": [],
        "removeSelectors": [],
        "selectors": ["body"],
        "hideSelectors": [],
        "delay": 500,
        "misMatchThreshold" : 0.1,
    };
});

const finalConfig = Object.assign(
    {},
    baseConfig,
    {scenarios: scenarios}
);

fs.writeFileSync("../backstop.json", JSON.stringify(finalConfig, null, 4));

from there you can run

node generate-backstop

and generate a static backstop.json in your root directory and then run npx backstop reference or npx backstop test

You will probably need to adjust based on how you're scraping your own sitemap, but, this should be a way to do it.