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);
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 = mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm a");
258 for (var i = 0; i < terms.length; i++) {
259 var term = mkws.$(terms[i]).text();
260 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
261 debug("ignore author facet: " + term);
267 if (mkws.$("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 + ") " + mkws.$("div.mkwsFacet[data-mkws-facet='author'] div.mkwsTerm:nth-child(" + author_number + ") a").text());
273 mkws.$("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 console.log("hits_single_target='" + hits_single_target + "' cf. hits_all_targets='" + hits_all_targets + "'");
279 return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
280 }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
283 var hits_single_target = get_hit_counter();
284 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
289 describe("Check active clients author", function () {
290 it("check for active clients after limited author search", function () {
291 waitsFor(function () {
292 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
293 // debug("clients: " + clients.text());
294 return clients.length == 1 && clients.text().match("/[1-9]+[0-9]*$");
295 }, "wait for Active clients: x/y", 5.5 * jasmine_config.second);
298 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
299 debug("span.mkwsClientCount: " + clients.text());
300 expect(clients.text()).toMatch("/[1-9]+[0-9]*$");
302 // exact match of active clients (e.g. a SP misconfiguration)
303 if (jasmine_config.active_clients) {
304 debug("check for " + jasmine_config.active_clients + " active connections");
305 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
311 describe("Check Source Facets", function () {
312 it("limit search to first source", function () {
313 var hits_all_targets = get_hit_counter();
314 var source_number = 2; // 2=first source
315 // wait for a stat response
317 // do not click on wikipedia link - no author or subject facets possible
318 var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm a";
320 // wait for a visible source link in facets
321 waitsFor(function () {
322 var terms = mkws.$(link);
323 return terms && terms.length > 0;
324 }, "wait for source facets after author search", 5 * jasmine_config.second);
328 var terms = mkws.$(link);
329 for (var i = 0; i < terms.length; i++) {
330 var term = mkws.$(terms[i]).text();
331 debug("check for good source: " + term);
333 if (term.match(/wikipedia/i)) {
334 debug("ignore source facet: " + term);
340 debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
342 if (mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").text().length == 0) {
343 debug("No good source found. Not clicking on the bad ones");
347 debug("click on source link nth-child(): " + source_number);
348 mkws.$("div.mkwsFacet[data-mkws-facet='xtargets'] div.mkwsTerm:nth-child(" + source_number + ") a").trigger("click");
350 mkws.$(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
352 debug("DOM wait for stat: " + waitcount);
356 waitsFor(function () {
357 if (mkws.$("div.mkwsNavi").length && mkws.$("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
362 }, "Search for source in navi bar", 4 * jasmine_config.second);
364 // Note: it may happens that limited source search returns the same number of hits
365 // as before. Thats not really an error, but unfortunate
366 waitsFor(function () {
367 var hits_single_target = get_hit_counter();
369 return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
370 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
373 var hits_single_target = get_hit_counter();
374 debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
375 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
376 jasmine_status.source_click = 1;
378 mkws.$(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
384 describe("Check record list", function () {
385 it("check for single active client", function () {
386 if (!jasmine_status.source_click) {
387 debug("skip clients check due missing source click");
391 waitsFor(function () {
392 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
393 //debug("clients: " + clients.text());
394 return clients.length == 1 && clients.text().match("/1$");
395 }, "wait for Active clients: x/1", 5 * jasmine_config.second);
398 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
399 debug("span.mkwsClientCount: " + clients.text());
400 expect(clients.text()).toMatch("/1$");
404 it("got a record", function () {
405 var linkaddr = "div.mkwsRecords div.mkwsSummary:nth-child(1) a";
407 waitsFor(function () {
408 // remove + insert node: must be at least 2
409 return mkws.$(linkaddr).length > 0;
410 }, "wait until we see a new record", 2.5 * jasmine_config.second);
413 expect(mkws.$(linkaddr).length).toBeGreaterThan(0);
418 describe("Show record", function () {
419 var record_number = 1; // the Nth record in hit list
420 it("show record author", function () {
421 var click = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") a").trigger("click");
422 debug("show record click is success: " + click.length);
423 expect(click.length).toBe(1);
425 // wait until the record pops up
426 waitsFor(function () {
427 var show = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") > div.mkwsDetails");
428 //debug("poprecord: " + (show ? show.length : -1) + " " + mkws.$("div.mkwsRecords div.mkwsSummary").text());
429 return show != null && show.length ? true : false;
430 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
433 debug("show record pop up");
434 expect(mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div")).not.toBe(null);
438 it("extract URL", function () {
439 if (jasmine_config.show_record_url == false) {
440 debug("ignore test for URL in record")
444 var urls = mkws.$("div.mkwsRecords div.mkwsSummary:nth-child(" + record_number + ") div table tbody tr td a");
445 debug("number of extracted URL from record: " + urls.length);
446 // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
447 for (var i = 0; i < urls.length; i++) {
448 var url = mkws.$(urls[i]);
449 debug("URL: " + url.attr('href') + " text: " + url.text());
451 expect(url.attr('href')).not.toBe(null);
452 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
453 expect(url.text()).not.toBe("");
458 describe("Check switch menu Records/Targets", function () {
459 it("check mkwsSwitch", function () {
460 expect(mkws.$("div.mkwsSwitch").length).toBe(1);
462 // expect 2 clickable links
463 expect(mkws.$("div.mkwsSwitch a").length).toBe(2);
466 it("switch to target view", function () {
467 mkws.$("div.mkwsSwitch").children('a').eq(1).trigger("click");
469 // now the target table must be visible
470 expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(true);
471 expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(false);
473 // wait a half second, to show the target view
474 var time = (new Date).getTime();
475 waitsFor(function () {
476 return (new Date).getTime() - time > 700 ? true : false;
477 }, "wait some miliseconds", 1 * jasmine_config.second);
479 // look for table header
481 expect(mkws.$("div.mkwsTargets").html()).toMatch(/Target ID/);
485 it("switch back to record view", function () {
486 mkws.$("div.mkwsSwitch").children('a').eq(0).trigger("click");
488 // now the target table must be visible
489 expect(mkws.$("div.mkwsTargets").is(":visible")).toBe(false);
490 expect(mkws.$("div.mkwsRecords").is(":visible")).toBe(true);
494 describe("Check status client counter", function () {
495 function get_time() {
496 var date = new Date();
497 return date.getTime();
499 var time = get_time();
501 it("check status clients", function () {
502 if (!jasmine_status.source_click) {
503 debug("skip clients check due missing source click");
507 waitsFor(function () {
508 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
509 debug("clients: " + clients.text());
510 if (clients.length == 1 && clients.text().match("0/1$")) {
515 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
518 var clients = mkws.$("div.mkwsStat span.mkwsClientCount");
519 debug("span.mkwsClientCount: " + clients.text());
520 expect(clients.text()).toMatch("0/1$");
526 describe("All tests are done", function () {
527 it(">>> hooray <<<", function () {
528 mkws.jasmine_done = true;