-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerators.spec.js
116 lines (94 loc) · 3.57 KB
/
generators.spec.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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const expect = require('chai').expect;
const jsdom = require('jsdom');
const generators = require('../src/generators');
function newDocument() {
const options = {};
const doc = jsdom.jsdom(null, options);
doc.parent = doc.defaultView;
return doc;
}
function simpleDocument() {}
describe('generators', () => {
describe('getElementTreeXPath', () => {
it('should work in simple DOM case', () => {
const document = newDocument();
const parentDiv = document.createElement('div');
const childDiv1 = document.createElement('div');
const childDiv2 = document.createElement('div');
childDiv2.className = 'test';
parentDiv.appendChild(childDiv1);
parentDiv.appendChild(childDiv2);
document.body.appendChild(parentDiv);
let xpathResult;
xpathResult = generators.getElementTreeXPath(childDiv1);
expect(xpathResult).to.be.equal(
'/html/body/div/div[1]',
"Can't find nested div appropriately."
);
xpathResult = generators.getElementTreeXPath(childDiv2);
expect(xpathResult).to.be.equal(
'/html/body/div/div[2]',
"Can't find nested div appropriately."
);
xpathResult = generators.getElementTreeXPath(parentDiv);
expect(xpathResult).to.be.equal(
'/html/body/div',
"Can't find non-leaf div appropriately."
);
xpathResult = generators.getElementTreeXPath(document.body);
expect(xpathResult).to.be.equal(
'/html/body',
"Can't find body appropriately."
);
});
it('should support returning XPath as a tree', () => {
const document = newDocument();
const parentDiv = document.createElement('div');
const childDiv1 = document.createElement('div');
const childDiv2 = document.createElement('div');
childDiv2.className = 'test';
parentDiv.appendChild(childDiv1);
parentDiv.appendChild(childDiv2);
document.body.appendChild(parentDiv);
const xpathResult = generators.getElementTreeXPath(childDiv1, false);
expect(xpathResult).to.have.length(4);
expect(xpathResult.nodes[0].tag).to.be.equal('html');
});
});
describe('getElementAttributes', () => {
it('should be able to get a single attribute from an element', () => {
const document = newDocument();
const childDiv2 = document.createElement('div');
childDiv2.align = 'center';
const attributes = generators.getElementAttributes(childDiv2);
expect(attributes).to.have.property('align', 'center');
});
it('should work in the no attribute case', () => {
const document = newDocument();
const div = document.createElement('div');
const attributes = generators.getElementAttributes(div);
expect(attributes).to.be.empty;
});
});
describe('getElementXPath', () => {
it('should work in simple DOM case', () => {
const document = newDocument();
const parentDiv = document.createElement('div');
const childDiv1 = document.createElement('div');
const childDiv2 = document.createElement('div');
childDiv2.class = 'test';
childDiv2.id = 'identifier';
parentDiv.appendChild(childDiv1);
parentDiv.appendChild(childDiv2);
document.body.appendChild(parentDiv);
let xpathResult;
xpathResult = generators.getElementXPath(childDiv2);
expect(xpathResult).to.be.equal('//*[@id="identifier"]');
xpathResult = generators.getElementXPath(childDiv2, true);
expect(xpathResult).to.be.equal(
'/html/body/div/div[2]',
'Expected non-ID lookup.'
);
});
});
});