1 // Test helpers for karma-e2e-dsl environment |
|
2 // |
|
3 // https://github.com/winsonwq/karma-e2e-dsl |
|
4 |
|
5 |
|
6 function test_page_loading (url, title) { |
|
7 test (url+' loads', dsl(function () { |
|
8 // here we are in Karma page |
|
9 browser.navigateTo (url); |
|
10 expect (element ('title').text ()).toMatch (new RegExp (title,'m')); |
|
11 // The same test agin vaniller javascript |
|
12 element ('title').text (function (page_title) { |
|
13 // here we got a value from the test iframe |
|
14 // to display the tested value : console.log (page_title); |
|
15 if (!(new RegExp (title, 'm').test (page_title))) throw 'got page '+page_title+' instead'; |
|
16 }); |
|
17 })); |
|
18 } |
|
19 |
|
20 /** Test if it's possible to change lang to the specified : |
|
21 * c : lang code |
|
22 * l : help label |
|
23 */ |
|
24 function test_i18n (c, l) { |
|
25 test ('to '+c, dsl(function () { |
|
26 element ('#footer a[href="/i18n/setlang/'+c+'/"]').click (); |
|
27 browser.navigateTo ('/'); |
|
28 expect (elt ('#footer a[href="/help/"]').text ()).toMatch (new RegExp (l, 'm')); |
|
29 })); |
|
30 } |
|
31 |
|
32 /** Test if the selected DOM element .text() value match the given regexp |
|
33 * s : CSS selector |
|
34 * r : regexp to match |
|
35 */ |
|
36 function test_match (s, r) { |
|
37 test (s+' text match '+r, dsl(function () { |
|
38 expect (elt (s).text ()).toMatch (r); |
|
39 })); |
|
40 } |
|
41 |
|
42 /** Test if the selected DOM element has a given value : .val() |
|
43 * s : CSS selector |
|
44 * e : expected value, if omited, test existence |
|
45 */ |
|
46 function test_val (s, e) { |
|
47 e = typeof e == 'undefined' ? '' : e; // .val() returns defined ? '' : undefined; // if no value |
|
48 test (s+' is '+e, dsl(function () { |
|
49 expect (elt (s).val ()).toMatch (new RegExp (e, 'm')); |
|
50 })); |
|
51 } |
|
52 |
|
53 /** |
|
54 * |
|
55 */ |
|
56 function test_exist (s) { |
|
57 test_val (s); |
|
58 } |
|
59 |
|
60 /** Test if the selected DOM element has the given text : .text() |
|
61 * s : CSS selector |
|
62 * e : expected value |
|
63 * v : should we test a visible value ? |
|
64 */ |
|
65 function test_text (s, e, v) { |
|
66 v = typeof v == 'undefined' ? true : v; |
|
67 test (s+' has text '+e, dsl(function () { |
|
68 expect (elt (s, v).text ()).toBe (e); |
|
69 })); |
|
70 } |
|
71 |
|
72 /** Count selector occurences |
|
73 * s : CSS selector |
|
74 * e : expected count |
|
75 */ |
|
76 function test_count (s, e) { |
|
77 test (s+' count is '+e, dsl(function () { |
|
78 expect (elt (s).count ()).toBe (e); |
|
79 })); |
|
80 } |
|
81 |
|
82 /** Test Django form field presence |
|
83 */ |
|
84 function test_field (form_id, field_id, type, position, label, mandatory) { |
|
85 test ('has a '+label+' form field', dsl(function () { |
|
86 var s = ''; |
|
87 |
|
88 switch (type) { |
|
89 case 'textarea':s = 'textarea#'+field_id; break; |
|
90 case 'select': s = 'select#'+field_id; break; |
|
91 default: s = 'input#'+field_id+'[type="'+type+'"]'; |
|
92 } |
|
93 |
|
94 expect (elt (s).val ()).toBeDefined (); |
|
95 expect (elt ('#'+form_id+' :input:eq('+position+')#'+field_id).val ()).toBeDefined (); |
|
96 expect (elt ('label[for='+field_id+']').text ()).toBe (label); |
|
97 |
|
98 if (mandatory) |
|
99 expect (elt ('label[for='+field_id+'] + span.required_star').val ()).toBeDefined (); |
|
100 })); |
|
101 } |
|
102 |
|
103 /** |
|
104 * Fill form field |
|
105 * s : form field id |
|
106 * v : array containing the value to use |
|
107 */ |
|
108 function test_fill_field (s, v) { |
|
109 test ('set '+s, dsl(function () { |
|
110 input (s).enter (v[s]); |
|
111 })); |
|
112 } |
|
113 |
|
114 /** |
|
115 * Click on something |
|
116 * s : selector of the item to click on |
|
117 * w : should we wait for a page to load in the browser after the click |
|
118 * v : is the element visible or not |
|
119 */ |
|
120 function test_click (s, w, v) { |
|
121 var t = 'click '+ w ? 'to submit form ' : ''; |
|
122 test (t+s, dsl(function () { |
|
123 elt (s, v).click (); |
|
124 if (w) |
|
125 browser.waitForPageLoad (); |
|
126 })); |
|
127 } |
|
128 |
|
129 /** |
|
130 * Send submit event to the given form |
|
131 * s : selector of the form to submit |
|
132 */ |
|
133 function test_submit (s) { |
|
134 test ('send submit event to '+s, dsl(function () { |
|
135 elt (s).submit (); |
|
136 browser.waitForPageLoad (); |
|
137 })); |
|
138 } |
|
139 |
|
140 |
|
141 /** |
|
142 * Reload a page |
|
143 */ |
|
144 function test_reload () { |
|
145 test ('reload current page', dsl(function () { |
|
146 browser.reload (); |
|
147 })); |
|
148 } |
|
149 |
|
150 /** |
|
151 * Fails a test |
|
152 */ |
|
153 function test_fail () { |
|
154 test ('must fail', dsl(function () { |
|
155 throw 'have been programmed to fail now'; |
|
156 })); |
|
157 } |
|
158 |
|
159 /** |
|
160 * Have the browser waiting while you inspect what's going on |
|
161 */ |
|
162 function test_pause () { |
|
163 test ('pause', dsl(function () { |
|
164 browser.pause (); |
|
165 })); |
|
166 } |
|
167 |
|
168 /** |
|
169 * Start back the testcases |
|
170 */ |
|
171 function test_resume () { |
|
172 test ('resume', dsl(function (){ |
|
173 browser.resume (); |
|
174 })); |
|
175 } |
|
176 |
|
177 /** Ensure the given element is visible |
|
178 * s : CSS selector of the DOM element to check |
|
179 * v : should the element being visible |
|
180 */ |
|
181 function elt (s, v) { |
|
182 return element (s + (v ? ':visible' : '')); |
|
183 } |
|
184 |
|