-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
358 lines (324 loc) · 11.5 KB
/
index.ts
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* Main class, containing the Algorithm.
*
* @remarks For more information on how the algorithm works, please refer to:
* Maurizio Leotta, Andrea Stocco, Filippo Ricca, Paolo Tonella. ROBULA+:
* An Algorithm for Generating Robust XPath Locators for Web Testing. Journal
* of Software: Evolution and Process (JSEP), Volume 28, Issue 3, pp.177–204.
* John Wiley & Sons, 2016.
* https://doi.org/10.1002/smr.1771
*
* @param options - (optional) algorithm options.
*/
export class RobulaPlus {
private attributePriorizationList: string[] = ['name', 'class', 'title', 'alt', 'value'];
private attributeBlackList: string[] = [
'href',
'src',
'onclick',
'onload',
'tabindex',
'width',
'height',
'style',
'size',
'maxlength',
];
constructor(options?: RobulaPlusOptions) {
if (options) {
this.attributePriorizationList = options.attributePriorizationList;
this.attributeBlackList = options.attributeBlackList;
}
}
/**
* Returns an optimized robust XPath locator string.
*
* @param element - The desired element.
* @param document - The document to analyse, that contains the desired element.
*
* @returns - A robust xPath locator string, describing the desired element.
*/
public getRobustXPath(element: Element, document: Document): string {
if (!document.body.contains(element)) {
throw new Error('Document does not contain given element!');
}
const xPathList: XPath[] = [new XPath('//*')];
while (xPathList.length > 0) {
const xPath: XPath = xPathList.shift()!;
let temp: XPath[] = [];
temp = temp.concat(this.transfConvertStar(xPath, element));
temp = temp.concat(this.transfAddId(xPath, element));
temp = temp.concat(this.transfAddText(xPath, element));
temp = temp.concat(this.transfAddAttribute(xPath, element));
temp = temp.concat(this.transfAddAttributeSet(xPath, element));
temp = temp.concat(this.transfAddPosition(xPath, element));
temp = temp.concat(this.transfAddLevel(xPath, element));
temp = [...new Set(temp)]; // removes duplicates
for (const x of temp) {
if (this.uniquelyLocate(x.getValue(), element, document)) {
return x.getValue();
}
xPathList.push(x);
}
}
throw new Error('Internal Error: xPathList.shift returns undefined');
}
/**
* Returns an element in the given document located by the given xPath locator.
*
* @param xPath - A xPath string, describing the desired element.
* @param document - The document to analyse, that contains the desired element.
*
* @returns - The first maching Element located.
*/
public getElementByXPath(xPath: string, document: Document): Element {
return document.evaluate(xPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)
.singleNodeValue as Element;
}
/**
* Returns, wheater an xPath describes only the given element.
*
* @param xPath - A xPath string, describing the desired element.
* @param element - The desired element.
* @param document - The document to analyse, that contains the desired element.
*
* @returns - True, if the xPath describes only the desired element.
*/
public uniquelyLocate(xPath: string, element: Element, document: Document): boolean {
const nodesSnapshot: XPathResult = document.evaluate(
xPath,
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null,
);
return nodesSnapshot.snapshotLength === 1 && nodesSnapshot.snapshotItem(0) === element;
}
public transfConvertStar(xPath: XPath, element: Element): XPath[] {
const output: XPath[] = [];
const ancestor: Element = this.getAncestor(element, xPath.getLength() - 1);
if (xPath.startsWith('//*')) {
output.push(new XPath('//' + ancestor.tagName.toLowerCase() + xPath.substring(3)));
}
return output;
}
public transfAddId(xPath: XPath, element: Element): XPath[] {
const output: XPath[] = [];
const ancestor: Element = this.getAncestor(element, xPath.getLength() - 1);
if (ancestor.id && !xPath.headHasAnyPredicates()) {
const newXPath: XPath = new XPath(xPath.getValue());
newXPath.addPredicateToHead(`[@id='${ancestor.id}']`);
output.push(newXPath);
}
return output;
}
public transfAddText(xPath: XPath, element: Element): XPath[] {
const output: XPath[] = [];
const ancestor: Element = this.getAncestor(element, xPath.getLength() - 1);
if (ancestor.textContent && !xPath.headHasPositionPredicate() && !xPath.headHasTextPredicate()) {
const newXPath: XPath = new XPath(xPath.getValue());
newXPath.addPredicateToHead(`[contains(text(),'${ancestor.textContent}')]`);
output.push(newXPath);
}
return output;
}
public transfAddAttribute(xPath: XPath, element: Element): XPath[] {
const output: XPath[] = [];
const ancestor: Element = this.getAncestor(element, xPath.getLength() - 1);
if (!xPath.headHasAnyPredicates()) {
// add priority attributes to output
for (const priorityAttribute of this.attributePriorizationList) {
for (const attribute of ancestor.attributes) {
if (attribute.name === priorityAttribute) {
const newXPath: XPath = new XPath(xPath.getValue());
newXPath.addPredicateToHead(`[@${attribute.name}='${attribute.value}']`);
output.push(newXPath);
break;
}
}
}
// append all other non-blacklist attributes to output
for (const attribute of ancestor.attributes) {
if (
!this.attributeBlackList.includes(attribute.name) &&
!this.attributePriorizationList.includes(attribute.name)
) {
const newXPath: XPath = new XPath(xPath.getValue());
newXPath.addPredicateToHead(`[@${attribute.name}='${attribute.value}']`);
output.push(newXPath);
}
}
}
return output;
}
public transfAddAttributeSet(xPath: XPath, element: Element): XPath[] {
const output: XPath[] = [];
const ancestor: Element = this.getAncestor(element, xPath.getLength() - 1);
if (!xPath.headHasAnyPredicates()) {
// add id to attributePriorizationList
this.attributePriorizationList.unshift('id');
let attributes: Attr[] = [...ancestor.attributes];
// remove black list attributes
attributes = attributes.filter(attribute => !this.attributeBlackList.includes(attribute.name));
// generate power set
let attributePowerSet: Attr[][] = this.generatePowerSet(attributes);
// remove sets with cardinality < 2
attributePowerSet = attributePowerSet.filter(attributeSet => attributeSet.length >= 2);
// sort elements inside each powerset
for (const attributeSet of attributePowerSet) {
attributeSet.sort(this.elementCompareFunction.bind(this));
}
// sort attributePowerSet
attributePowerSet.sort((set1: Attr[], set2: Attr[]) => {
if (set1.length < set2.length) {
return -1;
}
if (set1.length > set2.length) {
return 1;
}
for (let i: number = 0; i < set1.length; i++) {
if (set1[i] !== set2[i]) {
return this.elementCompareFunction(set1[i], set2[i]);
}
}
return 0;
});
// remove id from attributePriorizationList
this.attributePriorizationList.shift();
// convert to predicate
for (const attributeSet of attributePowerSet) {
let predicate: string = `[@${attributeSet[0].name}='${attributeSet[0].value}'`;
for (let i: number = 1; i < attributeSet.length; i++) {
predicate += ` and @${attributeSet[i].name}='${attributeSet[i].value}'`;
}
predicate += ']';
const newXPath: XPath = new XPath(xPath.getValue());
newXPath.addPredicateToHead(predicate);
output.push(newXPath);
}
}
return output;
}
public transfAddPosition(xPath: XPath, element: Element): XPath[] {
const output: XPath[] = [];
const ancestor: Element = this.getAncestor(element, xPath.getLength() - 1);
if (!xPath.headHasPositionPredicate()) {
let position: number = 1;
if (xPath.startsWith('//*')) {
position = Array.from(ancestor.parentNode!.children).indexOf(ancestor) + 1;
} else {
for (const child of ancestor.parentNode!.children) {
if (ancestor === child) {
break;
}
if (ancestor.tagName === child.tagName) {
position++;
}
}
}
const newXPath: XPath = new XPath(xPath.getValue());
newXPath.addPredicateToHead(`[${position}]`);
output.push(newXPath);
}
return output;
}
public transfAddLevel(xPath: XPath, element: Element): XPath[] {
const output: XPath[] = [];
if (xPath.getLength() - 1 < this.getAncestorCount(element)) {
output.push(new XPath('//*' + xPath.substring(1)));
}
return output;
}
private generatePowerSet(input: Attr[]): Attr[][] {
return input.reduce(
(subsets: Attr[][], value: Attr) => subsets.concat(subsets.map((set: Attr[]) => [value, ...set])),
[[]],
);
}
private elementCompareFunction(attr1: Attr, attr2: Attr): number {
for (const element of this.attributePriorizationList) {
if (element === attr1.name) {
return -1;
}
if (element === attr2.name) {
return 1;
}
}
return 0;
}
private getAncestor(element: Element, index: number): Element {
let output: Element = element;
for (let i: number = 0; i < index; i++) {
output = output.parentElement as Element;
}
return output;
}
private getAncestorCount(element: Element): number {
let count: number = 0;
while (element.parentElement) {
element = element.parentElement;
count++;
}
return count;
}
}
export class XPath {
private value: string;
constructor(value: string) {
this.value = value;
}
public getValue(): string {
return this.value;
}
public startsWith(value: string): boolean {
return this.value.startsWith(value);
}
public substring(value: number): string {
return this.value.substring(value);
}
public headHasAnyPredicates(): boolean {
return this.value.split('/')[2].includes('[');
}
public headHasPositionPredicate(): boolean {
const splitXPath: string[] = this.value.split('/');
const regExp: RegExp = new RegExp('[[0-9]]');
return splitXPath[2].includes('position()') || splitXPath[2].includes('last()') || regExp.test(splitXPath[2]);
}
public headHasTextPredicate(): boolean {
return this.value.split('/')[2].includes('text()');
}
public addPredicateToHead(predicate: string): void {
const splitXPath: string[] = this.value.split('/');
splitXPath[2] += predicate;
this.value = splitXPath.join('/');
}
public getLength(): number {
const splitXPath: string[] = this.value.split('/');
let length: number = 0;
for (const piece of splitXPath) {
if (piece) {
length++;
}
}
return length;
}
}
export class RobulaPlusOptions {
/**
* @attribute - attributePriorizationList: A prioritized list of HTML attributes, which are considered in the given order.
* @attribute - attributeBlackList: Contains HTML attributes, which are classified as too fragile and are ignored by the algorithm.
*/
public attributePriorizationList: string[] = ['name', 'class', 'title', 'alt', 'value'];
public attributeBlackList: string[] = [
'href',
'src',
'onclick',
'onload',
'tabindex',
'width',
'height',
'style',
'size',
'maxlength',
];
}