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 beforeEach(function () {
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]);
47 var get_hit_counter = function () {
49 if ($("#mkwsPager").length == 0) return -1;
51 var found = $("#mkwsPager").text();
52 var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
57 hits = parseInt(RegExp.$1);
58 expect(hits).toBeGreaterThan(0);
61 //debug("Hits: " + hits);
65 describe("Check pazpar2 search", function () {
66 it("pazpar2 was successfully initialize", function () {
67 expect(mkws_config.error).toBe(undefined);
70 it("validate HTML id's", function () {
71 expect($("input#mkwsQuery").length).toBe(1);
72 expect($("input#mkwsButton").length).toBe(1);
74 expect($("#mkwsNext").length).not.toBe(1);
75 expect($("#mkwsPrev").length).not.toBe(1);
78 it("run search query", function () {
79 var search_query = jasmine_config.search_query; // short hit counter with some paging
80 $("input#mkwsQuery").val(search_query);
81 debug("set search query: " + search_query)
82 expect($("input#mkwsQuery").val()).toMatch("^" + search_query + "$");
84 if (mkws_config.use_service_proxy) {
85 // wait for service proxy auth
86 waitsFor(function () {
87 return mkws.authenticated;
88 }, "SP auth done", 10 * jasmine_config.second);
90 debug("running raw pp2, don't wait for mkws auth");
94 debug("Click on submit button");
95 var click = $("input#mkwsButton").trigger("click");
96 expect(click.length).toBe(1);
103 * This part runs in background. It should be rewritten with
104 * async jasmine functions
107 describe("Check pazpar2 navigation", function () {
109 it("check running search next/prev", function () {
110 expect($("#mkwsPager").length).toBe(1);
112 function my_click(id, time) {
113 setTimeout(function () {
114 debug("trigger click on id: " + id);
115 var click = $(id).trigger("click");
117 debug("next/prev: " + id + " click is success: " + click.length);
118 expect(click.length).toBe(1);
119 }, time * jasmine_config.second);
122 waitsFor(function () {
123 return $("div#mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
124 }, "Expect next link 2", 10 * jasmine_config.second);
127 // click next/prev after N seconds
128 my_click("#mkwsNext", 0);
131 waitsFor(function () {
132 return $("div#mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
133 }, "Expect next link 3", 5 * jasmine_config.second);
136 // click next/prev after N seconds
137 my_click("#mkwsNext", 0);
138 my_click("#mkwsPrev", 0.2);
143 describe("Check pazpar2 hit counter", function () {
144 it("check running search hit counter", function () {
145 var max_time = jasmine_config.max_time; // in seconds
146 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
149 waitsFor(function () {
150 hits = get_hit_counter();
152 return hits > expected_hits;
153 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
157 debug("mkws pager found records: '" + hits + "'");
158 expect($("#mkwsPager").length).toBe(1);
159 expect(hits).toBeGreaterThan(expected_hits);
164 describe("Check Termlist", function () {
165 it("found Termlist", function () {
166 var termlist = $("div#mkwsTermlists");
167 debug("Termlist success: " + termlist.length);
168 expect(termlist.length).toBe(1);
170 waitsFor(function () {
171 return $("div#mkwsFacetSources").length == 1 ? true : false;
172 }, "check for facet sources", 2 * jasmine_config.second);
175 // everything displayed?
177 var sources = $("div#mkwsFacetSources");
178 debug("Termlist sources success: " + sources.length);
179 expect(sources.length).toBe(1);
181 var subjects = $("div#mkwsFacetSubjects");
182 expect(subjects.length).toBe(1);
184 var authors = $("div#mkwsFacetAuthors");
185 expect(authors.length).toBe(1);
188 waitsFor(function () {
189 return $("div#mkwsFacetAuthors div.term").length >= 2 ? true : false;
190 }, "At least one author link displayed", 2 * jasmine_config.second);
193 expect($("div#mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
197 it("limit search to first author", function () {
198 var hits_all_targets = get_hit_counter();
199 var author_number = 2; // 2=first author
200 var author_name = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").text();
201 // do not click on author with numbers, e.g.: Bower, James M. Beeman, David, 1938-
202 if (author_name.match(/[0-9].+[0-9]/)) {
206 var click = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
207 debug("limit author click is success: " + click.length);
208 expect(click.length).toBe(1);
210 waitsFor(function () {
211 return get_hit_counter() < hits_all_targets ? true : false;
212 }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
215 var hits_single_target = get_hit_counter();
216 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
217 expect(hits_all_targets).toBeGreaterThan(hits_single_target);
221 it("limit search to first source", function () {
222 var hits_all_targets = get_hit_counter();
223 var source_number = 2; // 2=first source
224 var source_name = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").text();
225 // do not click on wikipedia link - no author or subject facets possible
226 if (source_name.match(/wikipedia/i)) {
230 var click = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
231 debug("limit source click " + (source_number - 1) + " is success: " + click.length);
232 expect(click.length).toBe(1);
234 waitsFor(function () {
235 if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/Source: /)) {
240 }, "Search for source in navi bar", jasmine_config.second);
242 // Note: it may happens that limited source search returns the same number of hits
243 // as before. Thats not really an error, but unfortunate
244 waitsFor(function () {
245 return get_hit_counter() <= hits_all_targets ? true : false;
246 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
249 var hits_single_target = get_hit_counter();
250 debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
251 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
256 describe("Show record", function () {
257 var record_number = 1; // the Nth record in hit list
258 it("show record author", function () {
259 var click = $("div#mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
260 debug("show record click is success: " + click.length);
261 expect(click.length).toBe(1);
263 // wait until the record pops up
264 waitsFor(function () {
265 var show = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div");
266 return show != null && show.length ? true : false;
267 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
270 debug("show record pop up");
271 expect($("div#mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
275 it("extract URL", function () {
276 if (jasmine_config.show_record_url == false) {
277 debug("ignore test for URL in record")
281 var url = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a").text();
282 debug("extracted URL from record: " + url);
284 expect(url).not.toBe(null);
285 expect(url).toMatch(/^https?:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
289 describe("Check switch menu Records/Targets", function () {
290 it("check mkwsSwitch", function () {
291 expect($("div#mkwsSwitch").length).toBe(1);
293 // expect 2 clickable links
294 expect($("div#mkwsSwitch a").length).toBe(2);
297 it("switch to target view", function () {
298 var click = $("a#mkwsSwitch_targets").trigger("click");
299 debug("target view click is success: " + click.length);
300 expect(click.length).toBe(1);
302 // now the target table must be visible
303 expect($("div#mkwsBytarget").is(":visible")).toBe(true);
304 expect($("div#mkwsRecords").is(":visible")).toBe(false);
306 // wait a half second, to show the target view
307 var time = (new Date).getTime();
308 waitsFor(function () {
309 return (new Date).getTime() - time > 700 ? true : false;
310 }, "wait some miliseconds", 1 * jasmine_config.second);
312 // look for table header
314 expect($("div#mkwsBytarget").html()).toMatch(/Target ID/);
318 it("switch back to record view", function () {
319 var click = $("a#mkwsSwitch_records").trigger("click");
320 debug("record view click is success: " + click.length);
321 expect(click.length).toBe(1);
323 // now the target table must be visible
324 expect($("div#mkwsBytarget").is(":visible")).toBe(false);
325 expect($("div#mkwsRecords").is(":visible")).toBe(true);
329 describe("Check status client counter", function () {
330 function get_time() {
331 var date = new Date();
332 return date.getTime();
334 var time = get_time();
336 it("check status clients", function () {
337 waitsFor(function () {
338 var clients = $("div#mkwsStat span.clients");
339 if (clients.length == 1 && clients.text().match("0/1$")) {
345 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
349 var clients = $("div#mkwsStat span.clients");
350 debug("span.clients: " + clients.text());
351 expect(clients.text()).toEqual("0/1");
360 describe("All tests are done", function () {
361 it(">>> hooray <<<", function () {});