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);
82 describe("Check MOTD before search", function () {
83 // Check that the MOTD has been moved into its container, and
84 // is visible before the search.
85 // the mkwsMOTD div was originally inside a testMOTD div, which should
87 // Note that the testMOTD is a regular div, and uses #testMOTD,
88 // since the automagic class-making does not apply to it.
89 it("MOTD is hidden", function () {
90 expect($(".mkwsMOTD").length).toBe(1);
91 expect($("#testMOTD").length).toBe(1);
92 expect($("#testMOTD").text()).toMatch("^ *$");
95 it("mkwsMOTDContainer has received the text", function () {
96 expect($(".mkwsMOTDContainer").length).toBe(1);
97 expect($(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
101 describe("Check pazpar2 search", function () {
102 it("pazpar2 was successfully initialized", function () {
103 expect(mkws.config.error).toBe(undefined);
106 it("validate HTML id's", function () {
107 expect($("input.mkwsQuery").length).toBe(1);
108 expect($("input.mkwsButton").length).toBe(1);
110 expect($(".mkwsNext").length).not.toBe(1);
111 expect($(".mkwsPrev").length).not.toBe(1);
114 it("run search query", function () {
115 var search_query = jasmine_config.search_query; // short hit counter with some paging
116 $("input.mkwsQuery").val(search_query);
117 debug("set search query: " + search_query)
118 expect($("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
120 if (mkws.config.use_service_proxy) {
121 // wait for service proxy auth
122 waitsFor(function () {
123 return mkws.authenticated;
124 }, "SP auth done", 10 * jasmine_config.second);
126 debug("running raw pp2, don't wait for mkws auth");
130 debug("Click on submit button");
131 $("input.mkwsButton").trigger("click");
136 describe("Check MOTD after search", function () {
137 it("MOTD is hidden", function () {
138 expect($(".mkwsMOTD").length).toBe(1);
139 expect($(".mkwsMOTD").is(":hidden")).toBe(true);
140 debug("motd t=" + $(".mkwsMOTD").text());
141 debug("motd v=" + $(".mkwsMOTD").is(":visible"));
147 * This part runs in background. It should be rewritten with
148 * async jasmine functions
151 describe("Check pazpar2 navigation", function () {
153 it("check running search next/prev", function () {
154 expect($(".mkwsPager").length).toBe(1);
156 function my_click(id, time) {
157 setTimeout(function () {
158 debug("trigger click on id: " + id);
159 $(id).trigger("click");
160 }, time * jasmine_config.second);
163 waitsFor(function () {
164 return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
165 }, "Expect next link 2", 10 * jasmine_config.second);
168 // click next/prev after N seconds
169 my_click(".mkwsNext", 0);
172 waitsFor(function () {
173 return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
174 }, "Expect next link 3", 5 * jasmine_config.second);
177 // click next/prev after N seconds
178 my_click(".mkwsNext", 0);
179 my_click(".mkwsPrev", 0.2);
184 describe("Check pazpar2 hit counter", function () {
185 it("check running search hit counter", function () {
186 var max_time = jasmine_config.max_time; // in seconds
187 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
190 waitsFor(function () {
191 hits = get_hit_counter();
192 return hits > expected_hits;
193 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
196 debug("mkws pager found records: '" + hits + "'");
197 expect($(".mkwsPager").length).toBe(1);
198 expect(hits).toBeGreaterThan(expected_hits);
203 describe("Check Termlist", function () {
204 it("found Termlist", function () {
205 var termlist = $("div.mkwsTermlists");
206 debug("Termlist success: " + termlist.length);
207 expect(termlist.length).toBe(1);
209 waitsFor(function () {
210 return $("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
211 }, "check for facet sources", 4 * jasmine_config.second);
213 // everything displayed?
215 var sources = $("div.mkwsFacet[data-mkws-facet='xtargets']");
216 debug("Termlist sources success: " + sources.length);
217 expect(sources.length).toBe(1);
219 var subjects = $("div.mkwsFacet[data-mkws-facet='subject']");
220 expect(subjects.length).toBe(1);
222 var authors = $("div.mkwsFacet[data-mkws-facet='author']");
223 expect(authors.length).toBe(1);
226 waitsFor(function () {
227 return $("div.mkwsFacet[data-mkws-facet='author'] div.term").length >= 2 ? true : false;
228 }, "At least one author link displayed", 4 * jasmine_config.second);
231 expect($("div.mkwsFacet[data-mkws-facet='author'] div.term").length).toBeGreaterThan(1);
235 it("limit search to first author", function () {
236 if (mkws.config.disable_facet_authors_search) {
237 debug("Facets: ignore limit search for authors");
241 var hits_all_targets = get_hit_counter();
242 var author_number = 2; // 2=first author
243 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
244 // do not click on author names without a comma, e.g.: "Joe Barbara"
245 // because searching on such authors won't find anything.
246 var terms = $("div.mkwsFacet[data-mkws-facet='author'] div.term a");
247 for (var i = 0; i < terms.length; i++) {
248 var term = $(terms[i]).text();
249 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
250 debug("ignore author facet: " + term);
256 if ($("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text().length == 0) {
257 debug("No good authors found. Not clicking on the bad ones");
261 debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text());
262 $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").trigger("click");
264 waitsFor(function () {
265 var hits_single_target = get_hit_counter();
266 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
267 }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
270 var hits_single_target = get_hit_counter();
271 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
272 expect(hits_all_targets).toBeGreaterThan(hits_single_target);
276 it("check for active clients", function () {
277 waitsFor(function () {
278 var clients = $("div#mkwsStat span.clients");
279 //debug("clients: " + clients.text());
280 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]+$");
281 }, "wait for Active clients: x/y", 5 * jasmine_config.second);
284 var clients = $("div#mkwsStat span.clients");
285 debug("span.clients: " + clients.text());
286 expect(clients.text()).toMatch("/[1-9]+[0-9]+$");
288 // exact match of active clients (e.g. a SP misconfiguration)
289 if (jasmine_config.active_clients) {
290 debug("check for " + jasmine_config.active_clients + " active connections");
291 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
296 it("limit search to first source", function () {
297 var hits_all_targets = get_hit_counter();
298 var source_number = 2; // 2=first source
299 // do not click on wikipedia link - no author or subject facets possible
300 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.term a";
302 waitsFor(function () {
304 return terms && terms.length > 0;
305 }, "wait for source facets after author search", 5 * jasmine_config.second);
310 for (var i = 0; i < terms.length; i++) {
311 var term = $(terms[i]).text();
312 if (term.match(/wikipedia/i)) {
313 debug("ignore source facet: " + term);
319 debug("Source count: " + terms.length + ", click on: " + source_number);
322 if ($("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").text().length == 0) {
323 debug("No good source found. Not clicking on the bad ones");
327 $("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").trigger("click");
329 // wait for a stat response
331 $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
333 debug("DOM wait for stat: " + waitcount);
336 waitsFor(function () {
337 if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
342 }, "Search for source in navi bar", 4 * jasmine_config.second);
344 // Note: it may happens that limited source search returns the same number of hits
345 // as before. Thats not really an error, but unfortunate
346 waitsFor(function () {
347 var hits_single_target = get_hit_counter();
349 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
350 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
353 var hits_single_target = get_hit_counter();
354 debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
355 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
356 jasmine_status.source_click = 1;
358 $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
364 describe("Check record list", function () {
365 it("check for single active client", function () {
366 if (!jasmine_status.source_click) {
367 debug("skip clients check due missing source click");
371 waitsFor(function () {
372 var clients = $("div#mkwsStat span.clients");
373 //debug("clients: " + clients.text());
374 return clients.length == 1 && clients.text().match("/1$");
375 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
378 var clients = $("div#mkwsStat span.clients");
379 debug("span.clients: " + clients.text());
380 expect(clients.text()).toMatch("/1$");
384 it("got a record", function () {
385 var linkaddr = "div.mkwsRecords div.record:nth-child(1) a";
387 waitsFor(function () {
388 // remove + insert node: must be at least 2
389 return $(linkaddr).length > 0;
390 }, "wait until we see a new record", 2.5 * jasmine_config.second);
393 expect($(linkaddr).length).toBeGreaterThan(0);
398 describe("Show record", function () {
399 var record_number = 1; // the Nth record in hit list
400 it("show record author", function () {
401 var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
402 debug("show record click is success: " + click.length);
403 expect(click.length).toBe(1);
405 // wait until the record pops up
406 waitsFor(function () {
407 var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") > div.details");
408 //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.record").text());
409 return show != null && show.length ? true : false;
410 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
413 debug("show record pop up");
414 expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
418 it("extract URL", function () {
419 if (jasmine_config.show_record_url == false) {
420 debug("ignore test for URL in record")
424 var urls = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
425 debug("number of extracted URL from record: " + urls.length);
426 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
427 for (var i = 0; i < urls.length; i++) {
428 var url = $(urls[i]);
429 debug("URL: " + url.attr('href') + " text: " + url.text());
431 expect(url.attr('href')).not.toBe(null);
432 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
433 expect(url.text()).not.toBe("");
438 describe("Check switch menu Records/Targets", function () {
439 it("check mkwsSwitch", function () {
440 expect($("div.mkwsSwitch").length).toBe(1);
442 // expect 2 clickable links
443 expect($("div.mkwsSwitch a").length).toBe(2);
446 it("switch to target view", function () {
447 $("div.mkwsSwitch").children('a').eq(1).trigger("click");
449 // now the target table must be visible
450 expect($("div.mkwsBytarget").is(":visible")).toBe(true);
451 expect($("div.mkwsRecords").is(":visible")).toBe(false);
453 // wait a half second, to show the target view
454 var time = (new Date).getTime();
455 waitsFor(function () {
456 return (new Date).getTime() - time > 700 ? true : false;
457 }, "wait some miliseconds", 1 * jasmine_config.second);
459 // look for table header
461 expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
465 it("switch back to record view", function () {
466 $("div.mkwsSwitch").children('a').eq(0).trigger("click");
468 // now the target table must be visible
469 expect($("div.mkwsBytarget").is(":visible")).toBe(false);
470 expect($("div.mkwsRecords").is(":visible")).toBe(true);
474 describe("Check status client counter", function () {
475 function get_time() {
476 var date = new Date();
477 return date.getTime();
479 var time = get_time();
481 it("check status clients", function () {
482 if (!jasmine_status.source_click) {
483 debug("skip clients check due missing source click");
487 waitsFor(function () {
488 var clients = $("div#mkwsStat span.clients");
489 debug("clients: " + clients.text());
490 if (clients.length == 1 && clients.text().match("0/1$")) {
495 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
498 var clients = $("div#mkwsStat span.clients");
499 debug("span.clients: " + clients.text());
500 expect(clients.text()).toMatch("0/1$");
506 describe("All tests are done", function () {
507 it(">>> hooray <<<", function () {
508 mkws.jasmine_done = true;