-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
32 lines (24 loc) · 1.18 KB
/
app.js
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
const fs = require('fs');
const jsdom = require('jsdom');
const {getMostSimilarElementToTarget} = require('./src/getElementsSimilarity.js');
const {getElementPath} = require('./src/domPath.js');
try {
const [
originPath,
diffPath,
targetElementId = 'make-everything-ok-button'
] = process.argv.slice(2);
const sampleFile = fs.readFileSync(originPath);
const dom = new jsdom.JSDOM(sampleFile);
const targetElement = dom.window.document.getElementById(targetElementId);
// console.log(targetElement.tagName);
const diffFile = fs.readFileSync(diffPath);
const diffDom = new jsdom.JSDOM(diffFile);
let elements = Array.prototype.slice.apply(diffDom.window.document.getElementsByTagName(targetElement.tagName));
let mostSimilarElement = getMostSimilarElementToTarget(targetElement, elements);
const arrSim = Array.prototype.slice.apply(mostSimilarElement.element.attributes);
console.log(`Most similar element (similarity=${mostSimilarElement.similarity}):\n ${arrSim.map(attr => `${attr.name} = ${attr.value}`).join(', ')}`);
console.log(`Path: ${getElementPath(mostSimilarElement.element)}`);
} catch (err) {
console.error('Error trying to find element by id', err);
}