1 /* Copyright (c) 2013 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 initialize", 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 var hits_all_targets = get_hit_counter();
212 var author_number = 2; // 2=first author
213 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
214 // do not click on author names without a comma, e.g.: "Joe Barbara"
215 var terms = $("div#mkwsFacetAuthors div.term a");
216 for (var i = 0; i < terms.length; i++) {
217 var term = $(terms[i]).text();
218 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
219 debug("ignore author facet: " + term);
226 var click = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
227 debug("limit author click is success: " + click.length);
228 expect(click.length).toBe(1);
230 waitsFor(function () {
231 return get_hit_counter() < hits_all_targets ? true : false;
232 }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
235 var hits_single_target = get_hit_counter();
236 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
237 expect(hits_all_targets).toBeGreaterThan(hits_single_target);
241 it("limit search to first source", function () {
242 var hits_all_targets = get_hit_counter();
243 var source_number = 2; // 2=first source
244 // do not click on wikipedia link - no author or subject facets possible
245 var terms = $("div#mkwsFacetSources div.term a");
246 for (var i = 0; i < terms.length; i++) {
247 var term = $(terms[i]).text();
248 if (term.match(/wikipedia/i)) {
249 debug("ignore source facet: " + term);
256 var click = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
257 debug("limit source click " + (source_number - 1) + " is success: " + click.length);
258 expect(click.length).toBe(1);
260 waitsFor(function () {
261 if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/Source: /)) {
266 }, "Search for source in navi bar", 4 * jasmine_config.second);
268 // Note: it may happens that limited source search returns the same number of hits
269 // as before. Thats not really an error, but unfortunate
270 waitsFor(function () {
271 return get_hit_counter() <= hits_all_targets ? true : false;
272 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
275 var hits_single_target = get_hit_counter();
276 debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
277 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
282 describe("Show record", function () {
283 var record_number = 1; // the Nth record in hit list
284 it("show record author", function () {
285 var click = $("div#mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
286 debug("show record click is success: " + click.length);
287 expect(click.length).toBe(1);
289 // wait until the record pops up
290 waitsFor(function () {
291 var show = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div");
292 return show != null && show.length ? true : false;
293 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
296 debug("show record pop up");
297 expect($("div#mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
301 it("extract URL", function () {
302 if (jasmine_config.show_record_url == false) {
303 debug("ignore test for URL in record")
307 var urls = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
308 debug("number of extracted URL from record: " + urls.length);
309 for (var i = 0; i < urls.length; i++) {
310 var url = $(urls[i]);
311 debug("URL: " + url.attr('href'));
312 expect(url.attr('href')).not.toBe(null);
313 expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
314 expect(url.attr('href')).toBe(url.text());
319 describe("Check switch menu Records/Targets", function () {
320 it("check mkwsSwitch", function () {
321 expect($("div.mkwsSwitch").length).toBe(1);
323 // expect 2 clickable links
324 expect($("div.mkwsSwitch a").length).toBe(2);
327 it("switch to target view", function () {
328 var click = $("a.mkwsSwitch_targets").trigger("click");
329 debug("target view click is success: " + click.length);
330 expect(click.length).toBe(1);
332 // now the target table must be visible
333 expect($("div.mkwsBytarget").is(":visible")).toBe(true);
334 expect($("div#mkwsRecords").is(":visible")).toBe(false);
336 // wait a half second, to show the target view
337 var time = (new Date).getTime();
338 waitsFor(function () {
339 return (new Date).getTime() - time > 700 ? true : false;
340 }, "wait some miliseconds", 1 * jasmine_config.second);
342 // look for table header
344 expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
348 it("switch back to record view", function () {
349 var click = $("a.mkwsSwitch_records").trigger("click");
350 debug("record view click is success: " + click.length);
351 expect(click.length).toBe(1);
353 // now the target table must be visible
354 expect($("div#mkwsBytarget").is(":visible")).toBe(false);
355 expect($("div#mkwsRecords").is(":visible")).toBe(true);
359 describe("Check status client counter", function () {
360 function get_time() {
361 var date = new Date();
362 return date.getTime();
364 var time = get_time();
366 it("check status clients", function () {
367 waitsFor(function () {
368 var clients = $("div#mkwsStat span.clients");
369 if (clients.length == 1 && clients.text().match("0/1$")) {
375 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
379 var clients = $("div#mkwsStat span.clients");
380 debug("span.clients: " + clients.text());
381 expect(clients.text()).toEqual("0/1");
390 describe("All tests are done", function () {
391 it(">>> hooray <<<", function () {
392 mkws.jasmine_done = true;