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 // use a debug function with time stamps
10 mkws.teams["AUTO"].log("Jasmine: " + text);
12 //mkws.log("Jasmine: " + text)
15 // Define empty jasmine_config for simple applications that don't define it.
16 if (jasmine_config == null || typeof jasmine_config != 'object') {
17 var jasmine_config = {};
20 var jasmine_status = {
24 /* check config for jasmine test
26 * you can override the default values in the config
27 * object: jasmine_config = {};
30 function init_jasmine_config() {
32 var jasmine_config_default = {
33 search_query: "freebsd",
37 // at least expected hit counter
39 // miliseconds to seconds
40 show_record_url: true,
41 // check for valid URL in records
44 // check sort by and per page menu
50 // use default values for undefined values
51 for (var key in jasmine_config_default) {
52 if (!jasmine_config.hasOwnProperty(key)) {
53 jasmine_config[key] = jasmine_config_default[key];
55 debug("jasmine config: " + key + " => " + jasmine_config[key]);
58 mkws.jasmine_done = false;
61 var get_hit_counter = function () {
63 if ($(".mkwsPager").length == 0) return -1;
65 var found = $(".mkwsPager").text();
66 var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
71 hits = parseInt(RegExp.$1);
73 debug("Oooops in get_hit_counter: " + RegExp.$1 + " '" + found + "'");
77 //debug("Hits: " + hits);
81 describe("Init jasmine config", function () {
82 it("jasmine was successfully initialized", function () {
83 init_jasmine_config();
85 expect(jasmine_config.search_query).toMatch(/\w/);
86 expect(jasmine_config.second).toBeGreaterThan(100);
87 expect(jasmine_config.max_time).toBeGreaterThan(1);
88 expect(jasmine_config.expected_hits).toBeGreaterThan(1);
93 xdescribe("Check MOTD before search", function () {
94 // Check that the MOTD has been moved into its container, and
95 // is visible before the search.
96 // the mkwsMOTD div was originally inside a testMOTD div, which should
98 // Note that the testMOTD is a regular div, and uses #testMOTD,
99 // since the automagic class-making does not apply to it.
100 it("MOTD is hidden", function () {
101 expect($(".mkwsMOTD").length).toBe(1);
102 expect($("#testMOTD").length).toBe(1);
103 expect($("#testMOTD").text()).toMatch("^ *$");
106 it("mkwsMOTDContainer has received the text", function () {
107 expect($(".mkwsMOTDContainer").length).toBe(1);
108 expect($(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
112 describe("Check pazpar2 search", function () {
115 it("pazpar2 was successfully initialized", function () {
116 expect(mkws.config.error).toBe(undefined);
119 it("validate HTML id's", function () {
120 expect($("input.mkwsQuery").length).toBe(1);
121 expect($("input.mkwsButton").length).toBe(1);
123 expect($(".mkwsNext").length).not.toBe(1);
124 expect($(".mkwsPrev").length).not.toBe(1);
127 it("run search query", function () {
128 var search_query = jasmine_config.search_query; // short hit counter with some paging
129 $("input.mkwsQuery").val(search_query);
130 debug("set search query: " + search_query)
131 expect($("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
133 if (mkws.config.use_service_proxy) {
134 // wait for service proxy auth
135 waitsFor(function () {
136 return mkws.authenticated;
137 }, "SP auth done", 10 * jasmine_config.second);
139 debug("running raw pp2, don't wait for mkws auth");
143 debug("Click on submit button");
144 $("input.mkwsButton").trigger("click");
149 describe("Check MOTD after search", function () {
152 it("MOTD is hidden", function () {
153 if (!jasmine_config.check_motd) {
157 expect($(".mkwsMOTD").length).toBe(1);
158 expect($(".mkwsMOTD").is(":hidden")).toBe(true);
159 debug("motd t=" + $(".mkwsMOTD").text());
160 debug("motd v=" + $(".mkwsMOTD").is(":visible"));
166 * This part runs in background. It should be rewritten with
167 * async jasmine functions
170 describe("Check pazpar2 navigation", function () {
174 it("check running search next/prev", function () {
175 expect($(".mkwsPager").length).toBe(1);
177 function my_click(id, time) {
178 setTimeout(function () {
179 debug("trigger click on id: " + id);
180 $(id).trigger("click");
181 }, time * jasmine_config.second);
184 waitsFor(function () {
185 return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
186 }, "Expect next link 2", 10 * jasmine_config.second);
189 // click next/prev after N seconds
190 my_click(".mkwsNext", 0);
193 waitsFor(function () {
194 return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
195 }, "Expect next link 3", 5 * jasmine_config.second);
198 // click next/prev after N seconds
199 my_click(".mkwsNext", 0);
200 my_click(".mkwsPrev", 0.2);
205 describe("Check pazpar2 hit counter", function () {
208 it("check running search hit counter", function () {
209 var max_time = jasmine_config.max_time; // in seconds
210 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
213 waitsFor(function () {
214 hits = get_hit_counter();
215 return hits > expected_hits;
216 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
219 debug("mkws pager found records: '" + hits + "'");
220 expect($(".mkwsPager").length).toBe(1);
221 expect(hits).toBeGreaterThan(expected_hits);
226 describe("Check Termlist", function () {
229 it("found Termlist", function () {
230 var termlist = $("div.mkwsTermlists");
231 debug("Termlist success: " + termlist.length);
232 expect(termlist.length).toBe(1);
234 waitsFor(function () {
235 return $("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
236 }, "check for facet sources", 4 * jasmine_config.second);
238 // everything displayed?
240 var sources = $("div.mkwsFacet[data-mkws-facet='xtargets']");
241 debug("Termlist sources success: " + sources.length);
242 expect(sources.length).toBe(1);
244 var subjects = $("div.mkwsFacet[data-mkws-facet='subject']");
245 expect(subjects.length).toBe(1);
247 var authors = $("div.mkwsFacet[data-mkws-facet='author']");
248 expect(authors.length).toBe(1);
251 waitsFor(function () {
252 return $("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length >= 2 ? true : false;
253 }, "At least one author link displayed", 4 * jasmine_config.second);
256 expect($("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length).toBeGreaterThan(1);
263 describe("Check Author Facets", function () {
266 it("limit search to first author", function () {
267 if (mkws.config.disable_facet_authors_search) {
268 debug("Facets: ignore limit search for authors");
272 var hits_all_targets = get_hit_counter();
273 var author_number = 2; // 2=first author
274 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
275 // do not click on author names without a comma, e.g.: "Joe Barbara"
276 // because searching on such authors won't find anything.
278 var terms = $("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm a");
279 for (var i = 0; i < terms.length; i++) {
280 var term = $(terms[i]).text();
281 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
282 debug("ignore author facet: " + term);
288 if ($("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text().length == 0) {
289 debug("No good authors found. Not clicking on the bad ones");
293 debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text());
294 $("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").trigger("click");
297 waitsFor(function () {
298 var hits_single_target = get_hit_counter();
299 // debug("hits_single_target='" + hits_single_target + "' cf. hits_all_targets='" + hits_all_targets + "'");
300 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
301 }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
304 var hits_single_target = get_hit_counter();
305 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
310 describe("Check active clients author", function () {
313 it("check for active clients after limited author search", function () {
314 waitsFor(function () {
315 var clients = $("div.mkwsStat span.mkwsClientCount");
316 // debug("clients: " + clients.text());
317 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]*$");
318 }, "wait for Active clients: x/y", 5.5 * jasmine_config.second);
321 var clients = $("div.mkwsStat span.mkwsClientCount");
322 debug("span.mkwsClientCount: " + clients.text());
323 expect(clients.text()).toMatch("/[1-9]+[0-9]*$");
325 // exact match of active clients (e.g. a SP misconfiguration)
326 if (jasmine_config.active_clients) {
327 debug("check for " + jasmine_config.active_clients + " active connections");
328 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
334 describe("Check Source Facets", function () {
337 it("limit search to first source", function () {
338 var hits_all_targets = get_hit_counter();
339 var source_number = 2; // 2=first source
340 // wait for a stat response
342 // do not click on wikipedia link - no author or subject facets possible
343 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm a";
345 // wait for a visible source link in facets
346 waitsFor(function () {
348 return terms && terms.length > 0;
349 }, "wait for source facets after author search", 5 * jasmine_config.second);
354 for (var i = 0; i < terms.length; i++) {
355 var term = $(terms[i]).text();
356 debug("check for good source: " + term);
358 if (term.match(/wikipedia/i)) {
359 debug("ignore source facet: " + term);
365 debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
367 if ($("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").text().length == 0) {
368 debug("No good source found. Not clicking on the bad ones");
372 debug("click on source link nth-child(): " + source_number);
373 $("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").trigger("click");
375 $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
377 debug("DOM change mkwsPager, for stat: " + waitcount);
381 waitsFor(function () {
382 if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
387 }, "Search for source in navi bar", 4 * jasmine_config.second);
389 // Note: it may happens that limited source search returns the same number of hits
390 // as before. Thats not really an error, but unfortunate
391 waitsFor(function () {
392 var hits_single_target = get_hit_counter();
394 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
395 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
398 var hits_single_target = get_hit_counter();
399 debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
400 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
401 jasmine_status.source_click = 1;
403 $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
409 describe("Check record list", function () {
412 it("check for single active client", function () {
413 if (!jasmine_status.source_click) {
414 debug("skip clients check due missing source click");
418 waitsFor(function () {
419 var clients = $("div.mkwsStat span.mkwsClientCount");
420 //debug("clients: " + clients.text());
421 return clients.length == 1 && clients.text().match("/1$");
422 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
425 var clients = $("div.mkwsStat span.mkwsClientCount");
426 debug("span.mkwsClientCount: " + clients.text());
427 expect(clients.text()).toMatch("/1$");
431 it("got a record", function () {
432 var linkaddr = "div.mkwsRecords div.mkwsSummary:nth-child(1) a";
434 waitsFor(function () {
435 // remove + insert node: must be at least 2
436 return $(linkaddr).length > 0;
437 }, "wait until we see a new record", 2.5 * jasmine_config.second);
440 expect($(linkaddr).length).toBeGreaterThan(0);
445 describe("Show record", function () {
448 var record_number = 1; // the Nth record in hit list
449 it("show record author", function () {
450 var click = $("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") a").trigger("click");
451 debug("show record click is success: " + click.length);
452 expect(click.length).toBe(1);
454 // wait until the record pops up
455 waitsFor(function () {
456 var show = $("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") > div.mkwsDetails");
457 //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.mkwsSummary").text());
458 return show != null && show.length ? true : false;
459 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
462 debug("show record pop up");
463 expect($("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div")).not.toBe(null);
467 it("extract URL", function () {
468 if (jasmine_config.show_record_url == false) {
469 debug("ignore test for URL in record")
473 var urls = $("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div table tbody tr td a");
474 debug("number of extracted URL from record: " + urls.length);
475 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
476 for (var i = 0; i < urls.length; i++) {
477 var url = $(urls[i]);
478 debug("URL: " + url.attr('href') + " text: " + url.text());
480 expect(url.attr('href')).not.toBe(null);
481 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
482 expect(url.text()).not.toBe("");
487 describe("Check switch menu Records/Targets", function () {
490 it("check mkwsSwitch", function () {
491 expect($("div.mkwsSwitch").length).toBe(1);
493 // expect 2 clickable links
494 expect($("div.mkwsSwitch > a").length).toBe(2);
497 it("switch to target view", function () {
498 $("div.mkwsSwitch > a").eq(1).trigger("click");
500 // now the target table must be visible
501 expect($("div.mkwsTargets").is(":visible")).toBe(true);
502 expect($("div.mkwsRecords").is(":visible")).toBe(false);
504 // wait a half second, to show the target view
505 var time = (new Date).getTime();
506 waitsFor(function () {
507 return (new Date).getTime() - time > 700 ? true : false;
508 }, "wait some miliseconds", 1 * jasmine_config.second);
510 // look for table header
512 expect($("div.mkwsTargets").html()).toMatch(/Target ID/);
516 it("switch back to record view", function () {
517 $("div.mkwsSwitch > a").eq(0).trigger("click");
519 // now the target table must be visible
520 expect($("div.mkwsTargets").is(":visible")).toBe(false);
521 expect($("div.mkwsRecords").is(":visible")).toBe(true);
525 describe("Check status client counter", function () {
528 function get_time() {
529 var date = new Date();
530 return date.getTime();
532 var time = get_time();
534 it("check status clients", function () {
535 if (!jasmine_status.source_click) {
536 debug("skip clients check due missing source click");
540 waitsFor(function () {
541 var clients = $("div.mkwsStat span.mkwsClientCount");
542 debug("clients: " + clients.text());
543 if (clients.length == 1 && clients.text().match("0/1$")) {
548 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
551 var clients = $("div.mkwsStat span.mkwsClientCount");
552 debug("span.mkwsClientCount: " + clients.text());
553 expect(clients.text()).toMatch("0/1$");
558 /* remove the "source" and "author" facet link to get more records again */
559 describe("Check removable facets links", function () {
562 it("remove links for source and author", function () {
564 if (!jasmine_config.check_sortby) {
565 debug("ignore check for removable facets");
571 var click = $("a.mkwsRemovable").eq(0).trigger("click");
572 debug("Removed first facets link: " + click.length);
573 expect(click.length).toBe(1);
577 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
579 debug("DOM change mkwsRecords for removeable: " + waitcount);
583 waitsFor(function () {
584 return waitcount >= 2 && $("a.mkwsRemovable").length == 1 ? 1 : 0;
585 }, "Records DOM change mkwsRecords, removable", 2 * jasmine_config.second);
588 debug("unbind removable");
589 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
592 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
594 debug("DOM change mkwsRecords for removeable2: " + waitcount);
597 var click = $("a.mkwsRemovable").eq(0).trigger("click");
598 debug("Removed second facets link: " + click.length);
599 expect(click.length).toBe(1);
602 waitsFor(function () {
603 return waitcount >= 2 && $("a.mkwsRemovable").length == 0 ? true : false;
604 }, "DOM change mkwsRecords, removable2", 2 * jasmine_config.second);
607 debug("unbind removable2");
608 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
614 describe("Check per page options", function () {
617 it("show per page", function () {
618 if (!jasmine_config.check_sortby) {
619 debug("ignore check for per page select");
623 var per_page_number = 20;
627 var select = $("select.mkwsPerpage option[selected='selected']");
628 debug("per page default is: " + select.text() + " and unselect it");
629 select.removeAttr('selected');
631 select = $("select.mkwsPerpage option[value='" + per_page_number + "']").attr('selected', true);
632 debug("per page is set to: " + select.text());
633 select.trigger("change");
635 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
637 debug("DOM change mkwsRecords, per page: " + waitcount);
641 waitsFor(function () {
642 // debug("per page waitcounter: " + waitcount)
643 return waitcount >= (per_page_number + 10) ? true : false;
644 }, "DOM change mkwsRecords, by per page", 3 * jasmine_config.second);
647 debug("unbind per page");
648 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
650 var records = $("div.mkwsRecords > div.mkwsSummary");
651 debug("Per page got now " + records.length + " records");
652 expect(records.length).toBe(per_page_number);
657 describe("Check SortBy options", function () {
660 it("show per page", function () {
661 if (!jasmine_config.check_sortby) {
662 debug("ignore check for sort by");
667 var sort_value = 'title:1';
668 var per_page_number = 20;
669 var title_list_old = title_list("xxx ");
671 function title_list(prefix) {
673 var terms = $("div.mkwsRecords > div.mkwsSummary > a");
674 for (var i = 0; i < terms.length; i++) {
675 var term = $(terms[i]).text();
677 // debug(prefix + "title: " + term);
683 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
685 debug("DOM change mkwsRecords, sort by: " + waitcount);
688 var select = $("select.mkwsSort option[selected='selected']");
689 debug("Sort by default is: " + select.text() + " and unselect it");
690 select.removeAttr('selected');
692 select = $("select.mkwsSort option[value='" + sort_value + "']").attr('selected', true);
693 debug("sort by is set to: " + select.text());
694 select.trigger("change");
697 waitsFor(function () {
698 //debug("wait for2: " + waitcount);
699 return waitcount >= (per_page_number + 10)? true : false;
700 }, "DOM change mkwsRecords, by sort page", 3 * jasmine_config.second);
703 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
704 debug("unbind by sort");
706 var records = $("div.mkwsRecords > div.mkwsSummary a");
707 debug("Sort by got now " + records.length + " records");
708 expect(records.length).toBe(per_page_number);
712 var title_list_new = title_list("yyy ");
714 for (var i = 0; i < title_list_old.length; i++) {
715 debug(title_list_old[i] + " :: " + title_list_new[i]);
717 if (title_list_old[i] != title_list_new[i]) {
721 debug("Title changes: " + diff_flag + " out of " + per_page_number);
722 expect(diff_flag).not.toBe(0);
729 describe("All tests are done", function () {
730 it(">>> hooray <<<", function () {
731 mkws.jasmine_done = true;
732 debug(">>> hooray <<<");