|
1 | 1 | const puppeteer = require('puppeteer'); |
2 | 2 | const devices = require('puppeteer/DeviceDescriptors'); |
| 3 | +const sleep = require('util').promisify(setTimeout); |
3 | 4 |
|
4 | 5 | // XXX 1. Get URL to take screenshot |
5 | 6 | let URL = "http://lucasbugnazet.com"; |
6 | 7 |
|
7 | 8 | // XXX 2. Get viewport width |
8 | 9 | // 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)); |
10 | 12 |
|
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) { |
12 | 23 | (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]; |
15 | 27 | await page.setViewport(viewport); |
16 | 28 | await page.goto(URL); |
17 | 29 |
|
18 | 30 | // 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. |
19 | 31 | // 3.1 Ask user confirmation |
20 | 32 | // 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 |
22 | 34 | // 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 |
23 | 35 | // 3.2 Artificial intelligence ? |
24 | 36 |
|
| 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 | + |
25 | 40 | // 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 |
26 | 41 | // ====> this will require imagemagick ? |
27 | 42 | // ====> or can I just adjust the CSS in the page itself ? |
28 | 43 |
|
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(); |
34 | 82 |
|
35 | | - await browser.close(); |
36 | 83 | })(); |
37 | | -} |
| 84 | +}; |
38 | 85 |
|
39 | | -takescreenshot(URL, viewport); |
| 86 | +gotopage(URL, viewport); |
0 commit comments