src/cm/scripts/test-suite/tests/tests.js
changeset 581 d50d457e7e39
parent 579 869f02c75687
child 582 63b489ad02a3
equal deleted inserted replaced
580:f634a302c45e 581:d50d457e7e39
    21 	this.timeout(150000);
    21 	this.timeout(150000);
    22 
    22 
    23 	suite ('contact page', function () {
    23 	suite ('contact page', function () {
    24 		test_page_loading ('/contact/', 'Contact');
    24 		test_page_loading ('/contact/', 'Contact');
    25 		test_unlogged_header ();
    25 		test_unlogged_header ();
    26 
    26 		test_val ('form#profile[action="."]'); // the form exists
    27 		test ('has a contact form', dsl(function () {
    27 		test_count ('form#profile .label', 5); // it has no more than 5 labels (may be no more fields)
    28 			expect (elt ('#profile[action="."]').val ()).toBeDefined ();
    28 		test_form_field ('id_name', 'text', 'Your name', true); // the field id_name is…
    29 		}));
       
    30 
       
    31 		test_form_field ('id_name', 'text', 'Your name', true);
       
    32 		test_form_field ('id_email', 'text', 'Your email address', true);
    29 		test_form_field ('id_email', 'text', 'Your email address', true);
    33 		test_form_field ('id_title', 'text', 'Subject of the message', true);
    30 		test_form_field ('id_title', 'text', 'Subject of the message', true);
    34 		test_form_field ('id_body', 'textarea', 'Body of the message', true);
    31 		test_form_field ('id_body', 'textarea', 'Body of the message', true);
    35 		test_form_field ('id_copy', 'checkbox', 'Send me a copy of the email', false);
    32 		test_form_field ('id_copy', 'checkbox', 'Send me a copy of the email', false);
    36 
    33 		test_val ('#profile input[type=submit]','Send'); // test that a the .val() of the element is
    37 		test ('has some submit and cancel buttons', dsl(function () {
    34 		test_val ('input#cancel_button[type=button]', 'Cancel');
    38 			expect (elt ('#profile input[type=submit]').val ()).toBe ('Send');
    35 		test_unlogged_footer ();
    39 			expect (elt ('input#cancel_button[type=button]').val ()).toBe ('Cancel');
    36 		test ('to check that toBeDefined test still works', dsl(function () {
       
    37 			expect (elt ('#header_controls a[href="/xxx/"]').val ()).not ().toBeDefined ();
    40 		}));
    38 		}));
    41 
    39 		test ('get back to / to avoid bugging next page load', dsl(function () {
    42 		test_unlogged_footer ();
       
    43 
       
    44 		// To avoid bugging in loading the 2nd page
       
    45 		test ('just get back to /', dsl(function () {
       
    46 			browser.navigateTo ('/');
    40 			browser.navigateTo ('/');
    47 		}));
    41 		}));
    48 	});
    42 	});
    49 
    43 
    50 	suite ('help page', function () {
    44 	suite ('help page', function () {
    54 	});
    48 	});
    55 
    49 
    56 	suite ('reset password page', function () {
    50 	suite ('reset password page', function () {
    57 		test_page_loading ('/password_reset/', 'Reset my password');
    51 		test_page_loading ('/password_reset/', 'Reset my password');
    58 		test_unlogged_header ();
    52 		test_unlogged_header ();
    59 
    53 		test_val ('form#profile[action="."]');
    60 		test ('has a contact form', dsl(function () {
    54 		test_count ('form#profile .label', 1);
    61 			expect (elt ('#profile[action="."]').val ()).toBeDefined ();
       
    62 		}));
       
    63 
       
    64 		test_form_field ('id_email', 'text', 'E-mail', true);
    55 		test_form_field ('id_email', 'text', 'E-mail', true);
    65 
    56 		test_val ('#profile input[type=submit]', 'Reset my password');
    66 		test ('has a submit button', dsl(function () {
       
    67 			expect (elt ('#profile input[type=submit]').val ()).toBe ('Reset my password');
       
    68 		}));
       
    69 
       
    70 		test_unlogged_footer ();
    57 		test_unlogged_footer ();
    71 	});
    58 	});
    72 
    59 
    73 	suite ('login page', function () {
    60 	suite ('login page', function () {
    74 		test_page_loading ('/', 'Home');
    61 		test_page_loading ('/', 'Home');
    75 		test_unlogged_header ();
    62 		test_unlogged_header ();
    76 
    63 		test_val ('form#login[action="/login/"]');
    77 		test ('has a #login form', dsl(function () {
    64 		test_count ('form#login[action="/login/"] .label', 2);
    78 			expect (elt ('#login[action="/login/"]').val ()).toBeDefined ();
       
    79 		}));
       
    80 
       
    81 		test_form_field ('id_username', 'text', 'Username', true);
    65 		test_form_field ('id_username', 'text', 'Username', true);
    82 		test_form_field ('id_password', 'password', 'Password', true);
    66 		test_form_field ('id_password', 'password', 'Password', true);
    83 
    67 		test_val ('form#login input[type=submit]', 'Login');
    84 		test ('has a login button and reset password links', dsl(function () {
    68 		test_text ('form#login a[href="/password_reset/"]', 'Forgot password?');
    85 			expect (elt ('#login input[type=submit]').val ()).toBe ('Login');
    69 		test_unlogged_footer ();
    86 			expect (elt ('#login a[href="/password_reset/"]').text ()).toBe ('Forgot password?');
    70 		// test_i18n ();
    87 		}));
       
    88 
       
    89 		test_unlogged_footer ();
       
    90 
       
    91 		test ('logs an admin in', dsl(function () {
    71 		test ('logs an admin in', dsl(function () {
    92 			input ('#id_username').enter (w.USER_ADMIN);
    72 			input ('#id_username').enter (w.USER_ADMIN);
    93 			input ('#id_password').enter (w.PASS_ADMIN);
    73 			input ('#id_password').enter (w.PASS_ADMIN);
    94 			elt ('#login input[type=submit]').click ();
    74 			elt ('#login input[type=submit]').click ();
    95 			// Must be done here in this test() block
    75 			// Must be done here in this test() block
    99 		}));
    79 		}));
   100 	});
    80 	});
   101 
    81 
   102 	suite ('admin dashboard', function () {
    82 	suite ('admin dashboard', function () {
   103 		// Should starts with :
    83 		// Should starts with :
   104 		test_page_loading ('/', 'Dashboard');
    84 		// test_page_loading ('/', 'Dashboard');
   105 		// But its the last thing we did in the previous test
    85 		// But its the last thing we did in the previous test
   106 		test_logged_header (w.USER_ADMIN);
    86 		test_logged_header (w.USER_ADMIN);
       
    87 		test_default_tabs ();
       
    88 		test_count ('table.dash_table', 5);
       
    89 		test_text ('table.dash_table th:eq(0)', 'Actions', 0);
       
    90 		test_text ('table.dash_table th:eq(1)', 'Recent texts (all)', 2);
       
    91 		test_text ('table.dash_table th:eq(2)', 'Recent comments', 1);
       
    92 		test_match ('table.dash_table th:eq(3)', /^Workspace activity\n\s+\(month\/week\/24 hours\)$/m);
       
    93 		test_text ('table.dash_table th:eq(4) span.em', 'Activities', 4);
   107 		test_unlogged_footer ();
    94 		test_unlogged_footer ();
   108 	});
    95 	});
   109 });
    96 });
       
    97 
       
    98 function test_default_tabs () {
       
    99 	test_count ('#main-tabs a', 5);
       
   100 	test_text ('#main-tabs li:nth-of-type(1) a[href="/"]', 'Dashboard');
       
   101 	test_match ('#main-tabs li:nth-of-type(2) a[href="/text/"]', /^Texts \(\d+\) $/);
       
   102 	test_match ('#main-tabs li:nth-of-type(3) a[href="/user/"]', /^People  \(\d+\)$/);
       
   103 	test_text ('#main-tabs li:nth-of-type(4) a[href="/settings/"]', 'Settings');
       
   104 	test_text ('#main-tabs li:nth-of-type(5) a[href="/followup/"]', 'Followup');
       
   105 }
       
   106 
       
   107 function test_logged_header (username) {
       
   108 	test_text	('#header_controls b', username)
       
   109 	test_count	('#header_controls a', 6);
       
   110 	test_text	('#header_controls a:nth-of-type(1)[href="/"]',					'Home');
       
   111 	test_text	('#header_controls a:nth-of-type(2)[href="/create/content/"]',	'Create a text');
       
   112 	test_text	('#header_controls a:nth-of-type(3)[href="/create/upload/"]',	'Upload a text');
       
   113 	test_text	('#header_controls a:nth-of-type(4)[href="/create/import/"]',	'Import a co-mented text');
       
   114 	test_text	('#header_controls a:nth-of-type(5)[href="/profile/"]',			'Profile');
       
   115 	test_text	('#header_controls a:nth-of-type(6)[href="/logout/"]',			'Logout');
       
   116 }
       
   117 
       
   118 function test_unlogged_header () {
       
   119 	test_count	('#header_controls a', 2);
       
   120 	test_text	('#header_controls a[href="/"]', 'Home');
       
   121 	test_text	('#header_controls a[href="/login/"]', 'Login');
       
   122 }
       
   123 
       
   124 function test_unlogged_footer (url) {
       
   125 	test_count	('#footer a', 9);
       
   126 	test_text	('#footer a:nth-of-type(1)[href="/contact/"]', 'Contact');
       
   127 	test_match	('#footer #comentlink[href="http://www.co-ment.com"]', /Powered by/m);
       
   128 	test_text	('#footer a:nth-of-type(3)[href="/help/"]', 'Help');
       
   129 	test_text	('#footer a:nth-of-type(4)[href="/i18n/setlang/fr/"]', 'Français');
       
   130 	test_text	('#footer a:nth-of-type(5)[href="/i18n/setlang/no/"]', 'Norsk');
       
   131 	test_text	('#footer a:nth-of-type(6)[href="/i18n/setlang/pt_BR/"]', 'Português Brasileiro');
       
   132 	test_text	('#footer a:nth-of-type(7)[href="/i18n/setlang/es/"]', 'Español');
       
   133 	test_text	('#footer a:nth-of-type(8)[href="/i18n/setlang/bg/"]', 'Български');
       
   134 	test_text	('#footer a:nth-of-type(9)[href="/i18n/setlang/it/"]', 'Italiano');
       
   135 }
       
   136 
       
   137 function test_i18n () {
       
   138 	test ('can change lang to french', dsl(function () {
       
   139 		element ('#footer a[href="/i18n/setlang/fr/"]').click ();
       
   140 //		browser.waitForPageLoad ();
       
   141 //		browser.navigateTo ('/');
       
   142 //		browser.reload ();
       
   143 		expect (elt ('#footer a[href="/help/"]').text ()).toBe ('Aide');
       
   144 		element ('#footer a[href="/i18n/setlang/en/"]').click ();
       
   145 //		browser.waitForPageLoad ();
       
   146 //		browser.navigateTo ('/');
       
   147 	}));
       
   148 }
   110 
   149 
   111 function test_page_loading (url, title) {
   150 function test_page_loading (url, title) {
   112 	test ('loads', dsl(function () {
   151 	test ('loads', dsl(function () {
   113 		// here we are in Karma page
   152 		// here we are in Karma page
   114 		browser.navigateTo (url);
   153 		browser.navigateTo (url);
   115 		expect (element ('title').text ()).toMatch (new RegExp (title,'m'));
   154 		expect (element ('title').text ()).toMatch (new RegExp (title,'m'));
   116 		element ('title').text (function (page_title) { // The same test with more vanilla javascript
   155 		// The same test agin vaniller javascript
       
   156 		element ('title').text (function (page_title) {
   117 			// here we got a value from the test iframe
   157 			// here we got a value from the test iframe
       
   158 			// to display the tested value : console.log (page_title);
   118 			if (!(new RegExp (title, 'm').test (page_title))) throw 'got page '+page_title+' instead';
   159 			if (!(new RegExp (title, 'm').test (page_title))) throw 'got page '+page_title+' instead';
   119 		});
   160 		});
   120 	}));
   161 	}));
   121 }
   162 }
   122 
   163 
   123 function test_logged_header (username) {
   164 /** Test if the selected DOM element is defined or has a given value : .val()
   124 	test ('has 6 links in #header_controls', dsl(function () {
   165  *  s : CSS selector
   125 		expect (elt ('#header_controls a').count ()).toBe (6);
   166  *  e : expected value, if omited, test existence
   126 	}));
   167  */
   127 	test ('displays current connected user name', dsl(function () {
   168 function test_val (s, e) {
   128 		expect (elt ('#header_controls b').text ()).toBe (username);
   169 	e = typeof e == 'undefined' ? '' : e; // .val() returns defined ? '' : undefined; // if no value
   129 	}));
   170 	test (s+' is '+e, dsl(function () {
   130 	test ('has links', dsl(function () {
   171 		expect (elt (s).val ()).toBe (e);
   131 		expect (elt ('#header_controls a:nth-of-type(1)[href="/"]').text ()).toBe ('Home');
   172 	}));
   132 		expect (elt ('#header_controls a:nth-of-type(2)[href="/create/content/"]').text ()).toBe ('Create a text');
   173 }
   133 		expect (elt ('#header_controls a:nth-of-type(3)[href="/create/upload/"]').text ()).toBe ('Upload a text');
   174 
   134 		expect (elt ('#header_controls a:nth-of-type(4)[href="/create/import/"]').text ()).toBe ('Import a co-mented text');
   175 /** Test if the selected DOM element has the given text : .text()
   135 		expect (elt ('#header_controls a:nth-of-type(5)[href="/profile/"]').text ()).toBe ('Profile');
   176  *  s : CSS selector
   136 		expect (elt ('#header_controls a:nth-of-type(6)[href="/logout/"]').text ()).toBe ('Logout');
   177  *  e : expected value
   137 	}));
   178  */
   138 }
   179 function test_text (s, e) {
   139 
   180 	test (s+' has text '+e, dsl(function () {
   140 function test_unlogged_header () {
   181 		expect (elt (s).text ()).toBe (e);
   141 	test ('has 2 links', dsl(function () {
   182 	}));
   142 		expect (elt ('#header_controls a').count ()).toBe (2);
   183 }
   143 		expect (elt ('#header_controls a[href="/"]').val ()).toBeDefined ();
   184 
   144 		// to display the tested value :
   185 /** Test if the selected DOM element .text() value match the given regexp
   145 		//elt ('#header_controls a[href="/"]').text ( function (txt) { console.log (txt); });
   186  *  s : CSS selector
   146 		// test returns defined ? "" : undefined;
   187  *  r : regexp to match
   147 		expect (elt ('#header_controls a[href="/"]').text ()).toBe ('Home');
   188  */
   148 		expect (elt ('#header_controls a[href="/login/"]').text ()).toBe ('Login');
   189 function test_match (s, r) {
   149 		expect (elt ('#header_controls a[href="/xxx/"]').val ()).not ().toBeDefined ();
   190 	test (s+' text match '+r, dsl(function () {
   150 	}));
   191 		expect (elt (s).text ()).toMatch (r);
   151 }
   192 	}));
   152 
   193 }
   153 function test_unlogged_footer (url) {
   194 
   154 	test ('has >= 9 links in #footer', dsl(function () {
   195 /** Count selector occurences
   155 		expect (elt ('#footer a').count ()).toBeGreaterThan (8);
   196  *  s : CSS selector
   156 		expect (elt ('#footer a[href="/contact/"]').text ()).toBe ('Contact');
   197  *  e : expected count
   157 		expect (elt ('#footer #comentlink[href="http://www.co-ment.com"]').text ()).toMatch (/Powered by/m);
   198  */
   158 		expect (elt ('#footer a[href="/help/"]').text ()).toBe ('Help');
   199 function test_count (s, e) {
   159 	}));
   200 	test (s+' count is '+e, dsl(function () {
   160 /*	test ('can change lang to french', dsl(function () {
   201 		expect (elt (s).count ()).toBe (e);
   161 		element ('#footer a[href="/i18n/setlang/fr/"]').click ();
   202 	}));
   162 //		browser.waitForPageLoad ();
   203 }
   163 //		browser.navigateTo ('/');
   204 
   164 		expect (elt ('#footer a[href="/help/"]').text ()).toBe ('Aide');
   205 /** Test Django form field presence
   165 		element ('#footer a[href="/i18n/setlang/en/"]').click ();
   206  */
   166 //		browser.waitForPageLoad ();
       
   167 //		browser.navigateTo ('/');
       
   168 	}));*/
       
   169 }
       
   170 
       
   171 /* Test Django form field presence */
       
   172 function test_form_field (id, type, label, mandatory) {
   207 function test_form_field (id, type, label, mandatory) {
   173 	test ('has a '+label+' field', dsl(function () {
   208 	test ('has a '+label+' field', dsl(function () {
   174 		var s = type == 'textarea' ? 'textarea#'+id : 'input#'+id+'[type='+type+']';
   209 		var s = type == 'textarea' ? 'textarea#'+id : 'input#'+id+'[type='+type+']';
   175 		expect (elt (s).val ()).toBeDefined ();
   210 		expect (elt (s).val ()).toBeDefined ();
   176 		expect (elt ('label[for='+id+']').text ()).toBe (label);
   211 		expect (elt ('label[for='+id+']').text ()).toBe (label);
   178 		if (mandatory)
   213 		if (mandatory)
   179 			expect (elt ('label[for='+id+'] + span.required_star').val ()).toBeDefined ();
   214 			expect (elt ('label[for='+id+'] + span.required_star').val ()).toBeDefined ();
   180 	}));
   215 	}));
   181 }
   216 }
   182 
   217 
   183 function elt (selector) {
   218 /** Ensure the given element is visible
   184 	return element (selector + ':visible');
   219  *  s : CSS selector of the DOM element to check
   185 }
   220  */
       
   221 function elt (s) {
       
   222 	return element (s + ':visible');
       
   223 }