-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
69 lines (56 loc) · 2.09 KB
/
test.js
File metadata and controls
69 lines (56 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import puppeteer from 'puppeteer';
import fs from 'fs';
import pixelmatch from 'pixelmatch';
import { PNG } from 'pngjs';
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.setViewport({ width: 1200, height: 800 });
await page.goto('https://www.calculator.net/');
const text = await page.evaluate(() => document.querySelector('h1').innerText);
console.log('Extracted Text:', text);
await page.click('span[onclick="r(1)"]');
await page.click('span[onclick="r(5)"]');
await page.click('span[onclick="r(\'+\')"]');
await page.click('span[onclick="r(2)"]');
await page.click('span[onclick="r(0)"]');
await page.click('span[onclick="r(\'=\')"]');
await page.waitForSelector('#sciOutPut', { visible: true });
const output = await page.evaluate(() => document.querySelector('#sciOutPut').innerText.trim());
console.log('Calculated Output:', output);
if (output === '35') {
console.log('Test Passed: The output is correct.');
} else {
console.error('Test Failed: The output is incorrect.');
}
const referenceScreenshot = 'reference.png';
const testScreenshot = 'test.png';
if (!fs.existsSync(referenceScreenshot)) {
await page.screenshot({ path: referenceScreenshot });
console.log('Reference screenshot saved.');
} else {
await page.screenshot({ path: testScreenshot });
const img1 = PNG.sync.read(fs.readFileSync(referenceScreenshot));
const img2 = PNG.sync.read(fs.readFileSync(testScreenshot));
const { width, height } = img1;
const diff = new PNG({ width, height });
const mismatchedPixels = pixelmatch(
img1.data,
img2.data,
diff.data,
width,
height,
{ threshold: 0.1 }
);
if (mismatchedPixels > 0) {
fs.writeFileSync('diff.png', PNG.sync.write(diff));
console.log('Screenshots do not match. Check diff.png for differences.');
} else {
console.log('Screenshots match.');
}
}
await browser.close();
})().catch((err) => {
console.error('Error running the test:', err);
process.exit(1);
});