1 /* Copyright (c) 2013-2014 IndexData ApS. http://indexdata.com
3 * perform papzpar2 / pz2.js search & retrieve request in the browser
7 // get references from mkws.js, lazy evaluation
8 var debug = function (text) {
9 mkws.log("Jasmine: " + text)
12 // Define empty jasmine_config for simple applications that don't define it.
13 if (jasmine_config == null || typeof jasmine_config != 'object') {
14 var jasmine_config = {};
17 var jasmine_status = {
21 /* check config for jasmine test
23 * you can override the default values in the config
24 * object: jasmine_config = {};
27 function init_jasmine_config() {
29 var jasmine_config_default = {
30 search_query: "freebsd",
34 // at least expected hit counter
36 // miliseconds to seconds
37 show_record_url: true,
38 // check for valid URL in records
42 // use default values for undefined values
43 for (var key in jasmine_config_default) {
44 if (!jasmine_config.hasOwnProperty(key)) {
45 jasmine_config[key] = jasmine_config_default[key];
47 debug("jasmine config: " + key + " => " + jasmine_config[key]);
50 mkws.jasmine_done = false;
53 var get_hit_counter = function () {
55 if ($(".mkwsPager").length == 0) return -1;
57 var found = $(".mkwsPager").text();
58 var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
63 hits = parseInt(RegExp.$1);
64 expect(hits).toBeGreaterThan(0);
67 //debug("Hits: " + hits);
71 describe("Init jasmine config", function () {
72 it("jasmine was successfully initialized", function () {
73 init_jasmine_config();
75 expect(jasmine_config.search_query).toMatch(/\w/);
76 expect(jasmine_config.second).toBeGreaterThan(100);
77 expect(jasmine_config.max_time).toBeGreaterThan(1);
78 expect(jasmine_config.expected_hits).toBeGreaterThan(1);
83 xdescribe("Check MOTD before search", function () {
84 // Check that the MOTD has been moved into its container, and
85 // is visible before the search.
86 // the mkwsMOTD div was originally inside a testMOTD div, which should
88 // Note that the testMOTD is a regular div, and uses #testMOTD,
89 // since the automagic class-making does not apply to it.
90 it("MOTD is hidden", function () {
91 expect($(".mkwsMOTD").length).toBe(1);
92 expect($("#testMOTD").length).toBe(1);
93 expect($("#testMOTD").text()).toMatch("^ *$");
96 it("mkwsMOTDContainer has received the text", function () {
97 expect($(".mkwsMOTDContainer").length).toBe(1);
98 expect($(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
102 describe("Check pazpar2 search", function () {
103 it("pazpar2 was successfully initialized", function () {
104 expect(mkws.config.error).toBe(undefined);
107 it("validate HTML id's", function () {
108 expect($("input.mkwsQuery").length).toBe(1);
109 expect($("input.mkwsButton").length).toBe(1);
111 expect($(".mkwsNext").length).not.toBe(1);
112 expect($(".mkwsPrev").length).not.toBe(1);
115 it("run search query", function () {
116 var search_query = jasmine_config.search_query; // short hit counter with some paging
117 $("input.mkwsQuery").val(search_query);
118 debug("set search query: " + search_query)
119 expect($("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
121 if (mkws.config.use_service_proxy) {
122 // wait for service proxy auth
123 waitsFor(function () {
124 return mkws.authenticated;
125 }, "SP auth done", 10 * jasmine_config.second);
127 debug("running raw pp2, don't wait for mkws auth");
131 debug("Click on submit button");
132 $("input.mkwsButton").trigger("click");
137 describe("Check MOTD after search", function () {
138 it("MOTD is hidden", function () {
139 expect($(".mkwsMOTD").length).toBe(1);
140 expect($(".mkwsMOTD").is(":hidden")).toBe(true);
141 debug("motd t=" + $(".mkwsMOTD").text());
142 debug("motd v=" + $(".mkwsMOTD").is(":visible"));
148 * This part runs in background. It should be rewritten with
149 * async jasmine functions
152 describe("Check pazpar2 navigation", function () {
154 it("check running search next/prev", function () {
155 expect($(".mkwsPager").length).toBe(1);
157 function my_click(id, time) {
158 setTimeout(function () {
159 debug("trigger click on id: " + id);
160 $(id).trigger("click");
161 }, time * jasmine_config.second);
164 waitsFor(function () {
165 return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
166 }, "Expect next link 2", 10 * jasmine_config.second);
169 // click next/prev after N seconds
170 my_click(".mkwsNext", 0);
173 waitsFor(function () {
174 return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
175 }, "Expect next link 3", 5 * jasmine_config.second);
178 // click next/prev after N seconds
179 my_click(".mkwsNext", 0);
180 my_click(".mkwsPrev", 0.2);
185 describe("Check pazpar2 hit counter", function () {
186 it("check running search hit counter", function () {
187 var max_time = jasmine_config.max_time; // in seconds
188 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
191 waitsFor(function () {
192 hits = get_hit_counter();
193 return hits > expected_hits;
194 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
197 debug("mkws pager found records: '" + hits + "'");
198 expect($(".mkwsPager").length).toBe(1);
199 expect(hits).toBeGreaterThan(expected_hits);
204 describe("Check Termlist", function () {
205 it("found Termlist", function () {
206 var termlist = $("div.mkwsTermlists");
207 debug("Termlist success: " + termlist.length);
208 expect(termlist.length).toBe(1);
210 waitsFor(function () {
211 return $("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
212 }, "check for facet sources", 4 * jasmine_config.second);
214 // everything displayed?
216 var sources = $("div.mkwsFacet[data-mkws-facet='xtargets']");
217 debug("Termlist sources success: " + sources.length);
218 expect(sources.length).toBe(1);
220 var subjects = $("div.mkwsFacet[data-mkws-facet='subject']");
221 expect(subjects.length).toBe(1);
223 var authors = $("div.mkwsFacet[data-mkws-facet='author']");
224 expect(authors.length).toBe(1);
227 waitsFor(function () {
228 return $("div.mkwsFacet[data-mkws-facet='author'] div.term").length >= 2 ? true : false;
229 }, "At least one author link displayed", 4 * jasmine_config.second);
232 expect($("div.mkwsFacet[data-mkws-facet='author'] div.term").length).toBeGreaterThan(1);
237 describe("Check Author Facets", function () {
238 it("limit search to first author", function () {
239 if (mkws.config.disable_facet_authors_search) {
240 debug("Facets: ignore limit search for authors");
244 var hits_all_targets = get_hit_counter();
245 var author_number = 2; // 2=first author
246 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
247 // do not click on author names without a comma, e.g.: "Joe Barbara"
248 // because searching on such authors won't find anything.
250 var terms = $("div.mkwsFacet[data-mkws-facet='author'] div.term a");
251 for (var i = 0; i < terms.length; i++) {
252 var term = $(terms[i]).text();
253 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
254 debug("ignore author facet: " + term);
260 if ($("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text().length == 0) {
261 debug("No good authors found. Not clicking on the bad ones");
265 debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text());
266 $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").trigger("click");
269 waitsFor(function () {
270 var hits_single_target = get_hit_counter();
271 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
272 }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
275 var hits_single_target = get_hit_counter();
276 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
277 expect(hits_all_targets).toBeGreaterThan(hits_single_target);
281 it("check for active clients after limited author search", function () {
282 waitsFor(function () {
283 var clients = $("div#mkwsStat span.clients");
284 //debug("clients: " + clients.text());
285 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]+$");
286 }, "wait for Active clients: x/y", 5 * jasmine_config.second);
289 var clients = $("div#mkwsStat span.clients");
290 debug("span.clients: " + clients.text());
291 expect(clients.text()).toMatch("/[1-9]+[0-9]+$");
293 // exact match of active clients (e.g. a SP misconfiguration)
294 if (jasmine_config.active_clients) {
295 debug("check for " + jasmine_config.active_clients + " active connections");
296 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
302 describe("Check Source Facets", function () {
303 it("limit search to first source", function () {
304 var hits_all_targets = get_hit_counter();
305 var source_number = 2; // 2=first source
306 // wait for a stat response
308 // do not click on wikipedia link - no author or subject facets possible
309 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.term a";
311 // wait for a visible source link in facets
312 waitsFor(function () {
314 return terms && terms.length > 0;
315 }, "wait for source facets after author search", 5 * jasmine_config.second);
320 for (var i = 0; i < terms.length; i++) {
321 var term = $(terms[i]).text();
322 debug("check for good source: " + term);
324 if (term.match(/wikipedia/i)) {
325 debug("ignore source facet: " + term);
331 debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
333 if ($("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").text().length == 0) {
334 debug("No good source found. Not clicking on the bad ones");
338 debug("click on source link nth-child(): " + source_number);
339 $("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").trigger("click");
341 $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
343 debug("DOM wait for stat: " + waitcount);
347 waitsFor(function () {
348 if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
353 }, "Search for source in navi bar", 4 * jasmine_config.second);
355 // Note: it may happens that limited source search returns the same number of hits
356 // as before. Thats not really an error, but unfortunate
357 waitsFor(function () {
358 var hits_single_target = get_hit_counter();
360 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
361 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
364 var hits_single_target = get_hit_counter();
365 debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
366 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
367 jasmine_status.source_click = 1;
369 $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
375 describe("Check record list", function () {
376 it("check for single active client", function () {
377 if (!jasmine_status.source_click) {
378 debug("skip clients check due missing source click");
382 waitsFor(function () {
383 var clients = $("div#mkwsStat span.clients");
384 //debug("clients: " + clients.text());
385 return clients.length == 1 && clients.text().match("/1$");
386 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
389 var clients = $("div#mkwsStat span.clients");
390 debug("span.clients: " + clients.text());
391 expect(clients.text()).toMatch("/1$");
395 it("got a record", function () {
396 var linkaddr = "div.mkwsRecords div.record:nth-child(1) a";
398 waitsFor(function () {
399 // remove + insert node: must be at least 2
400 return $(linkaddr).length > 0;
401 }, "wait until we see a new record", 2.5 * jasmine_config.second);
404 expect($(linkaddr).length).toBeGreaterThan(0);
409 describe("Show record", function () {
410 var record_number = 1; // the Nth record in hit list
411 it("show record author", function () {
412 var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
413 debug("show record click is success: " + click.length);
414 expect(click.length).toBe(1);
416 // wait until the record pops up
417 waitsFor(function () {
418 var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") > div.details");
419 //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.record").text());
420 return show != null && show.length ? true : false;
421 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
424 debug("show record pop up");
425 expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
429 it("extract URL", function () {
430 if (jasmine_config.show_record_url == false) {
431 debug("ignore test for URL in record")
435 var urls = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
436 debug("number of extracted URL from record: " + urls.length);
437 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
438 for (var i = 0; i < urls.length; i++) {
439 var url = $(urls[i]);
440 debug("URL: " + url.attr('href') + " text: " + url.text());
442 expect(url.attr('href')).not.toBe(null);
443 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
444 expect(url.text()).not.toBe("");
449 describe("Check switch menu Records/Targets", function () {
450 it("check mkwsSwitch", function () {
451 expect($("div.mkwsSwitch").length).toBe(1);
453 // expect 2 clickable links
454 expect($("div.mkwsSwitch a").length).toBe(2);
457 it("switch to target view", function () {
458 $("div.mkwsSwitch").children('a').eq(1).trigger("click");
460 // now the target table must be visible
461 expect($("div.mkwsBytarget").is(":visible")).toBe(true);
462 expect($("div.mkwsRecords").is(":visible")).toBe(false);
464 // wait a half second, to show the target view
465 var time = (new Date).getTime();
466 waitsFor(function () {
467 return (new Date).getTime() - time > 700 ? true : false;
468 }, "wait some miliseconds", 1 * jasmine_config.second);
470 // look for table header
472 expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
476 it("switch back to record view", function () {
477 $("div.mkwsSwitch").children('a').eq(0).trigger("click");
479 // now the target table must be visible
480 expect($("div.mkwsBytarget").is(":visible")).toBe(false);
481 expect($("div.mkwsRecords").is(":visible")).toBe(true);
485 describe("Check status client counter", function () {
486 function get_time() {
487 var date = new Date();
488 return date.getTime();
490 var time = get_time();
492 it("check status clients", function () {
493 if (!jasmine_status.source_click) {
494 debug("skip clients check due missing source click");
498 waitsFor(function () {
499 var clients = $("div#mkwsStat span.clients");
500 debug("clients: " + clients.text());
501 if (clients.length == 1 && clients.text().match("0/1$")) {
506 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
509 var clients = $("div#mkwsStat span.clients");
510 debug("span.clients: " + clients.text());
511 expect(clients.text()).toMatch("0/1$");
517 describe("All tests are done", function () {
518 it(">>> hooray <<<", function () {
519 mkws.jasmine_done = true;