-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
91 lines (75 loc) · 2.5 KB
/
utils.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
class Utils {
constructor (page) {
this.page = page;
}
async click (identificator) {
const {elementHandle} = await this.getElement(identificator);
await elementHandle.click();
}
async doubleClick (identificator) {
const {elementHandle} = await this.getElement(identificator);
await elementHandle.click({clickCount: 1});
await elementHandle.click({clickCount: 2});
}
extractIdentificatorObject (inputData) {
const {label, placeholder, tag} = inputData;
return {label, placeholder, tag};
}
async getElementHandleByXpath (elementPath, options = {}) {
const {
externalPath,
index,
} = options;
let xPath = (externalPath || '') + elementPath;
if (typeof index !== 'undefined') {
xPath = '(' + xPath + ')[' + index + ']';
}
await this.page.waitForXPath(xPath);
const elements = await this.page.$x(xPath);
if (elements.length > 1 && typeof index === 'undefined') {
throw new Error('xPath ' + xPath + ' found more than once and options.index is not specified');
}
return elements[0];
}
async getElement (identificator) {
const {
label,
options,
placeholder,
siblingTag,
tag,
text,
} = identificator;
let {xPath} = identificator;
if (xPath && tag) throw new Error('Identificator can not have both "xPath" and "tag" keys');
if (!xPath && !tag) throw new Error('Identificator must have one of the following keys: "xPath" or "tag"');
if (xPath) {
const elementHandle = await this.getElementHandleByXpath(xPath, options);
return {elementHandle, xPath};
}
xPath = '//' + tag;
if (tag === 'input') {
if (text) throw new Error('Identificator can not have "text" key when "tag" key is "input"');
if (label) {
const {elementHandle: labelElement} = await this.getElement({tag: 'label', text: label});
const inputId = await this.page.evaluate((element) => element.getAttribute('for'), labelElement);
xPath += '[@id = "' + inputId + '"]';
}
if (placeholder) {
xPath += '[@placeholder = "' + placeholder + '"]';
}
}
if (text) {
xPath += '[text() = "' + text + '"]';
}
if (siblingTag) {
xPath += '//following-sibling::' + siblingTag;
}
const elementHandle = await this.getElementHandleByXpath(xPath, options);
return {elementHandle, xPath};
}
async waitForElement (identificator) {
await this.getElement(identificator);
}
}
module.exports = Utils;