UNPKG

3.65 kBtext/coffeescriptView Raw
1module.exports = (browser) ->
2
3 # Assertions
4 # -----------------------------------------------------
5
6 browser.addCommand 'shouldExist', ->
7 @waitForExist(X.apply(null, arguments)).should.eventually.be.true
8
9 browser.addCommand 'shouldNotExist', ->
10 @waitForExist(X.apply(null, arguments), undefined, true).should.eventually.be.true
11
12 browser.addCommand 'shouldBeVisible', ->
13 @waitForVisible(X.apply(null, arguments)).should.eventually.be.true
14
15 browser.addCommand 'shouldNotBeVisible', ->
16 @waitForVisible(X.apply(null, arguments), undefined, true).should.eventually.be.true
17
18 browser.addCommand 'shouldExecute', ->
19 @execute.apply this, arguments
20 .then (ret) ->
21 ret.value
22 .should.eventually.be.true
23
24 # History
25 # -----------------------------------------------------
26
27 browser.addCommand 'historyPush', (url) ->
28 @execute (url) ->
29 delete window._rendered
30 app.history.push(url)
31 , url
32 .waitUntil ->
33 @execute ->
34 window._rendered
35 .then (ret) ->
36 ret.value
37
38 browser.addCommand 'historyRefresh', ->
39 @execute ->
40 delete window._rendered
41 app.history.refresh()
42 .waitUntil ->
43 @execute ->
44 window._rendered
45 .then (ret) ->
46 ret.value
47
48 # Wait for page load
49 # -----------------------------------------------------
50
51 makeWaitForPageLoad = (fnName) ->
52 # Adds additional last argument -- timeout in ms (time to wait for the page to load)
53 ->
54 timeout = arguments[arguments.length - 1]
55 args = arguments
56 if typeof timeout is 'number'
57 args = Array.prototype.slice.call arguments, 1
58 else
59 timeout = undefined
60 @execute(->
61 delete window._rendered
62 )[fnName].apply(this, args)
63 .waitUntil ->
64 @execute ->
65 window._rendered
66 .then (ret) ->
67 ret.value
68 , timeout
69
70 browser.addCommand 'urlAndWait', ->
71 @url.apply this, arguments
72 .waitUntil ->
73 @execute ->
74 window._rendered
75 .then (ret) ->
76 ret.value
77 browser.addCommand 'clickAndWait', makeWaitForPageLoad('click')
78 browser.addCommand 'submitFormAndWait', makeWaitForPageLoad('submitForm')
79
80 # Racer Model
81 # -----------------------------------------------------
82
83 modelMethod = (method, isAsync) ->
84 ->
85 @[if isAsync then 'executeAsync' else 'execute']
86 .apply this, [(method, isAsync) ->
87 done = undefined
88 lastArgIndex = arguments.length
89 if isAsync
90 done = arguments[arguments.length - 1]
91 lastArgIndex = arguments.length - 1
92 args = Array.prototype.slice.call(arguments, 2, lastArgIndex)
93 res = app.model[method].apply app.model, args.concat [->
94 done? res
95 ]
96 , method, !!isAsync].concat(Array.prototype.slice.call(arguments))
97 .then (ret) ->
98 ret.value
99
100 [ # Getters (sync)
101 'get',
102 'getCopy',
103 'getDeepCopy'
104 ].forEach (method) ->
105 browser.addCommand 'model' + method.charAt(0).toUpperCase() + method.slice(1)
106 , modelMethod(method)
107
108 [ # Setters (async)
109 'set',
110 'del',
111 'setNull',
112 'setDiff',
113 'setDiffDeep',
114 'add',
115 'increment',
116 'push',
117 'unshift',
118 'insert',
119 'pop',
120 'shift',
121 'remove',
122 'move',
123 'stringInsert',
124 'stringRemove'
125 ].forEach (method) ->
126 browser.addCommand 'model' + method.charAt(0).toUpperCase() + method.slice(1)
127 , modelMethod(method, true)
128
129 browser.addCommand 'waitForModel', ->
130 @executeAsync (cb) ->
131 _pingId = '__ping_' + app.model.id()
132 app.model.add 'service', {id: _pingId}, cb
133 .then (ret) ->
134 ret.value
\No newline at end of file