forked from codeceptjs/CodeceptJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocator_test.js
304 lines (269 loc) · 10.2 KB
/
locator_test.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const { expect } = require('chai');
const { DOMParser } = require('@xmldom/xmldom');
const xpath = require('xpath');
const Locator = require('../../lib/locator');
let doc;
const xml = `<body>
<span>Hey boy</span>
<p>
<span></span>
<div></div>
<div id="user" data-element="name">davert</div>
</p>
<div class="form-wrapper" id="buttons-wrapper">
<fieldset id="fieldset-buttons">
<table>
<tr>
<td>List</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<tr>
<td>Show</td>
<td>Also Edit</td>
<td>Also Delete</td>
</tr>
</table>
<div id="submit-wrapper" class="form-wrapper">
<div id="submit-label" class="form-label"> </div>
<div id="submit-element" class="form-element">
<button name="submit" id="submit" type="submit" tabindex="3">Sign In</button>
</div>
</div>
<div id="remember-wrapper" class="form-wrapper">
<div class="form-label" id="remember-label"> </div>
<div id="remember-element" class="form-element">
<input type="hidden" name="session" value="1" />
<input type="hidden" name="remember" value="please_do" />
<input type="hidden" name="agree" value="no" />
<input type="checkbox" data-value="yes" id="remember" value="1" tabindex="4" />
<label for="remember" class="optional">Remember Me</label>
</div>
</div>
<div class="form-field">
<input name="name0" label="Выберите услугу" type="text" value=""/>
</div>
<div class="form-field">
<input name="name1" label="Выберите услугу" type="text" value=""/>
</div>
</fieldset>
<label class="n-1">Hello<a href="#">Please click</a></label>
</div>
<input type="hidden" name="return_url" value="" id="return_url" />
<div class="ps-submenu-root">
<ul class="ps-submenu-root">
<li class="ps-menu-button">Adhemar da Silva</li>
<li class="ps-menu-button">HelloWorld</li>
<li class="ps-menu-button">Hallo</li>
</ul>
<ul class="ps-submenu-root">
<li class="ps-menu-button">Adhemar da Silva</li>
<li class="ps-menu-button">HelloWorld</li>
<li class="ps-menu-button">Authoring</li>
</ul>
</div>
</body>`;
describe('Locator', () => {
beforeEach(() => {
doc = new DOMParser().parseFromString(xml, 'application/xhtml+xml');
});
describe('constructor', () => {
describe('with string argument', () => {
it('should create css locator', () => {
const l = new Locator('#foo');
expect(l.type).to.equal('css');
expect(l.value).to.equal('#foo');
expect(l.toString()).to.equal('#foo');
});
it('should create xpath locator', () => {
const l = new Locator('//foo[@bar="baz"]/*');
expect(l.type).to.equal('xpath');
expect(l.value).to.equal('//foo[@bar="baz"]/*');
expect(l.toString()).to.equal('//foo[@bar="baz"]/*');
});
it('should create fuzzy locator', () => {
const l = new Locator('foo');
expect(l.type).to.equal('fuzzy');
expect(l.value).to.equal('foo');
expect(l.toString()).to.equal('foo');
});
it('should create custom locator', () => {
const l = new Locator({ custom: 'foo' });
expect(l.type).to.equal('custom');
expect(l.value).to.equal('foo');
expect(l.toString()).to.equal('{custom: foo}');
});
it('should create shadow locator', () => {
const l = new Locator({ shadow: ['my-app', 'recipe-hello-binding', 'ui-input', 'input.input'] });
expect(l.type).to.equal('shadow');
expect(l.value).to.deep.equal(['my-app', 'recipe-hello-binding', 'ui-input', 'input.input']);
expect(l.toString()).to.equal('{shadow: my-app,recipe-hello-binding,ui-input,input.input}');
});
it('should create described custom default type locator', () => {
const l = new Locator('foo', 'defaultLocator');
expect(l.type).to.equal('defaultLocator');
expect(l.value).to.equal('foo');
expect(l.toString()).to.equal('foo');
});
});
describe('with object argument', () => {
it('should create id locator', () => {
const l = new Locator({ id: 'foo' });
expect(l.type).to.equal('id');
expect(l.value).to.equal('foo');
expect(l.toString()).to.equal('{id: foo}');
});
it('should create described custom locator', () => {
const l = new Locator({ customLocator: '=foo' });
expect(l.type).to.equal('customLocator');
expect(l.value).to.equal('=foo');
expect(l.toString()).to.equal('{customLocator: =foo}');
});
});
describe('with Locator object argument', () => {
it('should create id locator', () => {
const l = new Locator(new Locator({ id: 'foo' }));
expect(l).to.eql(new Locator({ id: 'foo' }));
expect(l.type).to.equal('id');
expect(l.value).to.equal('foo');
expect(l.toString()).to.equal('{id: foo}');
});
});
});
it('should transform CSS to xpath', () => {
const l = new Locator('p > #user', 'css');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1);
expect(nodes[0].firstChild.data).to.eql('davert');
});
it('should build locator to match element by attr', () => {
const l = Locator.build('input').withAttr({ 'data-value': 'yes' });
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1);
});
it('should build locator to match element containing a text', () => {
const l = Locator.build('span').withText('Hey');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1);
});
it('should build locator to match element by exact text', () => {
const l = Locator.build('span').withTextEquals('Hey boy');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1);
});
it('should build locator to match element by position', () => {
const l = Locator.build('#fieldset-buttons')
.find('//tr')
.first()
.find('td')
.at(2);
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
expect(nodes[0].firstChild.data).to.eql('Edit');
});
it('should build complex locator', () => {
const l = Locator.build('#fieldset-buttons')
.find('tr')
.last()
.find('td')
.first();
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
expect(nodes[0].firstChild.data).to.eql('Show');
});
it('should select a by label', () => {
const l = Locator.build('a')
.withAttr({ href: '#' })
.inside(Locator.build('label').withText('Hello'));
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
expect(nodes[0].firstChild.data).to.eql('Please click', l.toXPath());
});
it('should select child element by name', () => {
const l = Locator.build('.form-field')
.withDescendant(Locator.build('//input[@name="name1"]'));
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
});
it('should select element by siblings', () => {
const l = Locator.build('//table')
.withChild(Locator.build('tr')
.withChild('td')
.withText('Also Edit'));
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
});
it('should throw an error when xpath with round brackets is nested', () => {
expect(() => {
Locator.build('tr').find('(./td)[@id="id"]');
}).to.throw('round brackets');
});
it('should find element with class name contains hyphen', () => {
const l = Locator.build('').find('.n-1').first();
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
});
it('should throw an error when locator with specific position is nested', () => {
expect(() => {
Locator.build('tr').withChild(Locator.build('td').first());
}).to.throw('round brackets');
});
it('should not select element by deep nested siblings', () => {
const l = Locator.build('//table')
.withChild('td');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(0, l.toXPath());
});
it('should select element by siblings', () => {
const l = Locator.build('//table')
.find('td')
.after(Locator.build('td').withText('Also Edit'))
.first();
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
expect(nodes[0].firstChild.data).to.eql('Also Delete', l.toXPath());
});
it('should translate locator to string', () => {
const l = Locator.build('//table')
.find('td')
.as('cell');
expect(l.toString()).to.eql('cell');
});
it('should be able to add custom locator strategy', () => {
Locator.addFilter((selector, locator) => {
if (selector.data) {
locator.type = 'css';
locator.value = `[data-element=${locator.value}]`;
}
});
const l = Locator.build({ data: 'name' });
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
expect(nodes[0].firstChild.data).to.eql('davert', l.toXPath());
Locator.filters = [];
});
it('should be able to add custom locator strategy', () => {
Locator.addFilter((providedLocator, locator) => {
if (typeof providedLocator === 'string') {
// this is a string
if (providedLocator[0] === '=') {
locator.value = `.//*[text()='${providedLocator.substring(1)}']`;
locator.type = 'xpath';
}
}
});
const l = Locator.build('=Sign In');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
expect(nodes[0].firstChild.data).to.eql('Sign In', l.toXPath());
Locator.filters = [];
});
it('should be able to locate complicated locator', () => {
const l = Locator.build('.ps-menu-button')
.withText('Authoring')
.inside('.ps-submenu-root:nth-child(2)');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
expect(nodes[0].firstChild.data).to.eql('Authoring', l.toXPath());
});
});