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
43 // use default values for undefined values
44 for (var key in jasmine_config_default) {
45 if (!jasmine_config.hasOwnProperty(key)) {
46 jasmine_config[key] = jasmine_config_default[key];
48 debug("jasmine config: " + key + " => " + jasmine_config[key]);
51 mkws.jasmine_done = false;
54 var get_hit_counter = function () {
56 if (mkws.$(".mkwsPager").length == 0) return -1;
58 var found = mkws.$(".mkwsPager").text();
59 var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
64 hits = parseInt(RegExp.$1);
66 debug("Oooops in get_hit_counter: " + RegExp.$1 + " '" + found + "'");
70 //debug("Hits: " + hits);
74 describe("Init jasmine config", function () {
75 it("jasmine was successfully initialized", function () {
76 init_jasmine_config();
78 expect(jasmine_config.search_query).toMatch(/\w/);
79 expect(jasmine_config.second).toBeGreaterThan(100);
80 expect(jasmine_config.max_time).toBeGreaterThan(1);
81 expect(jasmine_config.expected_hits).toBeGreaterThan(1);
86 xdescribe("Check MOTD before search", function () {
87 // Check that the MOTD has been moved into its container, and
88 // is visible before the search.
89 // the mkwsMOTD div was originally inside a testMOTD div, which should
91 // Note that the testMOTD is a regular div, and uses #testMOTD,
92 // since the automagic class-making does not apply to it.
93 it("MOTD is hidden", function () {
94 expect(mkws.$(".mkwsMOTD").length).toBe(1);
95 expect(mkws.$("#testMOTD").length).toBe(1);
96 expect(mkws.$("#testMOTD").text()).toMatch("^ *$");
99 it("mkwsMOTDContainer has received the text", function () {
100 expect(mkws.$(".mkwsMOTDContainer").length).toBe(1);
101 expect(mkws.$(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
105 describe("Check pazpar2 search", function () {
106 it("pazpar2 was successfully initialized", function () {
107 expect(mkws.config.error).toBe(undefined);
110 it("validate HTML id's", function () {
111 expect(mkws.$("input.mkwsQuery").length).toBe(1);
112 expect(mkws.$("input.mkwsButton").length).toBe(1);
114 expect(mkws.$(".mkwsNext").length).not.toBe(1);
115 expect(mkws.$(".mkwsPrev").length).not.toBe(1);
118 it("run search query", function () {
119 var search_query = jasmine_config.search_query; // short hit counter with some paging
120 mkws.$("input.mkwsQuery").val(search_query);
121 debug("set search query: " + search_query)
122 expect(mkws.$("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
124 if (mkws.config.use_service_proxy) {
125 // wait for service proxy auth
126 waitsFor(function () {
127 return mkws.authenticated;
128 }, "SP auth done", 10 * jasmine_config.second);
130 debug("running raw pp2, don't wait for mkws auth");
134 debug("Click on submit button");
135 mkws.$("input.mkwsButton").trigger("click");
140 describe("Check MOTD after search", function () {
141 it("MOTD is hidden", function () {
142 if (!jasmine_config.check_motd) {
146 expect(mkws.$(".mkwsMOTD").length).toBe(1);
147 expect(mkws.$(".mkwsMOTD").is(":hidden")).toBe(true);
148 debug("motd t=" + mkws.$(".mkwsMOTD").text());
149 debug("motd v=" + mkws.$(".mkwsMOTD").is(":visible"));
155 * This part runs in background. It should be rewritten with
156 * async jasmine functions
159 describe("Check pazpar2 navigation", function () {
161 it("check running search next/prev", function () {
162 expect(mkws.$(".mkwsPager").length).toBe(1);
164 function my_click(id, time) {
165 setTimeout(function () {
166 debug("trigger click on id: " + id);
167 mkws.$(id).trigger("click");
168 }, time * jasmine_config.second);
171 waitsFor(function () {
172 return mkws.$("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
173 }, "Expect next link 2", 10 * jasmine_config.second);
176 // click next/prev after N seconds
177 my_click(".mkwsNext", 0);
180 waitsFor(function () {
181 return mkws.$("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
182 }, "Expect next link 3", 5 * jasmine_config.second);
185 // click next/prev after N seconds
186 my_click(".mkwsNext", 0);
187 my_click(".mkwsPrev", 0.2);
192 describe("Check pazpar2 hit counter", function () {
193 it("check running search hit counter", function () {
194 var max_time = jasmine_config.max_time; // in seconds
195 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
198 waitsFor(function () {
199 hits = get_hit_counter();
200 return hits > expected_hits;
201 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
204 debug("mkws pager found records: '" + hits + "'");
205 expect(mkws.$(".mkwsPager").length).toBe(1);
206 expect(hits).toBeGreaterThan(expected_hits);
211 describe("Check Termlist", function () {
212 it("found Termlist", function () {
213 var termlist = mkws.$("div.mkwsTermlists");
214 debug("Termlist success: " + termlist.length);
215 expect(termlist.length).toBe(1);
217 waitsFor(function () {
218 return mkws.$("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
219 }, "check for facet sources", 4 * jasmine_config.second);
221 // everything displayed?
223 var sources = mkws.$("div.mkwsFacet[data-mkws-facet='xtargets']");
224 debug("Termlist sources success: " + sources.length);
225 expect(sources.length).toBe(1);
227 var subjects = mkws.$("div.mkwsFacet[data-mkws-facet='subject']");
228 expect(subjects.length).toBe(1);
230 var authors = mkws.$("div.mkwsFacet[data-mkws-facet='author']");
231 expect(authors.length).toBe(1);
234 waitsFor(function () {
235 return mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length >= 2 ? true : false;
236 }, "At least one author link displayed", 4 * jasmine_config.second);
239 expect(mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length).toBeGreaterThan(1);
246 describe("Check Author Facets", function () {
247 it("limit search to first author", function () {
248 if (mkws.config.disable_facet_authors_search) {
249 debug("Facets: ignore limit search for authors");
253 var hits_all_targets = get_hit_counter();
254 var author_number = 2; // 2=first author
255 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
256 // do not click on author names without a comma, e.g.: "Joe Barbara"
257 // because searching on such authors won't find anything.
259 var terms = mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm a");
260 for (var i = 0; i < terms.length; i++) {
261 var term = mkws.$(terms[i]).text();
262 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
263 debug("ignore author facet: " + term);
269 if (mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text().length == 0) {
270 debug("No good authors found. Not clicking on the bad ones");
274 debug("Clicking on author (" + author_number + ") " + mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text());
275 mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").trigger("click");
278 waitsFor(function () {
279 var hits_single_target = get_hit_counter();
280 console.log("hits_single_target='" + hits_single_target + "' cf. hits_all_targets='" + hits_all_targets + "'");
281 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
282 }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
285 var hits_single_target = get_hit_counter();
286 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
291 describe("Check active clients author", function () {
292 it("check for active clients after limited author search", function () {
293 waitsFor(function () {
294 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
295 // debug("clients: " + clients.text());
296 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]*$");
297 }, "wait for Active clients: x/y", 5.5 * jasmine_config.second);
300 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
301 debug("span.mkwsClientCount: " + clients.text());
302 expect(clients.text()).toMatch("/[1-9]+[0-9]*$");
304 // exact match of active clients (e.g. a SP misconfiguration)
305 if (jasmine_config.active_clients) {
306 debug("check for " + jasmine_config.active_clients + " active connections");
307 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
313 describe("Check Source Facets", function () {
314 it("limit search to first source", function () {
315 var hits_all_targets = get_hit_counter();
316 var source_number = 2; // 2=first source
317 // wait for a stat response
319 // do not click on wikipedia link - no author or subject facets possible
320 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm a";
322 // wait for a visible source link in facets
323 waitsFor(function () {
324 var terms = mkws.$(link);
325 return terms && terms.length > 0;
326 }, "wait for source facets after author search", 5 * jasmine_config.second);
330 var terms = mkws.$(link);
331 for (var i = 0; i < terms.length; i++) {
332 var term = mkws.$(terms[i]).text();
333 debug("check for good source: " + term);
335 if (term.match(/wikipedia/i)) {
336 debug("ignore source facet: " + term);
342 debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
344 if (mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").text().length == 0) {
345 debug("No good source found. Not clicking on the bad ones");
349 debug("click on source link nth-child(): " + source_number);
350 mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").trigger("click");
352 mkws.$(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
354 debug("DOM wait for stat: " + waitcount);
358 waitsFor(function () {
359 if (mkws.$("div.mkwsNavi").length && mkws.$("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
364 }, "Search for source in navi bar", 4 * jasmine_config.second);
366 // Note: it may happens that limited source search returns the same number of hits
367 // as before. Thats not really an error, but unfortunate
368 waitsFor(function () {
369 var hits_single_target = get_hit_counter();
371 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
372 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
375 var hits_single_target = get_hit_counter();
376 debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
377 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
378 jasmine_status.source_click = 1;
380 mkws.$(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
386 describe("Check record list", function () {
387 it("check for single active client", function () {
388 if (!jasmine_status.source_click) {
389 debug("skip clients check due missing source click");
393 waitsFor(function () {
394 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
395 //debug("clients: " + clients.text());
396 return clients.length == 1 && clients.text().match("/1$");
397 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
400 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
401 debug("span.mkwsClientCount: " + clients.text());
402 expect(clients.text()).toMatch("/1$");
406 it("got a record", function () {
407 var linkaddr = "div.mkwsRecords div.mkwsSummary:nth-child(1) a";
409 waitsFor(function () {
410 // remove + insert node: must be at least 2
411 return mkws.$(linkaddr).length > 0;
412 }, "wait until we see a new record", 2.5 * jasmine_config.second);
415 expect(mkws.$(linkaddr).length).toBeGreaterThan(0);
420 describe("Show record", function () {
421 var record_number = 1; // the Nth record in hit list
422 it("show record author", function () {
423 var click = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") a").trigger("click");
424 debug("show record click is success: " + click.length);
425 expect(click.length).toBe(1);
427 // wait until the record pops up
428 waitsFor(function () {
429 var show = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") > div.mkwsDetails");
430 //debug("poprecord: " + (show ? show.length : -1) + " " + mkws.$("div.mkwsRecords div.mkwsSummary").text());
431 return show != null && show.length ? true : false;
432 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
435 debug("show record pop up");
436 expect(mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div")).not.toBe(null);
440 it("extract URL", function () {
441 if (jasmine_config.show_record_url == false) {
442 debug("ignore test for URL in record")
446 var urls = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div table tbody tr td a");
447 debug("number of extracted URL from record: " + urls.length);
448 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
449 for (var i = 0; i < urls.length; i++) {
450 var url = mkws.$(urls[i]);
451 debug("URL: " + url.attr('href') + " text: " + url.text());
453 expect(url.attr('href')).not.toBe(null);
454 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
455 expect(url.text()).not.toBe("");
460 describe("Check switch menu Records/Targets", function () {
461 it("check mkwsSwitch", function () {
462 expect(mkws.$("div.mkwsSwitch").length).toBe(1);
464 // expect 2 clickable links
465 expect(mkws.$("div.mkwsSwitch a").length).toBe(2);
468 it("switch to target view", function () {
469 mkws.$("div.mkwsSwitch").children('a').eq(1).trigger("click");
471 // now the target table must be visible
472 expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(true);
473 expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(false);
475 // wait a half second, to show the target view
476 var time = (new Date).getTime();
477 waitsFor(function () {
478 return (new Date).getTime() - time > 700 ? true : false;
479 }, "wait some miliseconds", 1 * jasmine_config.second);
481 // look for table header
483 expect(mkws.$("div.mkwsTargets").html()).toMatch(/Target ID/);
487 it("switch back to record view", function () {
488 mkws.$("div.mkwsSwitch").children('a').eq(0).trigger("click");
490 // now the target table must be visible
491 expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(false);
492 expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(true);
496 describe("Check status client counter", function () {
497 function get_time() {
498 var date = new Date();
499 return date.getTime();
501 var time = get_time();
503 it("check status clients", function () {
504 if (!jasmine_status.source_click) {
505 debug("skip clients check due missing source click");
509 waitsFor(function () {
510 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
511 debug("clients: " + clients.text());
512 if (clients.length == 1 && clients.text().match("0/1$")) {
517 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
520 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
521 debug("span.mkwsClientCount: " + clients.text());
522 expect(clients.text()).toMatch("0/1$");
527 /* remove the "source" and "author" facet link to get more records again */
528 describe("Check removable facets links", function () {
531 it("remove links for source and author", function () {
535 var click = $("a.mkwsRemovable").trigger("click");
536 debug("Removed facets links: " + click.length);
537 expect(click.length).toBe(2);
541 $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
543 debug("DOM change for removeable: " + waitcount);
547 waitsFor(function () {
548 // debug("wait for: " + waitcount);
549 return waitcount >= 2 ? true : false;
550 }, "Records DOM change, by per page", 2 * jasmine_config.second);
554 debug("unbind removable");
555 $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
561 describe("Check per page options", function () {
564 it("show per page", function () {
566 var per_page_number = 20;
569 var select = $("select.mkwsPerpage option[selected='selected']");
570 debug("per page default is: " + select.text() + " and unselect it");
571 select.removeAttr('selected');
573 select = $("select.mkwsPerpage option[value='" + per_page_number + "']").attr('selected', true);
574 debug("per page is set to: " + select.text());
575 select.trigger("change");
577 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
579 debug("DOM wait for change, per page: " + waitcount);
583 waitsFor(function () {
584 debug("wait for: " + waitcount);
585 return waitcount >= 6 ? true : false;
586 }, "Records DOM change, by per page", 3 * jasmine_config.second);
589 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
590 debug("unbind per page");
594 var records = $("div.mkwsRecords > div.mkwsSummary");
595 debug("Got now " + records.length + " records");
596 expect(records.length).toBe(per_page_number);
602 xdescribe("Check SortBy options", function () {
605 it("sort by title", function () {
609 var terms = $("div.mkwsRecords > div > a");
610 for (var i = 0; i < terms.length; i++) {
611 var term = $(terms[i]).text();
612 debug("xxx: " + term);
617 var select = $("select.mkwsSort option[selected='selected']");
618 debug("Sort by default: " + select.text());
620 select = $("select.mkwsSort option[value='title:1']").attr('selected', 'selected');
621 debug("Sort by is set to: " + select.text());
623 $("div.mkwsRecords").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
625 debug("DOM wait for stat: " + waitcount);
629 waitsFor(function () {
630 waitcount >= 1 ? true : false;
631 }, "Records DOM change, by sort", 3 * jasmine_config.second);
634 var terms = $("div.mkwsRecords > div > a");
635 for (var i = 0; i < terms.length; i++) {
636 var term = $(terms[i]).text();
637 debug("yyy: " + term);
640 $("div.mkwsRecords").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
646 describe("All tests are done", function () {
647 it(">>> hooray <<<", function () {
648 mkws.jasmine_done = true;