Set up a mostly-default project with BackstopJS
Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
module.exports = async (page, scenario) => {
|
||||
const hoverSelector = scenario.hoverSelectors || scenario.hoverSelector;
|
||||
const clickSelector = scenario.clickSelectors || scenario.clickSelector;
|
||||
const keyPressSelector = scenario.keyPressSelectors || scenario.keyPressSelector;
|
||||
const scrollToSelector = scenario.scrollToSelector;
|
||||
const postInteractionWait = scenario.postInteractionWait; // selector [str] | ms [int]
|
||||
|
||||
if (keyPressSelector) {
|
||||
for (const keyPressSelectorItem of [].concat(keyPressSelector)) {
|
||||
await page.waitForSelector(keyPressSelectorItem.selector);
|
||||
await page.type(keyPressSelectorItem.selector, keyPressSelectorItem.keyPress);
|
||||
}
|
||||
}
|
||||
|
||||
if (hoverSelector) {
|
||||
for (const hoverSelectorIndex of [].concat(hoverSelector)) {
|
||||
await page.waitForSelector(hoverSelectorIndex);
|
||||
await page.hover(hoverSelectorIndex);
|
||||
}
|
||||
}
|
||||
|
||||
if (clickSelector) {
|
||||
for (const clickSelectorIndex of [].concat(clickSelector)) {
|
||||
await page.waitForSelector(clickSelectorIndex);
|
||||
await page.click(clickSelectorIndex);
|
||||
}
|
||||
}
|
||||
|
||||
if (postInteractionWait) {
|
||||
if (parseInt(postInteractionWait) > 0) {
|
||||
await page.waitForTimeout(postInteractionWait);
|
||||
} else {
|
||||
await page.waitForSelector(postInteractionWait);
|
||||
}
|
||||
}
|
||||
|
||||
if (scrollToSelector) {
|
||||
await page.waitForSelector(scrollToSelector);
|
||||
await page.evaluate(scrollToSelector => {
|
||||
document.querySelector(scrollToSelector).scrollIntoView();
|
||||
}, scrollToSelector);
|
||||
}
|
||||
};
|
||||
31
backstop_data/engine_scripts/playwright/interceptImages.js
Normal file
31
backstop_data/engine_scripts/playwright/interceptImages.js
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* INTERCEPT IMAGES
|
||||
* Listen to all requests. If a request matches IMAGE_URL_RE
|
||||
* then stub the image with data from IMAGE_STUB_URL
|
||||
*
|
||||
* Use this in an onBefore script E.G.
|
||||
```
|
||||
module.exports = async function(page, scenario) {
|
||||
require('./interceptImages')(page, scenario);
|
||||
}
|
||||
```
|
||||
*
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const IMAGE_URL_RE = /\.gif|\.jpg|\.png/i;
|
||||
const IMAGE_STUB_URL = path.resolve(__dirname, '../../imageStub.jpg');
|
||||
const IMAGE_DATA_BUFFER = fs.readFileSync(IMAGE_STUB_URL);
|
||||
const HEADERS_STUB = {};
|
||||
|
||||
module.exports = async function (page, scenario) {
|
||||
page.route(IMAGE_URL_RE, route => {
|
||||
route.fulfill({
|
||||
body: IMAGE_DATA_BUFFER,
|
||||
headers: HEADERS_STUB,
|
||||
status: 200
|
||||
});
|
||||
});
|
||||
};
|
||||
16
backstop_data/engine_scripts/playwright/loadCookies.js
Normal file
16
backstop_data/engine_scripts/playwright/loadCookies.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const fs = require('fs');
|
||||
|
||||
module.exports = async (browserContext, scenario) => {
|
||||
let cookies = [];
|
||||
const cookiePath = scenario.cookiePath;
|
||||
|
||||
// Read Cookies from File, if exists
|
||||
if (fs.existsSync(cookiePath)) {
|
||||
cookies = JSON.parse(fs.readFileSync(cookiePath));
|
||||
}
|
||||
|
||||
// Add cookies to browser
|
||||
browserContext.addCookies(cookies);
|
||||
|
||||
console.log('Cookie state restored with:', JSON.stringify(cookies, null, 2));
|
||||
};
|
||||
3
backstop_data/engine_scripts/playwright/onBefore.js
Normal file
3
backstop_data/engine_scripts/playwright/onBefore.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = async (page, scenario, viewport, isReference, browserContext) => {
|
||||
await require('./loadCookies')(browserContext, scenario);
|
||||
};
|
||||
6
backstop_data/engine_scripts/playwright/onReady.js
Normal file
6
backstop_data/engine_scripts/playwright/onReady.js
Normal file
@@ -0,0 +1,6 @@
|
||||
module.exports = async (page, scenario, viewport, isReference, browserContext) => {
|
||||
console.log('SCENARIO > ' + scenario.label);
|
||||
await require('./clickAndHoverHelper')(page, scenario);
|
||||
|
||||
// add more ready handlers here...
|
||||
};
|
||||
27
backstop_data/engine_scripts/playwright/overrideCSS.js
Normal file
27
backstop_data/engine_scripts/playwright/overrideCSS.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* OVERRIDE CSS
|
||||
* Apply this CSS to the loaded page, as a way to override styles.
|
||||
*
|
||||
* Use this in an onReady script E.G.
|
||||
```
|
||||
module.exports = async function(page, scenario) {
|
||||
await require('./overrideCSS')(page, scenario);
|
||||
}
|
||||
```
|
||||
*
|
||||
*/
|
||||
|
||||
const BACKSTOP_TEST_CSS_OVERRIDE = `
|
||||
html {
|
||||
background-image: none;
|
||||
}
|
||||
`;
|
||||
|
||||
module.exports = async (page, scenario) => {
|
||||
// inject arbitrary css to override styles
|
||||
await page.addStyleTag({
|
||||
content: BACKSTOP_TEST_CSS_OVERRIDE
|
||||
});
|
||||
|
||||
console.log('BACKSTOP_TEST_CSS_OVERRIDE injected for: ' + scenario.label);
|
||||
};
|
||||
Reference in New Issue
Block a user