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.debug_function(text)
12 // Define empty mkws_config for simple applications that don't define it.
13 if (jasmine_config == null || typeof jasmine_config != 'object') {
14 var jasmine_config = {};
17 /* check config for jasmine test
19 * you can override the default values in the config
20 * object: jasmine_config = {};
23 function init_jasmine_config() {
25 var jasmine_config_default = {
26 search_query: "freebsd",
30 // at least expected hit counter
32 // miliseconds to seconds
33 show_record_url: true,
34 // check for valid URL in records
38 // use default values for undefined values
39 for (var key in jasmine_config_default) {
40 if (!jasmine_config.hasOwnProperty(key)) {
41 jasmine_config[key] = jasmine_config_default[key];
43 debug("jasmine config: " + key + " => " + jasmine_config[key]);
46 mkws.jasmine_done = false;
49 var get_hit_counter = function () {
51 if ($("#mkwsPager").length == 0) return -1;
53 var found = $("#mkwsPager").text();
54 var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
59 hits = parseInt(RegExp.$1);
60 expect(hits).toBeGreaterThan(0);
63 //debug("Hits: " + hits);
67 describe("Init jasmine config", function () {
68 it("jasmine was successfully initialized", function () {
69 init_jasmine_config();
71 expect(jasmine_config.search_query).toMatch(/\w/);
72 expect(jasmine_config.second).toBeGreaterThan(100);
73 expect(jasmine_config.max_time).toBeGreaterThan(1);
74 expect(jasmine_config.expected_hits).toBeGreaterThan(1);
78 describe("Check pazpar2 search", function () {
79 it("pazpar2 was successfully initialized", function () {
80 expect(mkws_config.error).toBe(undefined);
83 it("validate HTML id's", function () {
84 expect($("input#mkwsQuery").length).toBe(1);
85 expect($("input#mkwsButton").length).toBe(1);
87 expect($("#mkwsNext").length).not.toBe(1);
88 expect($("#mkwsPrev").length).not.toBe(1);
91 it("run search query", function () {
92 var search_query = jasmine_config.search_query; // short hit counter with some paging
93 $("input#mkwsQuery").val(search_query);
94 debug("set search query: " + search_query)
95 expect($("input#mkwsQuery").val()).toMatch("^" + search_query + "$");
97 if (mkws_config.use_service_proxy) {
98 // wait for service proxy auth
99 waitsFor(function () {
100 return mkws.authenticated;
101 }, "SP auth done", 10 * jasmine_config.second);
103 debug("running raw pp2, don't wait for mkws auth");
107 debug("Click on submit button");
108 var click = $("input#mkwsButton").trigger("click");
109 expect(click.length).toBe(1);
116 * This part runs in background. It should be rewritten with
117 * async jasmine functions
120 describe("Check pazpar2 navigation", function () {
122 it("check running search next/prev", function () {
123 expect($("#mkwsPager").length).toBe(1);
125 function my_click(id, time) {
126 setTimeout(function () {
127 debug("trigger click on id: " + id);
128 var click = $(id).trigger("click");
130 debug("next/prev: " + id + " click is success: " + click.length);
131 expect(click.length).toBe(1);
132 }, time * jasmine_config.second);
135 waitsFor(function () {
136 return $("div#mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
137 }, "Expect next link 2", 10 * jasmine_config.second);
140 // click next/prev after N seconds
141 my_click("#mkwsNext", 0);
144 waitsFor(function () {
145 return $("div#mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
146 }, "Expect next link 3", 5 * jasmine_config.second);
149 // click next/prev after N seconds
150 my_click("#mkwsNext", 0);
151 my_click("#mkwsPrev", 0.2);
156 describe("Check pazpar2 hit counter", function () {
157 it("check running search hit counter", function () {
158 var max_time = jasmine_config.max_time; // in seconds
159 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
162 waitsFor(function () {
163 hits = get_hit_counter();
165 return hits > expected_hits;
166 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
170 debug("mkws pager found records: '" + hits + "'");
171 expect($("#mkwsPager").length).toBe(1);
172 expect(hits).toBeGreaterThan(expected_hits);
177 describe("Check Termlist", function () {
178 it("found Termlist", function () {
179 var termlist = $("div#mkwsTermlists");
180 debug("Termlist success: " + termlist.length);
181 expect(termlist.length).toBe(1);
183 waitsFor(function () {
184 return $("div#mkwsFacetSources").length == 1 ? true : false;
185 }, "check for facet sources", 4 * jasmine_config.second);
188 // everything displayed?
190 var sources = $("div#mkwsFacetSources");
191 debug("Termlist sources success: " + sources.length);
192 expect(sources.length).toBe(1);
194 var subjects = $("div#mkwsFacetSubjects");
195 expect(subjects.length).toBe(1);
197 var authors = $("div#mkwsFacetAuthors");
198 expect(authors.length).toBe(1);
201 waitsFor(function () {
202 return $("div#mkwsFacetAuthors div.term").length >= 2 ? true : false;
203 }, "At least one author link displayed", 4 * jasmine_config.second);
206 expect($("div#mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
210 it("limit search to first author", function () {
211 if (!mkws_config.use_service_proxy) {
212 debug("running raw pp2, ignore non-functional limit search for authors");
216 var hits_all_targets = get_hit_counter();
217 var author_number = 2; // 2=first author
218 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
219 // do not click on author names without a comma, e.g.: "Joe Barbara"
220 var terms = $("div#mkwsFacetAuthors div.term a");
221 for (var i = 0; i < terms.length; i++) {
222 var term = $(terms[i]).text();
223 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
224 debug("ignore author facet: " + term);
231 var click = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
232 debug("limit author click is success: " + click.length);
233 expect(click.length).toBe(1);
235 waitsFor(function () {
236 return get_hit_counter() < hits_all_targets ? true : false;
237 }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
240 var hits_single_target = get_hit_counter();
241 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
242 expect(hits_all_targets).toBeGreaterThan(hits_single_target);
246 it("limit search to first source", function () {
247 var hits_all_targets = get_hit_counter();
248 var source_number = 2; // 2=first source
249 // do not click on wikipedia link - no author or subject facets possible
250 var terms = $("div#mkwsFacetSources div.term a");
251 for (var i = 0; i < terms.length; i++) {
252 var term = $(terms[i]).text();
253 if (term.match(/wikipedia/i)) {
254 debug("ignore source facet: " + term);
261 var click = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
262 debug("limit source click " + (source_number - 1) + " is success: " + click.length);
263 expect(click.length).toBe(1);
265 waitsFor(function () {
266 if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
271 }, "Search for source in navi bar", 4 * jasmine_config.second);
273 // Note: it may happens that limited source search returns the same number of hits
274 // as before. Thats not really an error, but unfortunate
275 waitsFor(function () {
276 return get_hit_counter() <= hits_all_targets ? true : false;
277 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
280 var hits_single_target = get_hit_counter();
281 debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
282 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
287 describe("Show record", function () {
288 var record_number = 1; // the Nth record in hit list
289 it("show record author", function () {
290 var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
291 debug("show record click is success: " + click.length);
292 expect(click.length).toBe(1);
294 // wait until the record pops up
295 waitsFor(function () {
296 var show = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div");
297 return show != null && show.length ? true : false;
298 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
301 debug("show record pop up");
302 expect($("div#mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
306 it("extract URL", function () {
307 if (jasmine_config.show_record_url == false) {
308 debug("ignore test for URL in record")
312 var urls = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
313 debug("number of extracted URL from record: " + urls.length);
314 for (var i = 0; i < urls.length; i++) {
315 var url = $(urls[i]);
316 debug("URL: " + url.attr('href'));
317 expect(url.attr('href')).not.toBe(null);
318 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
319 expect(url.attr('href')).toBe(url.text());
324 describe("Check switch menu Records/Targets", function () {
325 it("check mkwsSwitch", function () {
326 expect($("div.mkwsSwitch").length).toBe(1);
328 // expect 2 clickable links
329 expect($("div.mkwsSwitch a").length).toBe(2);
332 it("switch to target view", function () {
333 var click = $("div.mkwsSwitch").children('a').eq(1).trigger("click");
334 debug("target view click is success: " + click.length);
335 expect(click.length).toBe(1);
337 // now the target table must be visible
338 expect($("div.mkwsBytarget").is(":visible")).toBe(true);
339 expect($("div#mkwsRecords").is(":visible")).toBe(false);
341 // wait a half second, to show the target view
342 var time = (new Date).getTime();
343 waitsFor(function () {
344 return (new Date).getTime() - time > 700 ? true : false;
345 }, "wait some miliseconds", 1 * jasmine_config.second);
347 // look for table header
349 expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
353 it("switch back to record view", function () {
354 var click = $("div.mkwsSwitch").children('a').eq(0).trigger("click");
355 debug("record view click is success: " + click.length);
356 expect(click.length).toBe(1);
358 // now the target table must be visible
359 expect($("div#mkwsBytarget").is(":visible")).toBe(false);
360 expect($("div#mkwsRecords").is(":visible")).toBe(true);
364 describe("Check status client counter", function () {
365 function get_time() {
366 var date = new Date();
367 return date.getTime();
369 var time = get_time();
371 it("check status clients", function () {
372 waitsFor(function () {
373 var clients = $("div#mkwsStat span.clients");
374 if (clients.length == 1 && clients.text().match("0/1$")) {
380 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
384 var clients = $("div#mkwsStat span.clients");
385 debug("span.clients: " + clients.text());
386 expect(clients.text()).toEqual("0/1");
395 describe("All tests are done", function () {
396 it(">>> hooray <<<", function () {
397 mkws.jasmine_done = true;