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 (mkws.$(".mkwsPager").length == 0) return -1;
65 var found = mkws.$(".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(mkws.$(".mkwsMOTD").length).toBe(1);
102 expect(mkws.$("#testMOTD").length).toBe(1);
103 expect(mkws.$("#testMOTD").text()).toMatch("^ *$");
106 it("mkwsMOTDContainer has received the text", function () {
107 expect(mkws.$(".mkwsMOTDContainer").length).toBe(1);
108 expect(mkws.$(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
112 describe("Check pazpar2 search", function () {
113 it("pazpar2 was successfully initialized", function () {
114 expect(mkws.config.error).toBe(undefined);
117 it("validate HTML id's", function () {
118 expect(mkws.$("input.mkwsQuery").length).toBe(1);
119 expect(mkws.$("input.mkwsButton").length).toBe(1);
121 expect(mkws.$(".mkwsNext").length).not.toBe(1);
122 expect(mkws.$(".mkwsPrev").length).not.toBe(1);
125 it("run search query", function () {
126 var search_query = jasmine_config.search_query; // short hit counter with some paging
127 mkws.$("input.mkwsQuery").val(search_query);
128 debug("set search query: " + search_query)
129 expect(mkws.$("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
131 if (mkws.config.use_service_proxy) {
132 // wait for service proxy auth
133 waitsFor(function () {
134 return mkws.authenticated;
135 }, "SP auth done", 10 * jasmine_config.second);
137 debug("running raw pp2, don't wait for mkws auth");
141 debug("Click on submit button");
142 mkws.$("input.mkwsButton").trigger("click");
147 describe("Check MOTD after search", function () {
148 it("MOTD is hidden", function () {
149 if (!jasmine_config.check_motd) {
153 expect(mkws.$(".mkwsMOTD").length).toBe(1);
154 expect(mkws.$(".mkwsMOTD").is(":hidden")).toBe(true);
155 debug("motd t=" + mkws.$(".mkwsMOTD").text());
156 debug("motd v=" + mkws.$(".mkwsMOTD").is(":visible"));
162 * This part runs in background. It should be rewritten with
163 * async jasmine functions
166 describe("Check pazpar2 navigation", function () {
168 it("check running search next/prev", function () {
169 expect(mkws.$(".mkwsPager").length).toBe(1);
171 function my_click(id, time) {
172 setTimeout(function () {
173 debug("trigger click on id: " + id);
174 mkws.$(id).trigger("click");
175 }, time * jasmine_config.second);
178 waitsFor(function () {
179 return mkws.$("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
180 }, "Expect next link 2", 10 * jasmine_config.second);
183 // click next/prev after N seconds
184 my_click(".mkwsNext", 0);
187 waitsFor(function () {
188 return mkws.$("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
189 }, "Expect next link 3", 5 * jasmine_config.second);
192 // click next/prev after N seconds
193 my_click(".mkwsNext", 0);
194 my_click(".mkwsPrev", 0.2);
199 describe("Check pazpar2 hit counter", function () {
200 it("check running search hit counter", function () {
201 var max_time = jasmine_config.max_time; // in seconds
202 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
205 waitsFor(function () {
206 hits = get_hit_counter();
207 return hits > expected_hits;
208 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
211 debug("mkws pager found records: '" + hits + "'");
212 expect(mkws.$(".mkwsPager").length).toBe(1);
213 expect(hits).toBeGreaterThan(expected_hits);
218 describe("Check Termlist", function () {
219 it("found Termlist", function () {
220 var termlist = mkws.$("div.mkwsTermlists");
221 debug("Termlist success: " + termlist.length);
222 expect(termlist.length).toBe(1);
224 waitsFor(function () {
225 return mkws.$("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
226 }, "check for facet sources", 4 * jasmine_config.second);
228 // everything displayed?
230 var sources = mkws.$("div.mkwsFacet[data-mkws-facet='xtargets']");
231 debug("Termlist sources success: " + sources.length);
232 expect(sources.length).toBe(1);
234 var subjects = mkws.$("div.mkwsFacet[data-mkws-facet='subject']");
235 expect(subjects.length).toBe(1);
237 var authors = mkws.$("div.mkwsFacet[data-mkws-facet='author']");
238 expect(authors.length).toBe(1);
241 waitsFor(function () {
242 return mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length >= 2 ? true : false;
243 }, "At least one author link displayed", 4 * jasmine_config.second);
246 expect(mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length).toBeGreaterThan(1);
253 describe("Check Author Facets", function () {
254 it("limit search to first author", function () {
255 if (mkws.config.disable_facet_authors_search) {
256 debug("Facets: ignore limit search for authors");
260 var hits_all_targets = get_hit_counter();
261 var author_number = 2; // 2=first author
262 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
263 // do not click on author names without a comma, e.g.: "Joe Barbara"
264 // because searching on such authors won't find anything.
266 var terms = mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm a");
267 for (var i = 0; i < terms.length; i++) {
268 var term = mkws.$(terms[i]).text();
269 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
270 debug("ignore author facet: " + term);
276 if (mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text().length == 0) {
277 debug("No good authors found. Not clicking on the bad ones");
281 debug("Clicking on author (" + author_number + ") " + mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text());
282 mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").trigger("click");
285 waitsFor(function () {
286 var hits_single_target = get_hit_counter();
287 // debug("hits_single_target='" + hits_single_target + "' cf. hits_all_targets='" + hits_all_targets + "'");
288 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
289 }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
292 var hits_single_target = get_hit_counter();
293 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
298 describe("Check active clients author", function () {
299 it("check for active clients after limited author search", function () {
300 waitsFor(function () {
301 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
302 // debug("clients: " + clients.text());
303 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]*$");
304 }, "wait for Active clients: x/y", 5.5 * jasmine_config.second);
307 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
308 debug("span.mkwsClientCount: " + clients.text());
309 expect(clients.text()).toMatch("/[1-9]+[0-9]*$");
311 // exact match of active clients (e.g. a SP misconfiguration)
312 if (jasmine_config.active_clients) {
313 debug("check for " + jasmine_config.active_clients + " active connections");
314 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
320 describe("Check Source Facets", function () {
321 it("limit search to first source", function () {
322 var hits_all_targets = get_hit_counter();
323 var source_number = 2; // 2=first source
324 // wait for a stat response
326 // do not click on wikipedia link - no author or subject facets possible
327 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm a";
329 // wait for a visible source link in facets
330 waitsFor(function () {
331 var terms = mkws.$(link);
332 return terms && terms.length > 0;
333 }, "wait for source facets after author search", 5 * jasmine_config.second);
337 var terms = mkws.$(link);
338 for (var i = 0; i < terms.length; i++) {
339 var term = mkws.$(terms[i]).text();
340 debug("check for good source: " + term);
342 if (term.match(/wikipedia/i)) {
343 debug("ignore source facet: " + term);
349 debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
351 if (mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").text().length == 0) {
352 debug("No good source found. Not clicking on the bad ones");
356 debug("click on source link nth-child(): " + source_number);
357 mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").trigger("click");
359 mkws.$(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
361 debug("DOM wait for stat: " + waitcount);
365 waitsFor(function () {
366 if (mkws.$("div.mkwsNavi").length && mkws.$("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
371 }, "Search for source in navi bar", 4 * jasmine_config.second);
373 // Note: it may happens that limited source search returns the same number of hits
374 // as before. Thats not really an error, but unfortunate
375 waitsFor(function () {
376 var hits_single_target = get_hit_counter();
378 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
379 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
382 var hits_single_target = get_hit_counter();
383 debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
384 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
385 jasmine_status.source_click = 1;
387 mkws.$(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
393 describe("Check record list", function () {
394 it("check for single active client", function () {
395 if (!jasmine_status.source_click) {
396 debug("skip clients check due missing source click");
400 waitsFor(function () {
401 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
402 //debug("clients: " + clients.text());
403 return clients.length == 1 && clients.text().match("/1$");
404 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
407 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
408 debug("span.mkwsClientCount: " + clients.text());
409 expect(clients.text()).toMatch("/1$");
413 it("got a record", function () {
414 var linkaddr = "div.mkwsRecords div.mkwsSummary:nth-child(1) a";
416 waitsFor(function () {
417 // remove + insert node: must be at least 2
418 return mkws.$(linkaddr).length > 0;
419 }, "wait until we see a new record", 2.5 * jasmine_config.second);
422 expect(mkws.$(linkaddr).length).toBeGreaterThan(0);
427 describe("Show record", function () {
428 var record_number = 1; // the Nth record in hit list
429 it("show record author", function () {
430 var click = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") a").trigger("click");
431 debug("show record click is success: " + click.length);
432 expect(click.length).toBe(1);
434 // wait until the record pops up
435 waitsFor(function () {
436 var show = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") > div.mkwsDetails");
437 //debug("poprecord: " + (show ? show.length : -1) + " " + mkws.$("div.mkwsRecords div.mkwsSummary").text());
438 return show != null && show.length ? true : false;
439 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
442 debug("show record pop up");
443 expect(mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div")).not.toBe(null);
447 it("extract URL", function () {
448 if (jasmine_config.show_record_url == false) {
449 debug("ignore test for URL in record")
453 var urls = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div table tbody tr td a");
454 debug("number of extracted URL from record: " + urls.length);
455 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
456 for (var i = 0; i < urls.length; i++) {
457 var url = mkws.$(urls[i]);
458 debug("URL: " + url.attr('href') + " text: " + url.text());
460 expect(url.attr('href')).not.toBe(null);
461 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
462 expect(url.text()).not.toBe("");
467 describe("Check switch menu Records/Targets", function () {
468 it("check mkwsSwitch", function () {
469 expect(mkws.$("div.mkwsSwitch").length).toBe(1);
471 // expect 2 clickable links
472 expect(mkws.$("div.mkwsSwitch > a").length).toBe(2);
475 it("switch to target view", function () {
476 mkws.$("div.mkwsSwitch > a").eq(1).trigger("click");
478 // now the target table must be visible
479 expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(true);
480 expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(false);
482 // wait a half second, to show the target view
483 var time = (new Date).getTime();
484 waitsFor(function () {
485 return (new Date).getTime() - time > 700 ? true : false;
486 }, "wait some miliseconds", 1 * jasmine_config.second);
488 // look for table header
490 expect(mkws.$("div.mkwsTargets").html()).toMatch(/Target ID/);
494 it("switch back to record view", function () {
495 mkws.$("div.mkwsSwitch > a").eq(0).trigger("click");
497 // now the target table must be visible
498 expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(false);
499 expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(true);
503 describe("Check status client counter", function () {
504 function get_time() {
505 var date = new Date();
506 return date.getTime();
508 var time = get_time();
510 it("check status clients", function () {
511 if (!jasmine_status.source_click) {
512 debug("skip clients check due missing source click");
516 waitsFor(function () {
517 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
518 debug("clients: " + clients.text());
519 if (clients.length == 1 && clients.text().match("0/1$")) {
524 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
527 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
528 debug("span.mkwsClientCount: " + clients.text());
529 expect(clients.text()).toMatch("0/1$");
534 /* remove the "source" and "author" facet link to get more records again */
535 describe("Check removable facets links", function () {
538 it("remove links for source and author", function () {
540 if (!jasmine_config.check_sortby) {
541 debug("ignore check for removable facets");
547 var click = $("a.mkwsRemovable").eq(0).trigger("click");
548 debug("Removed first facets link: " + click.length);
549 expect(click.length).toBe(1);
553 $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
555 debug("DOM change for removeable: " + waitcount);
559 waitsFor(function () {
560 return $("a.mkwsRemovable").length == 1 ? 1 : 0;
564 var click = $("a.mkwsRemovable").eq(0).trigger("click");
565 debug("Removed second facets link: " + click.length);
566 expect(click.length).toBe(1);
569 waitsFor(function () {
570 // debug("wait for: " + waitcount);
571 return waitcount >= 2 ? true : false;
572 }, "Records DOM change, by per page", 2 * jasmine_config.second);
576 debug("unbind removable");
577 $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
583 describe("Check per page options", function () {
586 it("show per page", function () {
587 if (!jasmine_config.check_sortby) {
588 debug("ignore check for per page select");
592 var per_page_number = 20;
596 var select = $("select.mkwsPerpage option[selected='selected']");
597 debug("per page default is: " + select.text() + " and unselect it");
598 select.removeAttr('selected');
600 select = $("select.mkwsPerpage option[value='" + per_page_number + "']").attr('selected', true);
601 debug("per page is set to: " + select.text());
602 select.trigger("change");
604 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
606 debug("DOM wait for change, per page: " + waitcount);
610 waitsFor(function () {
611 //debug("wait for: " + waitcount);
612 return waitcount >= 30 ? true : false;
613 }, "Records DOM change, by per page", 3 * jasmine_config.second);
616 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
617 debug("unbind per page");
621 var records = $("div.mkwsRecords > div.mkwsSummary");
622 debug("Got now " + records.length + " records");
623 expect(records.length).toBe(per_page_number);
628 describe("Check SortBy options", function () {
631 it("show per page", function () {
632 if (!jasmine_config.check_sortby) {
633 debug("ignore check for sort by");
638 var sort_value = 'title:1';
639 var per_page_number = 20;
640 var title_list_old = title_list("xxx ");
642 function title_list(prefix) {
644 var terms = $("div.mkwsRecords > div.mkwsSummary > a");
645 for (var i = 0; i < terms.length; i++) {
646 var term = $(terms[i]).text();
648 // debug(prefix + "title: " + term);
654 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
656 //debug("DOM wait for change, sort by: " + waitcount);
659 var select = $("select.mkwsSort option[selected='selected']");
660 debug("Sort by default is: " + select.text() + " and unselect it");
661 select.removeAttr('selected');
663 select = $("select.mkwsSort option[value='" + sort_value + "']").attr('selected', true);
664 debug("sort by is set to: " + select.text());
665 select.trigger("change");
668 waitsFor(function () {
669 //debug("wait for2: " + waitcount);
670 return waitcount >= 6 ? true : false;
671 }, "Records DOM change, by sort page", 3 * jasmine_config.second);
674 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
675 debug("unbind per page");
679 var records = $("div.mkwsRecords > div.mkwsSummary a");
680 debug("Got now " + records.length + " records");
681 expect(records.length).toBe(per_page_number);
685 var title_list_new = title_list("yyy ");
687 for (var i = 0; i < title_list_old.length; i++) {
688 debug(title_list_old[i] + " :: " + title_list_new[i]);
690 if (title_list_old[i] != title_list_new[i]) {
694 debug("Title changes: " + diff_flag + " out of " + per_page_number);
695 expect(diff_flag).not.toBe(0);
702 describe("All tests are done", function () {
703 it(">>> hooray <<<", function () {
704 mkws.jasmine_done = true;