forked from segment-boneyard/nightmare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (145 loc) · 4.01 KB
/
index.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
var Nightmare = require('./lib/nightmare')
var template = require('./lib/javascript')
var vm = require('vm')
var sliced = require('sliced')
module.exports = Nightmare
/**
* Add support for iframes
*/
Nightmare.prototype.evaluate_now = function (js_fn, done) {
var args = Array.prototype.slice.call(arguments).slice(2)
var argsList = JSON.stringify(args).slice(1, -1)
var sendJsFn = String(function(){
var document = __nightmare.currentDocument
return (js_fn).apply(this, arguments)
})
sendJsFn = sendJsFn.replace('js_fn', String(js_fn))
var source = template.execute({ src: sendJsFn, args: argsList })
this.child.call('javascript', source, done)
return this
}
/*
* Enter to IFrame and run actions as if from inside
*
* @param {String} selector
* @param {Function} done
*/
Nightmare.action('enterIframe', function (selector, done) {
this.evaluate_now(function (selector) {
var element = __nightmare.currentDocument.querySelector(selector).contentDocument
__nightmare.currentDocument = element
}, done, selector)
})
/*
* Exit from all Iframes and go to the main document
*
* @param {Function} done
*/
Nightmare.action('exitIframe', function (done) {
this.evaluate_now(function() {
var element = __nightmare.rootDocument
__nightmare.currentDocument = element
}, done)
})
/**
* Additional actions.
*/
Nightmare.prototype.get = function (selector) {
this.currentSelector = selector
this.oneOrAll = 'get'
return this
}
Nightmare.prototype.getAll = function (selector) {
this.currentSelector = selector
this.oneOrAll = 'getAll'
return this
}
Nightmare.action('attribute', function (/**...attributes, done**/) {
function getElement (attrs, selector) {
var element = document.querySelector(selector)
if (attrs.length === 1) {
return element[attrs[0]]
}
else {
var res = {}
attrs.forEach( function (attr) {
res[attr] = element[attr]
})
return res
}
}
function getAllElements (attrs, selector) {
var elements = document.querySelectorAll(selector)
var arr = Array.from(elements)
return arr.map( function (element) {
if (attrs.length === 1) {
return element[attrs[0]]
}
else {
var res = {}
attrs.forEach( function (attr) {
res[attr] = element[attr]
})
return res
}
})
}
var fn = this.oneOrAll === 'get' ? getElement : getAllElements
var selector = this.currentSelector
var args = sliced(arguments)
var done = args[args.length - 1]
args.pop()
var attrs = args
this.evaluate_now(fn, done, attrs, selector)
})
Nightmare.prototype.text = function () {
this.attribute('innerText')
return this
}
/**
* Add default wait for element before actions
*/
var proto = Nightmare.prototype
proto.enterIframeProxy = proto.enterIframe
proto.enterIframe = function (/*...selectors*/) {
var args = sliced(arguments)
var selectors = args[0].constructor === Array ? args[0] : args
var self = this
function waitAndEnter (selector) {
self.wait(selector).enterIframeProxy(selector)
}
if (selectors.length === 1) {
waitAndEnter(selectors[0])
}
else {
this.exitIframe()
selectors.forEach(waitAndEnter)
}
return this
}
proto.clickProxy = proto.click
proto.click = function (selector) {
this.wait(selector).clickProxy(selector)
return this
}
proto.typeProxy = proto.type
proto.type = function (selector, text) {
this.wait(selector).typeProxy(selector, text)
return this
}
/**
* Able to use Nightmare with electron instances.
*/
Nightmare.action('evaluate', function (fn) {
// electron's ipc prevent callbacks from renderer process so need to pass the cb as a string
if (typeof fn === 'string' ) {
fn = vm.runInThisContext("("+fn+")")
}
var args = sliced(arguments)
var done = args[args.length - 1]
var newArgs = [fn, done].concat(args.slice(1, -1))
if (typeof fn !== 'function') {
return done(new Error('.evaluate() fn should be a function'))
}
this.evaluate_now.apply(this, newArgs)
})