-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzhain-ext.js
153 lines (150 loc) · 5.89 KB
/
zhain-ext.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
zhain.ext = {
assert: chai.assert,
wait: {
forAjax: function(callback) {
$.active === 0 ? callback() : $(document).one('ajaxStop', function() { zhain.ext.wait.forAjax(callback) })
},
forThrottledAjax: function(callback) {
$(document).one('ajaxSend', function() { zhain.ext.wait.forAjax(callback) })
},
forTransitionEnd: function($locator, callback) {
$($locator.selector).one('transitionEnd webkitTransitionEnd oTransitionEnd msTransitionEnd', function() { callback() })
},
forAnimations: function(callback) {
zhain.ext.wait.until(function() { return $(":animated").length === 0 }, callback)
},
untilVisible: function($locator, callback) {
zhain.ext.wait.until(function() { return $($locator.selector).is(':visible') }, callback)
},
untilHidden: function($locator, callback) {
zhain.ext.wait.until(function() { return !$($locator.selector).is(':visible') }, callback)
},
untilExists: function($locator, callback) {
zhain.ext.wait.until(function() { return $($locator.selector).length > 0 }, callback)
},
until: function(conditionFn, callback) {
var that = this
if (conditionFn.call(that)) {
callback()
} else {
setTimeout(function() {
zhain.ext.wait.until.call(that, conditionFn, callback)
}, 10)
}
}
},
sync: {
breakpoint: function() {
debugger
},
enterVal: function($input, val) {
$($input.selector).val(val)
},
enterValAndKeyupBlur: function($input, val) {
$($input.selector).val(val).trigger('keyup').trigger('blur')
},
enterValAndKeyupChange: function($input, val) {
$($input.selector).val(val).trigger('keyup').trigger('change')
},
click: function($selector) {
$($selector.selector).click()
},
assertVal: function($locator, val) {
zhain.ext.assert.equal($($locator.selector).val(), val)
},
assertDisabled: function($locator) {
zhain.ext.assert.isTrue($($locator.selector).is(':disabled'), '$("'+$locator.selector + '") is not disabled')
},
assertEnabled: function($locator) {
zhain.ext.assert.isFalse($($locator.selector).is(':disabled'), '$("'+$locator.selector + '") is disabled')
},
assertText: function($locator, txt) {
zhain.ext.assert.equal($($locator.selector).text(), txt)
},
assertHtml: function($locator, html) {
zhain.ext.assert.equal($($locator.selector).html(), html)
},
assertVisible: function($locator) {
zhain.ext.assert.isTrue($($locator.selector).is(':visible'), '$("'+$locator.selector + '") is not visible')
},
assertNotVisible: function($locator) {
zhain.ext.assert.isFalse($($locator.selector).is(':visible'), '$("'+$locator.selector + '") is visible')
},
assertHidden: function($locator) {
zhain.ext.assert.isTrue($($locator.selector).is(':hidden'), '$("'+$locator.selector + '") is not hidden')
},
assertDoesNotExist: function($locator) {
zhain.ext.assert.equal($($locator.selector).length, 0, '$("'+$locator.selector + '") exists')
},
assertCount: function($locator, count) {
zhain.ext.assert.equal($($locator.selector).length, count, '$("'+$locator.selector + '").length')
},
assertEmpty: function($locator) {
zhain.ext.assert.isTrue($($locator.selector).is(':empty'), '$("'+$locator.selector + '") is not empty')
},
assertNotEmpty: function($locator) {
zhain.ext.assert.isFalse($($locator.selector).is(':empty'), '$("'+$locator.selector + '") is empty')
},
assertHasClass: function($locator, clazz) {
zhain.ext.assert.isTrue($($locator.selector).hasClass(clazz), '$("'+$locator.selector + '") doesn\'t have class "'+clazz+'"')
},
assertNoClass: function($locator, clazz) {
zhain.ext.assert.isFalse($($locator.selector).hasClass(clazz), '$("'+$locator.selector + '") shouldn\'t have class "'+clazz+'"')
},
assertChecked: function($locator) {
zhain.ext.assert.isTrue($($locator.selector).is(':checked'), '$("'+$locator.selector + '") is not checked')
},
assertNotChecked: function($locator) {
zhain.ext.assert.isFalse($($locator.selector).is(':checked'), '$("'+$locator.selector + '") is checked')
},
logAjax: function() {
$(document).ajaxSend(log).ajaxComplete(log)
function log(e, jqXHR, opts) {
var header = (e.type == 'ajaxComplete') ? 'DONE ' : ''
console.log(header+opts.type+' '+opts.url)
}
}
},
async: {
enterThrottledVal: function($input, val, done) {
zhain.ext.sync.enterValAndKeyupBlur($input, val, done)
zhain.ext.wait.forThrottledAjax(done)
},
ajaxClick: function($selector, done) {
$($selector.selector).click()
zhain.ext.wait.forAjax(done)
},
ajaxTrigger: function($selector, event, done) {
$($selector.selector).trigger(event)
zhain.ext.wait.forAjax(done)
}
},
register: function() {
Object.keys(zhain.ext.wait).forEach(function(fnName) {
zhain.ext.registerAsync('wait' + toTitleCase(fnName), zhain.ext.wait[fnName])
})
Object.keys(zhain.ext.sync).forEach(function(fnName) {
zhain.ext.registerSync(fnName, zhain.ext.sync[fnName])
})
Object.keys(zhain.ext.async).forEach(function(fnName) {
zhain.ext.registerAsync(fnName, zhain.ext.async[fnName])
})
function toTitleCase(s) {
return s.replace(/(?:^|\s|-)\S/g, function(c){ return c.toUpperCase() })
}
},
registerSync: function(name, fn) {
Zhain.prototype[name] = function() {
var args = Array.prototype.slice.call(arguments)
return this.do(function() { fn.apply(this, args) })
}
},
registerAsync: function(name, fn) {
Zhain.prototype[name] = function() {
var args = Array.prototype.slice.call(arguments)
return this.do(function(invokedArgs) {
var done = arguments[arguments.length - 1]
fn.apply(this, args.concat([done])) })
}
}
}