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);
67 expect(hits).toBeGreaterThan(0);
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 expect($(".mkwsMOTD").length).toBe(1);
143 expect($(".mkwsMOTD").is(":hidden")).toBe(true);
144 debug("motd t=" + $(".mkwsMOTD").text());
145 debug("motd v=" + $(".mkwsMOTD").is(":visible"));
151 * This part runs in background. It should be rewritten with
152 * async jasmine functions
155 describe("Check pazpar2 navigation", function () {
157 it("check running search next/prev", function () {
158 expect($(".mkwsPager").length).toBe(1);
160 function my_click(id, time) {
161 setTimeout(function () {
162 debug("trigger click on id: " + id);
163 $(id).trigger("click");
164 }, time * jasmine_config.second);
167 waitsFor(function () {
168 return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
169 }, "Expect next link 2", 10 * jasmine_config.second);
172 // click next/prev after N seconds
173 my_click(".mkwsNext", 0);
176 waitsFor(function () {
177 return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
178 }, "Expect next link 3", 5 * jasmine_config.second);
181 // click next/prev after N seconds
182 my_click(".mkwsNext", 0);
183 my_click(".mkwsPrev", 0.2);
188 describe("Check pazpar2 hit counter", function () {
189 it("check running search hit counter", function () {
190 var max_time = jasmine_config.max_time; // in seconds
191 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
194 waitsFor(function () {
195 hits = get_hit_counter();
196 return hits > expected_hits;
197 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
200 debug("mkws pager found records: '" + hits + "'");
201 expect($(".mkwsPager").length).toBe(1);
202 expect(hits).toBeGreaterThan(expected_hits);
207 describe("Check Termlist", function () {
208 it("found Termlist", function () {
209 var termlist = $("div.mkwsTermlists");
210 debug("Termlist success: " + termlist.length);
211 expect(termlist.length).toBe(1);
213 waitsFor(function () {
214 return $("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
215 }, "check for facet sources", 4 * jasmine_config.second);
217 // everything displayed?
219 var sources = $("div.mkwsFacet[data-mkws-facet='xtargets']");
220 debug("Termlist sources success: " + sources.length);
221 expect(sources.length).toBe(1);
223 var subjects = $("div.mkwsFacet[data-mkws-facet='subject']");
224 expect(subjects.length).toBe(1);
226 var authors = $("div.mkwsFacet[data-mkws-facet='author']");
227 expect(authors.length).toBe(1);
230 waitsFor(function () {
231 return $("div.mkwsFacet[data-mkws-facet='author'] div.term").length >= 2 ? true : false;
232 }, "At least one author link displayed", 4 * jasmine_config.second);
235 expect($("div.mkwsFacet[data-mkws-facet='author'] div.term").length).toBeGreaterThan(1);
240 describe("Check Author Facets", function () {
241 it("limit search to first author", function () {
242 if (mkws.config.disable_facet_authors_search) {
243 debug("Facets: ignore limit search for authors");
247 var hits_all_targets = get_hit_counter();
248 var author_number = 2; // 2=first author
249 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
250 // do not click on author names without a comma, e.g.: "Joe Barbara"
251 // because searching on such authors won't find anything.
253 var terms = $("div.mkwsFacet[data-mkws-facet='author'] div.term a");
254 for (var i = 0; i < terms.length; i++) {
255 var term = $(terms[i]).text();
256 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
257 debug("ignore author facet: " + term);
263 if ($("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text().length == 0) {
264 debug("No good authors found. Not clicking on the bad ones");
268 debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text());
269 $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").trigger("click");
272 waitsFor(function () {
273 var hits_single_target = get_hit_counter();
274 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
275 }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
278 var hits_single_target = get_hit_counter();
279 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
284 describe("Check active clients author", function () {
285 it("check for active clients after limited author search", function () {
286 waitsFor(function () {
287 var clients = $("div.mkwsStat span.clients");
288 //debug("clients: " + clients.text());
289 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]+$");
290 }, "wait for Active clients: x/y", 5 * jasmine_config.second);
293 var clients = $("div.mkwsStat span.clients");
294 debug("span.clients: " + clients.text());
295 expect(clients.text()).toMatch("/[1-9]+[0-9]+$");
297 // exact match of active clients (e.g. a SP misconfiguration)
298 if (jasmine_config.active_clients) {
299 debug("check for " + jasmine_config.active_clients + " active connections");
300 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
306 describe("Check Source Facets", function () {
307 it("limit search to first source", function () {
308 var hits_all_targets = get_hit_counter();
309 var source_number = 2; // 2=first source
310 // wait for a stat response
312 // do not click on wikipedia link - no author or subject facets possible
313 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.term a";
315 // wait for a visible source link in facets
316 waitsFor(function () {
318 return terms && terms.length > 0;
319 }, "wait for source facets after author search", 5 * jasmine_config.second);
324 for (var i = 0; i < terms.length; i++) {
325 var term = $(terms[i]).text();
326 debug("check for good source: " + term);
328 if (term.match(/wikipedia/i)) {
329 debug("ignore source facet: " + term);
335 debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
337 if ($("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").text().length == 0) {
338 debug("No good source found. Not clicking on the bad ones");
342 debug("click on source link nth-child(): " + source_number);
343 $("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").trigger("click");
345 $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
347 debug("DOM wait for stat: " + waitcount);
351 waitsFor(function () {
352 if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
357 }, "Search for source in navi bar", 4 * jasmine_config.second);
359 // Note: it may happens that limited source search returns the same number of hits
360 // as before. Thats not really an error, but unfortunate
361 waitsFor(function () {
362 var hits_single_target = get_hit_counter();
364 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
365 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
368 var hits_single_target = get_hit_counter();
369 debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
370 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
371 jasmine_status.source_click = 1;
373 $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
379 describe("Check record list", function () {
380 it("check for single active client", function () {
381 if (!jasmine_status.source_click) {
382 debug("skip clients check due missing source click");
386 waitsFor(function () {
387 var clients = $("div.mkwsStat span.clients");
388 //debug("clients: " + clients.text());
389 return clients.length == 1 && clients.text().match("/1$");
390 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
393 var clients = $("div.mkwsStat span.clients");
394 debug("span.clients: " + clients.text());
395 expect(clients.text()).toMatch("/1$");
399 it("got a record", function () {
400 var linkaddr = "div.mkwsRecords div.record:nth-child(1) a";
402 waitsFor(function () {
403 // remove + insert node: must be at least 2
404 return $(linkaddr).length > 0;
405 }, "wait until we see a new record", 2.5 * jasmine_config.second);
408 expect($(linkaddr).length).toBeGreaterThan(0);
413 describe("Show record", function () {
414 var record_number = 1; // the Nth record in hit list
415 it("show record author", function () {
416 var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
417 debug("show record click is success: " + click.length);
418 expect(click.length).toBe(1);
420 // wait until the record pops up
421 waitsFor(function () {
422 var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") > div.details");
423 //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.record").text());
424 return show != null && show.length ? true : false;
425 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
428 debug("show record pop up");
429 expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
433 it("extract URL", function () {
434 if (jasmine_config.show_record_url == false) {
435 debug("ignore test for URL in record")
439 var urls = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
440 debug("number of extracted URL from record: " + urls.length);
441 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
442 for (var i = 0; i < urls.length; i++) {
443 var url = $(urls[i]);
444 debug("URL: " + url.attr('href') + " text: " + url.text());
446 expect(url.attr('href')).not.toBe(null);
447 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
448 expect(url.text()).not.toBe("");
453 describe("Check switch menu Records/Targets", function () {
454 it("check mkwsSwitch", function () {
455 expect($("div.mkwsSwitch").length).toBe(1);
457 // expect 2 clickable links
458 expect($("div.mkwsSwitch a").length).toBe(2);
461 it("switch to target view", function () {
462 $("div.mkwsSwitch").children('a').eq(1).trigger("click");
464 // now the target table must be visible
465 expect($("div.mkwsBytarget").is(":visible")).toBe(true);
466 expect($("div.mkwsRecords").is(":visible")).toBe(false);
468 // wait a half second, to show the target view
469 var time = (new Date).getTime();
470 waitsFor(function () {
471 return (new Date).getTime() - time > 700 ? true : false;
472 }, "wait some miliseconds", 1 * jasmine_config.second);
474 // look for table header
476 expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
480 it("switch back to record view", function () {
481 $("div.mkwsSwitch").children('a').eq(0).trigger("click");
483 // now the target table must be visible
484 expect($("div.mkwsBytarget").is(":visible")).toBe(false);
485 expect($("div.mkwsRecords").is(":visible")).toBe(true);
489 describe("Check status client counter", function () {
490 function get_time() {
491 var date = new Date();
492 return date.getTime();
494 var time = get_time();
496 it("check status clients", function () {
497 if (!jasmine_status.source_click) {
498 debug("skip clients check due missing source click");
502 waitsFor(function () {
503 var clients = $("div.mkwsStat span.clients");
504 debug("clients: " + clients.text());
505 if (clients.length == 1 && clients.text().match("0/1$")) {
510 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
513 var clients = $("div.mkwsStat span.clients");
514 debug("span.clients: " + clients.text());
515 expect(clients.text()).toMatch("0/1$");
521 describe("All tests are done", function () {
522 it(">>> hooray <<<", function () {
523 mkws.jasmine_done = true;