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 is defined or has a given value : .val() |
|
33 * s : CSS selector |
|
34 * e : expected value, if omited, test existence |
|
35 */ |
|
36 function test_val (s, e) { |
|
37 e = typeof e == 'undefined' ? '' : e; // .val() returns defined ? '' : undefined; // if no value |
|
38 test (s+' is '+e, dsl(function () { |
|
39 expect (elt (s).val ()).toMatch (new RegExp (e, 'm')); |
|
40 })); |
|
41 } |
|
42 |
|
43 function test_exist (s) { |
|
44 test_val (s); |
|
45 } |
|
46 |
|
47 /** Test if the selected DOM element has the given text : .text() |
|
48 * s : CSS selector |
|
49 * e : expected value |
|
50 * v : should we test a visible value ? |
|
51 */ |
|
52 function test_text (s, e, v) { |
|
53 v = typeof v == 'undefined' ? true : v; |
|
54 test (s+' has text '+e, dsl(function () { |
|
55 expect (elt (s, v).text ()).toBe (e); |
|
56 })); |
|
57 } |
|
58 |
|
59 /** Test if the selected DOM element .text() value match the given regexp |
|
60 * s : CSS selector |
|
61 * r : regexp to match |
|
62 */ |
|
63 function test_match (s, r) { |
|
64 test (s+' text match '+r, dsl(function () { |
|
65 expect (elt (s).text ()).toMatch (r); |
|
66 })); |
|
67 } |
|
68 |
|
69 /** Count selector occurences |
|
70 * s : CSS selector |
|
71 * e : expected count |
|
72 */ |
|
73 function test_count (s, e) { |
|
74 test (s+' count is '+e, dsl(function () { |
|
75 expect (elt (s).count ()).toBe (e); |
|
76 })); |
|
77 } |
|
78 |
|
79 /** Test Django form field presence |
|
80 */ |
|
81 function test_field (form_id, field_id, type, position, label, mandatory) { |
|
82 test ('has a '+label+' form field', dsl(function () { |
|
83 var s = ''; |
|
84 |
|
85 switch (type) { |
|
86 case 'textarea':s = 'textarea#'+field_id; break; |
|
87 case 'select': s = 'select#'+field_id; break; |
|
88 default: s = 'input#'+field_id+'[type="'+type+'"]'; |
|
89 } |
|
90 |
|
91 expect (elt (s).val ()).toBeDefined (); |
|
92 expect (elt ('#'+form_id+' :input:eq('+position+')#'+field_id).val ()).toBeDefined (); |
|
93 expect (elt ('label[for='+field_id+']').text ()).toBe (label); |
|
94 |
|
95 if (mandatory) |
|
96 expect (elt ('label[for='+field_id+'] + span.required_star').val ()).toBeDefined (); |
|
97 })); |
|
98 } |
|
99 |
|
100 /** |
|
101 * Fill form field |
|
102 * s : form field id |
|
103 * v : array containing the value to use |
|
104 */ |
|
105 function test_fill_field (s, v) { |
|
106 test ('set '+s, dsl(function () { |
|
107 input (s).enter (v[s]); |
|
108 })); |
|
109 } |
|
110 |
|
111 /** |
|
112 * Click somewhere |
|
113 * s : selector of the item to click on |
|
114 * v : is the element visible or not |
|
115 */ |
|
116 function test_click (s, v) { |
|
117 test ('click '+s, dsl(function () { |
|
118 elt (s, v).click (); |
|
119 })); |
|
120 } |
|
121 |
|
122 /** |
|
123 * Submit a form |
|
124 * s : selector of the submit button |
|
125 */ |
|
126 function test_submit (s) { |
|
127 test ('submit '+s, dsl(function () { |
|
128 elt (s).click (); |
|
129 browser.waitForPageLoad (); |
|
130 })); |
|
131 } |
|
132 |
|
133 /** |
|
134 * Reload a page |
|
135 */ |
|
136 function test_reload () { |
|
137 test ('reload current page', dsl(function () { |
|
138 browser.reload (); |
|
139 })); |
|
140 } |
|
141 |
|
142 /** |
|
143 * Fails a test |
|
144 */ |
|
145 function test_fail () { |
|
146 test ('must fail', dsl(function () { |
|
147 throw 'have been programmed to fail now'; |
|
148 })); |
|
149 } |
|
150 |
|
151 /** |
|
152 * Have the browser waiting while you inspect what's going on |
|
153 */ |
|
154 function test_pause () { |
|
155 test ('pause', dsl(function () { |
|
156 browser.pause (); |
|
157 })); |
|
158 } |
|
159 |
|
160 /** |
|
161 * Start back the testcases |
|
162 */ |
|
163 function test_resume () { |
|
164 test ('resume', dsl(function (){ |
|
165 browser.resume (); |
|
166 })); |
|
167 } |
|
168 |
|
169 /** Ensure the given element is visible |
|
170 * s : CSS selector of the DOM element to check |
|
171 * v : should the element being visible |
|
172 */ |
|
173 function elt (s, v) { |
|
174 return element (s + (v ? ':visible' : '')); |
|
175 } |
|
176 |
|