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);
65 debug("Oooops in get_hit_counter: " + RegExp.$1 + " '" + found + "'");
69 //debug("Hits: " + hits);
73 describe("Init jasmine config", function () {
74 it("jasmine was successfully initialized", function () {
75 init_jasmine_config();
77 expect(jasmine_config.search_query).toMatch(/\w/);
78 expect(jasmine_config.second).toBeGreaterThan(100);
79 expect(jasmine_config.max_time).toBeGreaterThan(1);
80 expect(jasmine_config.expected_hits).toBeGreaterThan(1);
85 xdescribe("Check MOTD before search", function () {
86 // Check that the MOTD has been moved into its container, and
87 // is visible before the search.
88 // the mkwsMOTD div was originally inside a testMOTD div, which should
90 // Note that the testMOTD is a regular div, and uses #testMOTD,
91 // since the automagic class-making does not apply to it.
92 it("MOTD is hidden", function () {
93 expect($(".mkwsMOTD").length).toBe(1);
94 expect($("#testMOTD").length).toBe(1);
95 expect($("#testMOTD").text()).toMatch("^ *$");
98 it("mkwsMOTDContainer has received the text", function () {
99 expect($(".mkwsMOTDContainer").length).toBe(1);
100 expect($(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
104 describe("Check pazpar2 search", function () {
105 it("pazpar2 was successfully initialized", function () {
106 expect(mkws.config.error).toBe(undefined);
109 it("validate HTML id's", function () {
110 expect($("input.mkwsQuery").length).toBe(1);
111 expect($("input.mkwsButton").length).toBe(1);
113 expect($(".mkwsNext").length).not.toBe(1);
114 expect($(".mkwsPrev").length).not.toBe(1);
117 it("run search query", function () {
118 var search_query = jasmine_config.search_query; // short hit counter with some paging
119 $("input.mkwsQuery").val(search_query);
120 debug("set search query: " + search_query)
121 expect($("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
123 if (mkws.config.use_service_proxy) {
124 // wait for service proxy auth
125 waitsFor(function () {
126 return mkws.authenticated;
127 }, "SP auth done", 10 * jasmine_config.second);
129 debug("running raw pp2, don't wait for mkws auth");
133 debug("Click on submit button");
134 $("input.mkwsButton").trigger("click");
139 describe("Check MOTD after search", function () {
140 it("MOTD is hidden", function () {
141 expect($(".mkwsMOTD").length).toBe(1);
142 expect($(".mkwsMOTD").is(":hidden")).toBe(true);
143 debug("motd t=" + $(".mkwsMOTD").text());
144 debug("motd v=" + $(".mkwsMOTD").is(":visible"));
150 * This part runs in background. It should be rewritten with
151 * async jasmine functions
154 describe("Check pazpar2 navigation", function () {
156 it("check running search next/prev", function () {
157 expect($(".mkwsPager").length).toBe(1);
159 function my_click(id, time) {
160 setTimeout(function () {
161 debug("trigger click on id: " + id);
162 $(id).trigger("click");
163 }, time * jasmine_config.second);
166 waitsFor(function () {
167 return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
168 }, "Expect next link 2", 10 * jasmine_config.second);
171 // click next/prev after N seconds
172 my_click(".mkwsNext", 0);
175 waitsFor(function () {
176 return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
177 }, "Expect next link 3", 5 * jasmine_config.second);
180 // click next/prev after N seconds
181 my_click(".mkwsNext", 0);
182 my_click(".mkwsPrev", 0.2);
187 describe("Check pazpar2 hit counter", function () {
188 it("check running search hit counter", function () {
189 var max_time = jasmine_config.max_time; // in seconds
190 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
193 waitsFor(function () {
194 hits = get_hit_counter();
195 return hits > expected_hits;
196 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
199 debug("mkws pager found records: '" + hits + "'");
200 expect($(".mkwsPager").length).toBe(1);
201 expect(hits).toBeGreaterThan(expected_hits);
206 describe("Check Termlist", function () {
207 it("found Termlist", function () {
208 var termlist = $("div.mkwsTermlists");
209 debug("Termlist success: " + termlist.length);
210 expect(termlist.length).toBe(1);
212 waitsFor(function () {
213 return $("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
214 }, "check for facet sources", 4 * jasmine_config.second);
216 // everything displayed?
218 var sources = $("div.mkwsFacet[data-mkws-facet='xtargets']");
219 debug("Termlist sources success: " + sources.length);
220 expect(sources.length).toBe(1);
222 var subjects = $("div.mkwsFacet[data-mkws-facet='subject']");
223 expect(subjects.length).toBe(1);
225 var authors = $("div.mkwsFacet[data-mkws-facet='author']");
226 expect(authors.length).toBe(1);
229 waitsFor(function () {
230 return $("div.mkwsFacet[data-mkws-facet='author'] div.term").length >= 2 ? true : false;
231 }, "At least one author link displayed", 4 * jasmine_config.second);
234 expect($("div.mkwsFacet[data-mkws-facet='author'] div.term").length).toBeGreaterThan(1);
239 describe("Check Author Facets", function () {
240 it("limit search to first author", function () {
241 if (mkws.config.disable_facet_authors_search) {
242 debug("Facets: ignore limit search for authors");
246 var hits_all_targets = get_hit_counter();
247 var author_number = 2; // 2=first author
248 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
249 // do not click on author names without a comma, e.g.: "Joe Barbara"
250 // because searching on such authors won't find anything.
252 var terms = $("div.mkwsFacet[data-mkws-facet='author'] div.term a");
253 for (var i = 0; i < terms.length; i++) {
254 var term = $(terms[i]).text();
255 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
256 debug("ignore author facet: " + term);
262 if ($("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text().length == 0) {
263 debug("No good authors found. Not clicking on the bad ones");
267 debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text());
268 $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").trigger("click");
271 waitsFor(function () {
272 var hits_single_target = get_hit_counter();
273 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
274 }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
277 var hits_single_target = get_hit_counter();
278 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
283 describe("Check active clients author", function () {
284 it("check for active clients after limited author search", function () {
285 waitsFor(function () {
286 var clients = $("div.mkwsStat span.clients");
287 //debug("clients: " + clients.text());
288 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]+$");
289 }, "wait for Active clients: x/y", 5 * jasmine_config.second);
292 var clients = $("div.mkwsStat span.clients");
293 debug("span.clients: " + clients.text());
294 expect(clients.text()).toMatch("/[1-9]+[0-9]+$");
296 // exact match of active clients (e.g. a SP misconfiguration)
297 if (jasmine_config.active_clients) {
298 debug("check for " + jasmine_config.active_clients + " active connections");
299 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
305 describe("Check Source Facets", function () {
306 it("limit search to first source", function () {
307 var hits_all_targets = get_hit_counter();
308 var source_number = 2; // 2=first source
309 // wait for a stat response
311 // do not click on wikipedia link - no author or subject facets possible
312 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.term a";
314 // wait for a visible source link in facets
315 waitsFor(function () {
317 return terms && terms.length > 0;
318 }, "wait for source facets after author search", 5 * jasmine_config.second);
323 for (var i = 0; i < terms.length; i++) {
324 var term = $(terms[i]).text();
325 debug("check for good source: " + term);
327 if (term.match(/wikipedia/i)) {
328 debug("ignore source facet: " + term);
334 debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
336 if ($("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").text().length == 0) {
337 debug("No good source found. Not clicking on the bad ones");
341 debug("click on source link nth-child(): " + source_number);
342 $("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").trigger("click");
344 $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
346 debug("DOM wait for stat: " + waitcount);
350 waitsFor(function () {
351 if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
356 }, "Search for source in navi bar", 4 * jasmine_config.second);
358 // Note: it may happens that limited source search returns the same number of hits
359 // as before. Thats not really an error, but unfortunate
360 waitsFor(function () {
361 var hits_single_target = get_hit_counter();
363 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
364 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
367 var hits_single_target = get_hit_counter();
368 debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
369 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
370 jasmine_status.source_click = 1;
372 $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
378 describe("Check record list", function () {
379 it("check for single active client", function () {
380 if (!jasmine_status.source_click) {
381 debug("skip clients check due missing source click");
385 waitsFor(function () {
386 var clients = $("div.mkwsStat span.clients");
387 //debug("clients: " + clients.text());
388 return clients.length == 1 && clients.text().match("/1$");
389 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
392 var clients = $("div.mkwsStat span.clients");
393 debug("span.clients: " + clients.text());
394 expect(clients.text()).toMatch("/1$");
398 it("got a record", function () {
399 var linkaddr = "div.mkwsRecords div.record:nth-child(1) a";
401 waitsFor(function () {
402 // remove + insert node: must be at least 2
403 return $(linkaddr).length > 0;
404 }, "wait until we see a new record", 2.5 * jasmine_config.second);
407 expect($(linkaddr).length).toBeGreaterThan(0);
412 describe("Show record", function () {
413 var record_number = 1; // the Nth record in hit list
414 it("show record author", function () {
415 var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
416 debug("show record click is success: " + click.length);
417 expect(click.length).toBe(1);
419 // wait until the record pops up
420 waitsFor(function () {
421 var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") > div.details");
422 //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.record").text());
423 return show != null && show.length ? true : false;
424 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
427 debug("show record pop up");
428 expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
432 it("extract URL", function () {
433 if (jasmine_config.show_record_url == false) {
434 debug("ignore test for URL in record")
438 var urls = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
439 debug("number of extracted URL from record: " + urls.length);
440 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
441 for (var i = 0; i < urls.length; i++) {
442 var url = $(urls[i]);
443 debug("URL: " + url.attr('href') + " text: " + url.text());
445 expect(url.attr('href')).not.toBe(null);
446 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
447 expect(url.text()).not.toBe("");
452 describe("Check switch menu Records/Targets", function () {
453 it("check mkwsSwitch", function () {
454 expect($("div.mkwsSwitch").length).toBe(1);
456 // expect 2 clickable links
457 expect($("div.mkwsSwitch a").length).toBe(2);
460 it("switch to target view", function () {
461 $("div.mkwsSwitch").children('a').eq(1).trigger("click");
463 // now the target table must be visible
464 expect($("div.mkwsBytarget").is(":visible")).toBe(true);
465 expect($("div.mkwsRecords").is(":visible")).toBe(false);
467 // wait a half second, to show the target view
468 var time = (new Date).getTime();
469 waitsFor(function () {
470 return (new Date).getTime() - time > 700 ? true : false;
471 }, "wait some miliseconds", 1 * jasmine_config.second);
473 // look for table header
475 expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
479 it("switch back to record view", function () {
480 $("div.mkwsSwitch").children('a').eq(0).trigger("click");
482 // now the target table must be visible
483 expect($("div.mkwsBytarget").is(":visible")).toBe(false);
484 expect($("div.mkwsRecords").is(":visible")).toBe(true);
488 describe("Check status client counter", function () {
489 function get_time() {
490 var date = new Date();
491 return date.getTime();
493 var time = get_time();
495 it("check status clients", function () {
496 if (!jasmine_status.source_click) {
497 debug("skip clients check due missing source click");
501 waitsFor(function () {
502 var clients = $("div.mkwsStat span.clients");
503 debug("clients: " + clients.text());
504 if (clients.length == 1 && clients.text().match("0/1$")) {
509 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
512 var clients = $("div.mkwsStat span.clients");
513 debug("span.clients: " + clients.text());
514 expect(clients.text()).toMatch("0/1$");
520 describe("All tests are done", function () {
521 it(">>> hooray <<<", function () {
522 mkws.jasmine_done = true;