Skip to content

Instantly share code, notes, and snippets.

@AdrienLemaire
Created January 16, 2019 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdrienLemaire/8dd526446ace92290fd8f16ff3b98a7e to your computer and use it in GitHub Desktop.
Save AdrienLemaire/8dd526446ace92290fd8f16ff3b98a7e to your computer and use it in GitHub Desktop.
BackstopJS script to sort random feeds
/* backstop_data/engine_scripts/puppet/sortFeed.js */
/* eslint-disable */
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports = async (page, scenario, vp) => {
await require("./onReady")(page, scenario, vp);
// if needed, add the slowest element selector here
const readySelector = ".card-image";
await sleep(1000);
await page.waitFor(readySelector);
await page.evaluate(readySelector => {
// Add the list selector here
const list = document.querySelector(".experiences-list");
const items = list.childNodes;
const itemsArr = [];
for (var i in items) {
if (items[i].nodeType == 1) {
// get rid of the whitespace text nodes
itemsArr.push(items[i]);
}
}
itemsArr.sort((a, b) =>
a.innerHTML == b.innerHTML ? 0 : a.innerHTML > b.innerHTML ? 1 : -1,
);
for (i = 0; i < itemsArr.length; ++i) {
list.appendChild(itemsArr[i]);
}
}, readySelector);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment