Skip to content

Commit d9373e6

Browse files
author
Lucas Bugnazet
committed
WIP
1 parent ae9c29e commit d9373e6

File tree

3 files changed

+118
-13
lines changed

3 files changed

+118
-13
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,15 @@ After a while of fiddling around here are some findings :
5454
- Puppeteer for navigation and screenshot
5555
- ImageMagick for image manipulation
5656
- A web interface to choose what URL, what size you want to compare screenshots, and at which interval
57+
58+
59+
## Packages
60+
### Installed
61+
- Node
62+
- Puppeteer
63+
64+
### Looking into
65+
- Resemble.js [https://github.com/HuddleEng/Resemble.js]
66+
- looks-same [https://github.com/gemini-testing/looks-same]
67+
- pixelmatch [https://github.com/mapbox/pixelmatch]
68+
- ImageMagick

puppeteer.js

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,86 @@
11
const puppeteer = require('puppeteer');
22
const devices = require('puppeteer/DeviceDescriptors');
3+
const sleep = require('util').promisify(setTimeout);
34

45
// XXX 1. Get URL to take screenshot
56
let URL = "http://lucasbugnazet.com";
67

78
// XXX 2. Get viewport width
89
// height doesn't matter since we will take a full size screenshot
9-
let viewport = {width: 1080, height :1};
10+
let viewport = {width: 1920, height: 1080};
11+
var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
1012

11-
function takescreenshot(URL, viewport) {
13+
function delay(timeout) {
14+
return wait(timeout).then(() => console.log('waited for ' + timeout + ' ms'));
15+
};
16+
17+
const delay2 = async (timeout) => {
18+
await wait(timeout);
19+
console.log('waited for ' + timeout + ' ms');
20+
};
21+
22+
function gotopage(URL, viewport) {
1223
(async () => {
13-
const browser = await puppeteer.launch();
14-
const page = await browser.newPage();
24+
const browser = await puppeteer.launch({headless: false, devtools: true});
25+
let pages = await browser.pages();
26+
const page = await pages[0];
1527
await page.setViewport(viewport);
1628
await page.goto(URL);
1729

1830
// XXX 3. If page contains videos, sliders, ads, or anything that moves in page : there is a risk that the screenshot will be different than the last one and thus warn the user even though the page has not changed.
1931
// 3.1 Ask user confirmation
2032
// 3.1.1 Either select specific page tag and this will leave it out ===> this uses code-based comparison
21-
// ====> Replaces the selected tag and everything inside with a same-sized div with a light blue background. DESTRUCTIVE METHOD
33+
// ====> Replaces the selected tag and everything inside with a same-sized div with a light blue background. DESTRUCTIVE METHOD
2234
// 3.1.2 Or draw on top of page where it shouldn't take the screenshot ===> this requires imagemagick to handle the response. NON-DESTRUCTIVE METHOD
2335
// 3.2 Artificial intelligence ?
2436

37+
// XXX Current solution : open browser at "config time" (define config time : first run of set up for screenshots), toggle off specific divs/tags for pixel match. This will draw SVGs on top of image but will not destruct code.
38+
39+
2540
// XXX 4. If a difference has been found in the images or the code, draw a red rectangle around what area (or tags) have changed
2641
// ====> this will require imagemagick ?
2742
// ====> or can I just adjust the CSS in the page itself ?
2843

29-
const screenshot = await page.screenshot({
30-
fullPage: true,
31-
type: 'png',
32-
path: Date.now() + '.png'
33-
});
44+
const resultsSelector = '#heroVideo';
45+
await page.waitForSelector(resultsSelector);
46+
47+
const eval = await page.evaluate(resultsSelector => {
48+
delay(5000);
49+
const anchors = document.querySelector(resultsSelector);
50+
// console.log(anchors);
51+
const offsets = anchors.getClientRects()[0];
52+
console.log(offsets);
53+
console.log(anchors.getBoundingClientRect());
54+
console.log(anchors.getBoundingClientRect());
55+
let svgContainer = document.createElement("div");
56+
svgContainer.setAttribute("class", "svg-container");
57+
svgContainer.style.position = 'absolute';
58+
svgContainer.style.top = offsets.top + "px";
59+
svgContainer.style.left = offsets.left + "px";
60+
svgContainer.style.zIndex = 999999999999999999;
61+
var svg = document.createElementNS("http://www.w3.org/2000/svg", 'svg');
62+
var rect = document.createElementNS("http://www.w3.org/2000/svg", 'rect');
63+
svg.setAttribute("width", offsets.width) //Set svg's data
64+
svg.setAttribute("height", offsets.height) //Set svg's data
65+
rect.setAttribute("width", "100%") //Set rect's data
66+
rect.setAttribute("height", "100%") //Set rect's data
67+
rect.setAttribute("fill", "red") //Set rect's data
68+
svg.appendChild(rect);
69+
svgContainer.appendChild(svg);
70+
document.body.appendChild(svgContainer);
71+
72+
return anchors;
73+
}, resultsSelector);
74+
75+
// const screenshot = await page.screenshot({
76+
// fullPage: true,
77+
// type: 'png',
78+
// path: Date.now() + '.png'
79+
// });
80+
81+
// await browser.close();
3482

35-
await browser.close();
3683
})();
37-
}
84+
};
3885

39-
takescreenshot(URL, viewport);
86+
gotopage(URL, viewport);

test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const puppeteer = require('puppeteer');
2+
const devices = require('puppeteer/DeviceDescriptors');
3+
4+
// XXX 1. Get URL to take screenshot
5+
let URL = "http://lucasbugnazet.com";
6+
7+
// XXX 2. Get viewport width
8+
// height doesn't matter since we will take a full size screenshot
9+
let viewport = {width: 1920, height: 1080};
10+
// let wait = ms => new Promise(resolve => setTimeout(resolve, ms));
11+
12+
function delay(timeout) {
13+
return new Promise((resolve) => {
14+
setTimeout(resolve, timeout);
15+
});
16+
};
17+
18+
function isAsync(fn) {
19+
console.log(fn.constructor.name);
20+
if(fn.constructor.name === 'AsyncFunction') {
21+
console.log('Async');
22+
return true;
23+
}
24+
};
25+
26+
function gotopage(URL, viewport) {
27+
(async () => {
28+
const browser = await puppeteer.launch({headless: false, devtools: true});
29+
let pages = await browser.pages();
30+
const page = await pages[0];
31+
// await page.setViewport(viewport);
32+
await page.goto(URL);
33+
34+
await page.evaluate(() => {
35+
console.log('start');
36+
console.time('delay');
37+
// await delay(3000);
38+
console.timeEnd('delay');
39+
});
40+
41+
await browser.close();
42+
})();
43+
};
44+
45+
gotopage(URL, viewport);
46+
isAsync(gotopage);

0 commit comments

Comments
 (0)