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
41 // check sort by and per page menu
47 // use default values for undefined values
48 for (var key in jasmine_config_default) {
49 if (!jasmine_config.hasOwnProperty(key)) {
50 jasmine_config[key] = jasmine_config_default[key];
52 debug("jasmine config: " + key + " => " + jasmine_config[key]);
55 mkws.jasmine_done = false;
58 var get_hit_counter = function () {
60 if (mkws.$(".mkwsPager").length == 0) return -1;
62 var found = mkws.$(".mkwsPager").text();
63 var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
68 hits = parseInt(RegExp.$1);
70 debug("Oooops in get_hit_counter: " + RegExp.$1 + " '" + found + "'");
74 //debug("Hits: " + hits);
78 describe("Init jasmine config", function () {
79 it("jasmine was successfully initialized", function () {
80 init_jasmine_config();
82 expect(jasmine_config.search_query).toMatch(/\w/);
83 expect(jasmine_config.second).toBeGreaterThan(100);
84 expect(jasmine_config.max_time).toBeGreaterThan(1);
85 expect(jasmine_config.expected_hits).toBeGreaterThan(1);
90 xdescribe("Check MOTD before search", function () {
91 // Check that the MOTD has been moved into its container, and
92 // is visible before the search.
93 // the mkwsMOTD div was originally inside a testMOTD div, which should
95 // Note that the testMOTD is a regular div, and uses #testMOTD,
96 // since the automagic class-making does not apply to it.
97 it("MOTD is hidden", function () {
98 expect(mkws.$(".mkwsMOTD").length).toBe(1);
99 expect(mkws.$("#testMOTD").length).toBe(1);
100 expect(mkws.$("#testMOTD").text()).toMatch("^ *$");
103 it("mkwsMOTDContainer has received the text", function () {
104 expect(mkws.$(".mkwsMOTDContainer").length).toBe(1);
105 expect(mkws.$(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
109 describe("Check pazpar2 search", function () {
110 it("pazpar2 was successfully initialized", function () {
111 expect(mkws.config.error).toBe(undefined);
114 it("validate HTML id's", function () {
115 expect(mkws.$("input.mkwsQuery").length).toBe(1);
116 expect(mkws.$("input.mkwsButton").length).toBe(1);
118 expect(mkws.$(".mkwsNext").length).not.toBe(1);
119 expect(mkws.$(".mkwsPrev").length).not.toBe(1);
122 it("run search query", function () {
123 var search_query = jasmine_config.search_query; // short hit counter with some paging
124 mkws.$("input.mkwsQuery").val(search_query);
125 debug("set search query: " + search_query)
126 expect(mkws.$("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
128 if (mkws.config.use_service_proxy) {
129 // wait for service proxy auth
130 waitsFor(function () {
131 return mkws.authenticated;
132 }, "SP auth done", 10 * jasmine_config.second);
134 debug("running raw pp2, don't wait for mkws auth");
138 debug("Click on submit button");
139 mkws.$("input.mkwsButton").trigger("click");
144 describe("Check MOTD after search", function () {
145 it("MOTD is hidden", function () {
146 if (!jasmine_config.check_motd) {
150 expect(mkws.$(".mkwsMOTD").length).toBe(1);
151 expect(mkws.$(".mkwsMOTD").is(":hidden")).toBe(true);
152 debug("motd t=" + mkws.$(".mkwsMOTD").text());
153 debug("motd v=" + mkws.$(".mkwsMOTD").is(":visible"));
159 * This part runs in background. It should be rewritten with
160 * async jasmine functions
163 describe("Check pazpar2 navigation", function () {
165 it("check running search next/prev", function () {
166 expect(mkws.$(".mkwsPager").length).toBe(1);
168 function my_click(id, time) {
169 setTimeout(function () {
170 debug("trigger click on id: " + id);
171 mkws.$(id).trigger("click");
172 }, time * jasmine_config.second);
175 waitsFor(function () {
176 return mkws.$("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
177 }, "Expect next link 2", 10 * jasmine_config.second);
180 // click next/prev after N seconds
181 my_click(".mkwsNext", 0);
184 waitsFor(function () {
185 return mkws.$("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
186 }, "Expect next link 3", 5 * jasmine_config.second);
189 // click next/prev after N seconds
190 my_click(".mkwsNext", 0);
191 my_click(".mkwsPrev", 0.2);
196 describe("Check pazpar2 hit counter", function () {
197 it("check running search hit counter", function () {
198 var max_time = jasmine_config.max_time; // in seconds
199 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
202 waitsFor(function () {
203 hits = get_hit_counter();
204 return hits > expected_hits;
205 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
208 debug("mkws pager found records: '" + hits + "'");
209 expect(mkws.$(".mkwsPager").length).toBe(1);
210 expect(hits).toBeGreaterThan(expected_hits);
215 describe("Check Termlist", function () {
216 it("found Termlist", function () {
217 var termlist = mkws.$("div.mkwsTermlists");
218 debug("Termlist success: " + termlist.length);
219 expect(termlist.length).toBe(1);
221 waitsFor(function () {
222 return mkws.$("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
223 }, "check for facet sources", 4 * jasmine_config.second);
225 // everything displayed?
227 var sources = mkws.$("div.mkwsFacet[data-mkws-facet='xtargets']");
228 debug("Termlist sources success: " + sources.length);
229 expect(sources.length).toBe(1);
231 var subjects = mkws.$("div.mkwsFacet[data-mkws-facet='subject']");
232 expect(subjects.length).toBe(1);
234 var authors = mkws.$("div.mkwsFacet[data-mkws-facet='author']");
235 expect(authors.length).toBe(1);
238 waitsFor(function () {
239 return mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length >= 2 ? true : false;
240 }, "At least one author link displayed", 4 * jasmine_config.second);
243 expect(mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length).toBeGreaterThan(1);
250 describe("Check Author Facets", function () {
251 it("limit search to first author", function () {
252 if (mkws.config.disable_facet_authors_search) {
253 debug("Facets: ignore limit search for authors");
257 var hits_all_targets = get_hit_counter();
258 var author_number = 2; // 2=first author
259 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
260 // do not click on author names without a comma, e.g.: "Joe Barbara"
261 // because searching on such authors won't find anything.
263 var terms = mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm a");
264 for (var i = 0; i < terms.length; i++) {
265 var term = mkws.$(terms[i]).text();
266 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
267 debug("ignore author facet: " + term);
273 if (mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text().length == 0) {
274 debug("No good authors found. Not clicking on the bad ones");
278 debug("Clicking on author (" + author_number + ") " + mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text());
279 mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").trigger("click");
282 waitsFor(function () {
283 var hits_single_target = get_hit_counter();
284 console.log("hits_single_target='" + hits_single_target + "' cf. hits_all_targets='" + hits_all_targets + "'");
285 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
286 }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
289 var hits_single_target = get_hit_counter();
290 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
295 describe("Check active clients author", function () {
296 it("check for active clients after limited author search", function () {
297 waitsFor(function () {
298 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
299 // debug("clients: " + clients.text());
300 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]*$");
301 }, "wait for Active clients: x/y", 5.5 * jasmine_config.second);
304 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
305 debug("span.mkwsClientCount: " + clients.text());
306 expect(clients.text()).toMatch("/[1-9]+[0-9]*$");
308 // exact match of active clients (e.g. a SP misconfiguration)
309 if (jasmine_config.active_clients) {
310 debug("check for " + jasmine_config.active_clients + " active connections");
311 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
317 describe("Check Source Facets", function () {
318 it("limit search to first source", function () {
319 var hits_all_targets = get_hit_counter();
320 var source_number = 2; // 2=first source
321 // wait for a stat response
323 // do not click on wikipedia link - no author or subject facets possible
324 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm a";
326 // wait for a visible source link in facets
327 waitsFor(function () {
328 var terms = mkws.$(link);
329 return terms && terms.length > 0;
330 }, "wait for source facets after author search", 5 * jasmine_config.second);
334 var terms = mkws.$(link);
335 for (var i = 0; i < terms.length; i++) {
336 var term = mkws.$(terms[i]).text();
337 debug("check for good source: " + term);
339 if (term.match(/wikipedia/i)) {
340 debug("ignore source facet: " + term);
346 debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
348 if (mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").text().length == 0) {
349 debug("No good source found. Not clicking on the bad ones");
353 debug("click on source link nth-child(): " + source_number);
354 mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").trigger("click");
356 mkws.$(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
358 debug("DOM wait for stat: " + waitcount);
362 waitsFor(function () {
363 if (mkws.$("div.mkwsNavi").length && mkws.$("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
368 }, "Search for source in navi bar", 4 * jasmine_config.second);
370 // Note: it may happens that limited source search returns the same number of hits
371 // as before. Thats not really an error, but unfortunate
372 waitsFor(function () {
373 var hits_single_target = get_hit_counter();
375 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
376 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
379 var hits_single_target = get_hit_counter();
380 debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
381 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
382 jasmine_status.source_click = 1;
384 mkws.$(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
390 describe("Check record list", function () {
391 it("check for single active client", function () {
392 if (!jasmine_status.source_click) {
393 debug("skip clients check due missing source click");
397 waitsFor(function () {
398 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
399 //debug("clients: " + clients.text());
400 return clients.length == 1 && clients.text().match("/1$");
401 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
404 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
405 debug("span.mkwsClientCount: " + clients.text());
406 expect(clients.text()).toMatch("/1$");
410 it("got a record", function () {
411 var linkaddr = "div.mkwsRecords div.mkwsSummary:nth-child(1) a";
413 waitsFor(function () {
414 // remove + insert node: must be at least 2
415 return mkws.$(linkaddr).length > 0;
416 }, "wait until we see a new record", 2.5 * jasmine_config.second);
419 expect(mkws.$(linkaddr).length).toBeGreaterThan(0);
424 describe("Show record", function () {
425 var record_number = 1; // the Nth record in hit list
426 it("show record author", function () {
427 var click = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") a").trigger("click");
428 debug("show record click is success: " + click.length);
429 expect(click.length).toBe(1);
431 // wait until the record pops up
432 waitsFor(function () {
433 var show = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") > div.mkwsDetails");
434 //debug("poprecord: " + (show ? show.length : -1) + " " + mkws.$("div.mkwsRecords div.mkwsSummary").text());
435 return show != null && show.length ? true : false;
436 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
439 debug("show record pop up");
440 expect(mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div")).not.toBe(null);
444 it("extract URL", function () {
445 if (jasmine_config.show_record_url == false) {
446 debug("ignore test for URL in record")
450 var urls = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div table tbody tr td a");
451 debug("number of extracted URL from record: " + urls.length);
452 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
453 for (var i = 0; i < urls.length; i++) {
454 var url = mkws.$(urls[i]);
455 debug("URL: " + url.attr('href') + " text: " + url.text());
457 expect(url.attr('href')).not.toBe(null);
458 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
459 expect(url.text()).not.toBe("");
464 describe("Check switch menu Records/Targets", function () {
465 it("check mkwsSwitch", function () {
466 expect(mkws.$("div.mkwsSwitch").length).toBe(1);
468 // expect 2 clickable links
469 expect(mkws.$("div.mkwsSwitch > a").length).toBe(2);
472 it("switch to target view", function () {
473 mkws.$("div.mkwsSwitch > a").eq(1).trigger("click");
475 // now the target table must be visible
476 expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(true);
477 expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(false);
479 // wait a half second, to show the target view
480 var time = (new Date).getTime();
481 waitsFor(function () {
482 return (new Date).getTime() - time > 700 ? true : false;
483 }, "wait some miliseconds", 1 * jasmine_config.second);
485 // look for table header
487 expect(mkws.$("div.mkwsTargets").html()).toMatch(/Target ID/);
491 it("switch back to record view", function () {
492 mkws.$("div.mkwsSwitch > a").eq(0).trigger("click");
494 // now the target table must be visible
495 expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(false);
496 expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(true);
500 describe("Check status client counter", function () {
501 function get_time() {
502 var date = new Date();
503 return date.getTime();
505 var time = get_time();
507 it("check status clients", function () {
508 if (!jasmine_status.source_click) {
509 debug("skip clients check due missing source click");
513 waitsFor(function () {
514 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
515 debug("clients: " + clients.text());
516 if (clients.length == 1 && clients.text().match("0/1$")) {
521 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
524 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
525 debug("span.mkwsClientCount: " + clients.text());
526 expect(clients.text()).toMatch("0/1$");
531 /* remove the "source" and "author" facet link to get more records again */
532 describe("Check removable facets links", function () {
535 it("remove links for source and author", function () {
537 if (!jasmine_config.check_sortby) {
538 debug("ignore check for removable facets");
544 var click = $("a.mkwsRemovable").eq(0).trigger("click");
545 debug("Removed first facets link: " + click.length);
546 expect(click.length).toBe(1);
550 $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
552 debug("DOM change for removeable: " + waitcount);
556 waitsFor(function () {
557 return $("a.mkwsRemovable").length == 1 ? 1 : 0;
561 var click = $("a.mkwsRemovable").eq(0).trigger("click");
562 debug("Removed second facets link: " + click.length);
563 expect(click.length).toBe(1);
566 waitsFor(function () {
567 // debug("wait for: " + waitcount);
568 return waitcount >= 2 ? true : false;
569 }, "Records DOM change, by per page", 2 * jasmine_config.second);
573 debug("unbind removable");
574 $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
580 describe("Check per page options", function () {
583 it("show per page", function () {
584 if (!jasmine_config.check_sortby) {
585 debug("ignore check for per page select");
589 var per_page_number = 20;
593 var select = $("select.mkwsPerpage option[selected='selected']");
594 debug("per page default is: " + select.text() + " and unselect it");
595 select.removeAttr('selected');
597 select = $("select.mkwsPerpage option[value='" + per_page_number + "']").attr('selected', true);
598 debug("per page is set to: " + select.text());
599 select.trigger("change");
601 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
603 // debug("DOM wait for change, per page: " + waitcount);
607 waitsFor(function () {
608 //debug("wait for: " + waitcount);
609 return waitcount >= 6 ? true : false;
610 }, "Records DOM change, by per page", 3 * jasmine_config.second);
613 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
614 debug("unbind per page");
618 var records = $("div.mkwsRecords > div.mkwsSummary");
619 debug("Got now " + records.length + " records");
620 expect(records.length).toBe(per_page_number);
625 describe("Check SortBy options", function () {
628 it("show per page", function () {
629 if (!jasmine_config.check_sortby) {
630 debug("ignore check for sort by");
635 var sort_value = 'title:1';
636 var per_page_number = 20;
637 var title_list_old = title_list("xxx ");
639 function title_list(prefix) {
641 var terms = $("div.mkwsRecords > div.mkwsSummary > a");
642 for (var i = 0; i < terms.length; i++) {
643 var term = $(terms[i]).text();
645 // debug(prefix + "title: " + term);
651 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
653 //debug("DOM wait for change, sort by: " + waitcount);
656 var select = $("select.mkwsSort option[selected='selected']");
657 debug("Sort by default is: " + select.text() + " and unselect it");
658 select.removeAttr('selected');
660 select = $("select.mkwsSort option[value='" + sort_value + "']").attr('selected', true);
661 debug("sort by is set to: " + select.text());
662 select.trigger("change");
665 waitsFor(function () {
666 //debug("wait for2: " + waitcount);
667 return waitcount >= 6 ? true : false;
668 }, "Records DOM change, by sort page", 3 * jasmine_config.second);
671 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
672 debug("unbind per page");
676 var records = $("div.mkwsRecords > div.mkwsSummary a");
677 debug("Got now " + records.length + " records");
678 expect(records.length).toBe(per_page_number);
682 var title_list_new = title_list("yyy ");
684 for (var i = 0; i < title_list_old.length; i++) {
685 debug(title_list_old[i] + " :: " + title_list_new[i]);
687 if (title_list_old[i] != title_list_new[i]) {
691 debug("Title changes: " + diff_flag + " out of " + per_page_number);
692 expect(diff_flag).not.toBe(0);
699 describe("All tests are done", function () {
700 it(">>> hooray <<<", function () {
701 mkws.jasmine_done = true;