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