-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathelementUtil.js
224 lines (197 loc) · 8.04 KB
/
elementUtil.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
const {toElementObject} = require('./util')
const getVisible = `function getVisible(elementList) {
return Array.prototype.filter.call(elementList, function(el) {
const rects = el.getBoundingClientRect()
return (rects.height !== 0 && rects.width !== 0)
})
}`
const uploadFileToDropZoneInput = `function uploadFileToDropZone(target) {
const document = target.ownerDocument || document
const window = document.defaultView || window
const input = document.createElement('INPUT')
input.type = 'file'
input.id = '__awb_upload_file_to_dop_zone'
input.onchange = function() {
try {target.scrollIntoView(true)}
catch(e) {console.log(e)}
const rect = target.getBoundingClientRect()
const offsetX = rect.width >> 1
const offsetY = rect.height >> 1
const x = rect.left + offsetX
const y = rect.top + offsetY
const dataTransfer = {files: this.files}
const events = ['dragenter', 'dragover', 'drop']
events.forEach(function(name) {
const evt = document.createEvent('MouseEvent');
evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null)
evt.dataTransfer = dataTransfer
target.dispatchEvent(evt)
})
setTimeout(function() {document.body.removeChild(input)}, 25)
}
document.body.appendChild(input)
return input
}`
const buildHeaderHash = `function buildHeaderHash(headerCells) {
function replacer(str) {
return str.trim().replace(/( )|(:)|\\(\\d\\)/ig, '').replace(RegExp(
String.fromCharCode(10) + '|' + String.fromCharCode(160) + '|' +
String.fromCharCode(13) + '|' + String.fromCharCode(32), 'ig'), '')
}
return Array.prototype.reduce.call(headerCells, function(acc, current, index) {
const title = replacer(current.innerText.trim()).replace('*', '')
if(!title.length) {acc['EmptyTitle' + index] = index; return acc}
if(acc.hasOwnProperty(title)) {acc[title + index] = index; return acc}
acc[title] = index
return acc
}, {})
}`
const getTableHeaderObject = `function getTableHeaderObject(rootTbl, indexRowHeader) {
const buildHeaderHash = ${buildHeaderHash}
let tbl = null
if(rootTbl.tagName !== 'TABLE') {rootTbl = rootTbl.querySelector('table')}
for(let i = 0; i < rootTbl.children.length; i++) {
if(rootTbl.children[i].tagName === 'TBODY') {tbl = rootTbl.children[i]; break}
}
let header = rootTbl.querySelector('thead')
let headerCells = null
if(!header) {
header = tbl.querySelectorAll('tr')[indexRowHeader]
headerCells = header.querySelector('th') ? header.querySelectorAll('th') : header.querySelectorAll('td')
} else {
headerCells = tblHeader.querySelectorAll('th')
}
const headerHash = buildHeaderHash(headerCells)
return headerHash
}`
const getTableData = `function getTableData(rootTbl, indexRowHeader, indexRowFooter) {
const buildHeaderHash = ${buildHeaderHash}
const getVisible = ${getVisible}
let tbl = null
if(rootTbl.tagName !== 'TABLE') {rootTbl = rootTbl.querySelector('table')}
for(let i = 0; i < rootTbl.children.length; i++) {
if(rootTbl.children[i].tagName === 'TBODY') {tbl = rootTbl.children[i]; break}
}
let footer = rootTbl.querySelector('tfoot')
let header = rootTbl.querySelector('thead')
let headerCells = null
let bodyRows = null
if(!header) {
header = tbl.querySelectorAll('tr')[indexRowHeader]
headerCells = header.querySelector('th') ? header.querySelectorAll('th') : header.querySelectorAll('td')
bodyRows = Array.prototype.map.call(tbl.children, function(el) {return el})
bodyRows.splice(indexRowHeader, 1)[0]
} else {
headerCells = tblHeader.querySelectorAll('th')
bodyRows = tbl.children
}
if(typeof indexRowFooter === 'number' && !footer) {
footer = Array.prototype.map.call(bodyRows, function(el) {return el}).splice(indexRowFooter, 1)[0]
}
const headerHash = buildHeaderHash(headerCells)
const headerHashKeys = Object.keys(headerHash)
let bodyData = Array.prototype.map.call(bodyRows, function(row) {
return headerHashKeys.reduce(function(acc, current, index, hashArr) {
if(row.children.length !== hashArr.length) {
acc['Separator'] = row.innerText; return acc
}
acc[current] = {}
acc[current].text = row.children[headerHash[current]].innerText.trim()
const imgs = getVisible(row.children[headerHash[current]].querySelectorAll('img'))
const checkboxes = getVisible(row.children[headerHash[current]].querySelectorAll('input[type="checkbox"]'))
const inpts = getVisible(row.children[headerHash[current]].querySelectorAll('input[type="text"]'))
if(imgs.length) {
acc[current].imgs = imgs.reduce(function(imAcc, curIm) {
imAcc.push(curIm.src); return imAcc
}, [])
}
if(checkboxes.length) {
acc[current].checkboxes = checkboxes.reduce(function(ac, chb) {
if(chb.type !== 'hidden') {ac.push(chb.checked)} return ac
}, [])
}
if(inpts.length) {
acc[current].inputs = inpts.reduce(function(ac, inp) {
if(inp.type !== 'hidden') {ac.push(inp.value)} return ac
}, [])
}
return acc
}, {})
})
bodyData = bodyData.filter(function(rowObj) {return !!Object.keys(rowObj).length})
if(footer && (footer.querySelectorAll('tr > td').length === headerHashKeys.length)) {
const futterInfo = headerHashKeys.reduce(function(acc, current, index) {
const footerCells = footer.querySelectorAll('td')
if(footerCells[headerHash[current]].innerText.length) {
acc['footer' + current] = footerCells[index].innerText.trim()
}
return acc
}, {})
bodyData.push(futterInfo)
}
return bodyData
}`
const getOptionList = `function getOptionList(list) {
const getVisible = ${getVisible}
function buildOptionCollection(options) {
return getVisible(options).reduce(function(acc, cur, ind) {
acc.push({
index: ind,
text: cur.innerText,
value: cur.value,
isSelected: cur.selected
})
return acc
}, [])
}
if(!list.querySelector('optgroup')) {
return buildOptionCollection(list.querySelectorAll('option'))
} else {
return Array.prototype.reduce.call(list.querySelectorAll('optgroup'), function(acc, cur, ind) {
acc[cur.label.length ? cur.label : cur.title] = buildOptionCollection(cur.querySelectorAll('option'))
return acc
}, {})
}
}`
function GetElementUtils(client, element) {
this.client = client
this.element = element
this.getTableCollection = async function(indexRowHeader = 0, indexRowFooter = null) {
if(!this.element.elementId) {await this.element.getThisElement()}
return this.client.executeScript(`
const getTableData = ${getTableData};
return getTableData(arguments[0], arguments[1], arguments[2])
`, toElementObject(this.element.elementId), indexRowHeader, indexRowFooter)
}
this.uploadFileViaDnD = async function(file) {
if(!this.element.elementId) {await this.element.getThisElement()}
const elementIdBefore = this.element.elementId
const {ELEMENT} = await this.client.executeScript(`
const uploadFileToDropZoneInput = ${uploadFileToDropZoneInput};
return uploadFileToDropZoneInput(arguments[0])
`, toElementObject(this.element.elementId))
// use element as current element
this.element.elementId = ELEMENT
// use element API
await this.element.sendKeys(file)
// back element id to initial
this.element.elementId = elementIdBefore
}
this.getTableHeaderObject = async function(indexRowHeader = 0) {
if(!this.element.elementId) {await this.element.getThisElement()}
return this.client.executeScript(`
const getTableData = ${getTableHeaderObject};
return getTableData(arguments[0], arguments[1])
`, toElementObject(this.element.elementId), indexRowHeader)
}
this.getSelectListCollection = async function() {
if(!this.element.elementId) {await this.element.getThisElement()}
return this.client.executeScript(`
const getOptionList = ${getOptionList};
return getOptionList(arguments[0])
`, toElementObject(this.element.elementId))
}
}
module.exports = {
GetElementUtils
}