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 ($(".mkwsPager").length == 0) return -1;
58 var found = $(".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($(".mkwsMOTD").length).toBe(1);
95 expect($("#testMOTD").length).toBe(1);
96 expect($("#testMOTD").text()).toMatch("^ *$");
99 it("mkwsMOTDContainer has received the text", function () {
100 expect($(".mkwsMOTDContainer").length).toBe(1);
101 expect($(".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($("input.mkwsQuery").length).toBe(1);
112 expect($("input.mkwsButton").length).toBe(1);
114 expect($(".mkwsNext").length).not.toBe(1);
115 expect($(".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 $("input.mkwsQuery").val(search_query);
121 debug("set search query: " + search_query)
122 expect($("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 $("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($(".mkwsMOTD").length).toBe(1);
147 expect($(".mkwsMOTD").is(":hidden")).toBe(true);
148 debug("motd t=" + $(".mkwsMOTD").text());
149 debug("motd v=" + $(".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($(".mkwsPager").length).toBe(1);
164 function my_click(id, time) {
165 setTimeout(function () {
166 debug("trigger click on id: " + id);
167 $(id).trigger("click");
168 }, time * jasmine_config.second);
171 waitsFor(function () {
172 return $("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 $("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($(".mkwsPager").length).toBe(1);
206 expect(hits).toBeGreaterThan(expected_hits);
211 describe("Check Termlist", function () {
212 it("found Termlist", function () {
213 var termlist = $("div.mkwsTermlists");
214 debug("Termlist success: " + termlist.length);
215 expect(termlist.length).toBe(1);
217 waitsFor(function () {
218 return $("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 = $("div.mkwsFacet[data-mkws-facet='xtargets']");
224 debug("Termlist sources success: " + sources.length);
225 expect(sources.length).toBe(1);
227 var subjects = $("div.mkwsFacet[data-mkws-facet='subject']");
228 expect(subjects.length).toBe(1);
230 var authors = $("div.mkwsFacet[data-mkws-facet='author']");
231 expect(authors.length).toBe(1);
234 waitsFor(function () {
235 return $("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($("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm").length).toBeGreaterThan(1);
244 describe("Check Author Facets", function () {
245 it("limit search to first author", function () {
246 if (mkws.config.disable_facet_authors_search) {
247 debug("Facets: ignore limit search for authors");
251 var hits_all_targets = get_hit_counter();
252 var author_number = 2; // 2=first author
253 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
254 // do not click on author names without a comma, e.g.: "Joe Barbara"
255 // because searching on such authors won't find anything.
257 var terms = $("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm a");
258 for (var i = 0; i < terms.length; i++) {
259 var term = $(terms[i]).text();
260 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
261 debug("ignore author facet: " + term);
267 if ($("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text().length == 0) {
268 debug("No good authors found. Not clicking on the bad ones");
272 debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text());
273 $("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").trigger("click");
276 waitsFor(function () {
277 var hits_single_target = get_hit_counter();
278 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
279 }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
282 var hits_single_target = get_hit_counter();
283 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
288 describe("Check active clients author", function () {
289 it("check for active clients after limited author search", function () {
290 waitsFor(function () {
291 var clients = $("div.mkwsStat span.mkwsClientCount");
292 // debug("clients: " + clients.text());
293 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]*$");
294 }, "wait for Active clients: x/y", 5.5 * jasmine_config.second);
297 var clients = $("div.mkwsStat span.mkwsClientCount");
298 debug("span.mkwsClientCount: " + clients.text());
299 expect(clients.text()).toMatch("/[1-9]+[0-9]*$");
301 // exact match of active clients (e.g. a SP misconfiguration)
302 if (jasmine_config.active_clients) {
303 debug("check for " + jasmine_config.active_clients + " active connections");
304 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
310 describe("Check Source Facets", function () {
311 it("limit search to first source", function () {
312 var hits_all_targets = get_hit_counter();
313 var source_number = 2; // 2=first source
314 // wait for a stat response
316 // do not click on wikipedia link - no author or subject facets possible
317 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm a";
319 // wait for a visible source link in facets
320 waitsFor(function () {
322 return terms && terms.length > 0;
323 }, "wait for source facets after author search", 5 * jasmine_config.second);
328 for (var i = 0; i < terms.length; i++) {
329 var term = $(terms[i]).text();
330 debug("check for good source: " + term);
332 if (term.match(/wikipedia/i)) {
333 debug("ignore source facet: " + term);
339 debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
341 if ($("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").text().length == 0) {
342 debug("No good source found. Not clicking on the bad ones");
346 debug("click on source link nth-child(): " + source_number);
347 $("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").trigger("click");
349 $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
351 debug("DOM wait for stat: " + waitcount);
355 waitsFor(function () {
356 if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
361 }, "Search for source in navi bar", 4 * jasmine_config.second);
363 // Note: it may happens that limited source search returns the same number of hits
364 // as before. Thats not really an error, but unfortunate
365 waitsFor(function () {
366 var hits_single_target = get_hit_counter();
368 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
369 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
372 var hits_single_target = get_hit_counter();
373 debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
374 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
375 jasmine_status.source_click = 1;
377 $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
383 describe("Check record list", function () {
384 it("check for single active client", function () {
385 if (!jasmine_status.source_click) {
386 debug("skip clients check due missing source click");
390 waitsFor(function () {
391 var clients = $("div.mkwsStat span.mkwsClientCount");
392 //debug("clients: " + clients.text());
393 return clients.length == 1 && clients.text().match("/1$");
394 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
397 var clients = $("div.mkwsStat span.mkwsClientCount");
398 debug("span.mkwsClientCount: " + clients.text());
399 expect(clients.text()).toMatch("/1$");
403 it("got a record", function () {
404 var linkaddr = "div.mkwsRecords div.mkwsSummary:nth-child(1) a";
406 waitsFor(function () {
407 // remove + insert node: must be at least 2
408 return $(linkaddr).length > 0;
409 }, "wait until we see a new record", 2.5 * jasmine_config.second);
412 expect($(linkaddr).length).toBeGreaterThan(0);
417 describe("Show record", function () {
418 var record_number = 1; // the Nth record in hit list
419 it("show record author", function () {
420 var click = $("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") a").trigger("click");
421 debug("show record click is success: " + click.length);
422 expect(click.length).toBe(1);
424 // wait until the record pops up
425 waitsFor(function () {
426 var show = $("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") > div.mkwsDetails");
427 //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.mkwsSummary").text());
428 return show != null && show.length ? true : false;
429 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
432 debug("show record pop up");
433 expect($("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div")).not.toBe(null);
437 it("extract URL", function () {
438 if (jasmine_config.show_record_url == false) {
439 debug("ignore test for URL in record")
443 var urls = $("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div table tbody tr td a");
444 debug("number of extracted URL from record: " + urls.length);
445 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
446 for (var i = 0; i < urls.length; i++) {
447 var url = $(urls[i]);
448 debug("URL: " + url.attr('href') + " text: " + url.text());
450 expect(url.attr('href')).not.toBe(null);
451 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
452 expect(url.text()).not.toBe("");
457 describe("Check switch menu Records/Targets", function () {
458 it("check mkwsSwitch", function () {
459 expect($("div.mkwsSwitch").length).toBe(1);
461 // expect 2 clickable links
462 expect($("div.mkwsSwitch a").length).toBe(2);
465 it("switch to target view", function () {
466 $("div.mkwsSwitch").children('a').eq(1).trigger("click");
468 // now the target table must be visible
469 expect($("div.mkwsTargets").is(":visible")).toBe(true);
470 expect($("div.mkwsRecords").is(":visible")).toBe(false);
472 // wait a half second, to show the target view
473 var time = (new Date).getTime();
474 waitsFor(function () {
475 return (new Date).getTime() - time > 700 ? true : false;
476 }, "wait some miliseconds", 1 * jasmine_config.second);
478 // look for table header
480 expect($("div.mkwsTargets").html()).toMatch(/Target ID/);
484 it("switch back to record view", function () {
485 $("div.mkwsSwitch").children('a').eq(0).trigger("click");
487 // now the target table must be visible
488 expect($("div.mkwsTargets").is(":visible")).toBe(false);
489 expect($("div.mkwsRecords").is(":visible")).toBe(true);
493 describe("Check status client counter", function () {
494 function get_time() {
495 var date = new Date();
496 return date.getTime();
498 var time = get_time();
500 it("check status clients", function () {
501 if (!jasmine_status.source_click) {
502 debug("skip clients check due missing source click");
506 waitsFor(function () {
507 var clients = $("div.mkwsStat span.mkwsClientCount");
508 debug("clients: " + clients.text());
509 if (clients.length == 1 && clients.text().match("0/1$")) {
514 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
517 var clients = $("div.mkwsStat span.mkwsClientCount");
518 debug("span.mkwsClientCount: " + clients.text());
519 expect(clients.text()).toMatch("0/1$");
525 describe("All tests are done", function () {
526 it(">>> hooray <<<", function () {
527 mkws.jasmine_done = true;