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)
13 var jasmine_config = {};
15 /* check config for jasmine test
17 * you can override the default values in the config
18 * object: jasmine_config = {};
21 beforeEach(function () {
23 var jasmine_config_default = {
24 search_query: "freebsd",
28 // at least expected hit counter
30 // miliseconds to seconds
31 show_record_url: true,
32 // check for valid URL in records
36 // use default values for undefined values
37 for (var key in jasmine_config_default) {
38 if (!jasmine_config.hasOwnProperty(key)) {
39 jasmine_config[key] = jasmine_config_default[key];
44 var get_hit_counter = function () {
46 if ($("#mkwsPager").length == 0) return -1;
48 var found = $("#mkwsPager").text();
49 var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
54 hits = parseInt(RegExp.$1);
55 expect(hits).toBeGreaterThan(0);
58 //debug("Hits: " + hits);
62 describe("Check pazpar2 search", function () {
63 it("pazpar2 was successfully initialize", function () {
64 expect(mkws_config.error).toBe(undefined);
67 it("validate HTML id's", function () {
68 expect($("input#mkwsQuery").length).toBe(1);
69 expect($("input#mkwsButton").length).toBe(1);
71 expect($("#mkwsNext").length).not.toBe(1);
72 expect($("#mkwsPrev").length).not.toBe(1);
75 it("run search query", function () {
76 var search_query = jasmine_config.search_query; // short hit counter with some paging
77 $("input#mkwsQuery").val(search_query);
78 debug("set search query: " + search_query)
79 expect($("input#mkwsQuery").val()).toMatch("^" + search_query + "$");
81 if (mkws_config.use_service_proxy) {
82 // wait for service proxy auth
83 waitsFor(function () {
84 return mkws.authenticated;
85 }, "SP auth done", 10 * jasmine_config.second);
87 debug("running raw pp2, don't wait for mkws auth");
91 debug("Click on submit button");
92 var click = $("input#mkwsButton").trigger("click");
93 expect(click.length).toBe(1);
100 * This part runs in background. It should be rewritten with
101 * async jasmine functions
104 describe("Check pazpar2 navigation", function () {
106 it("check running search next/prev", function () {
107 expect($("#mkwsPager").length).toBe(1);
109 function my_click(id, time) {
110 setTimeout(function () {
111 debug("trigger click on id: " + id);
112 var click = $(id).trigger("click");
114 debug("next/prev: " + id + " click is success: " + click.length);
115 expect(click.length).toBe(1);
116 }, time * jasmine_config.second);
119 waitsFor(function () {
120 return $("div#mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
121 }, "Expect next link 2", 10 * jasmine_config.second);
124 // click next/prev after N seconds
125 my_click("#mkwsNext", 0);
128 waitsFor(function () {
129 return $("div#mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
130 }, "Expect next link 3", 5 * jasmine_config.second);
133 // click next/prev after N seconds
134 my_click("#mkwsNext", 0);
135 my_click("#mkwsPrev", 0.2);
140 describe("Check pazpar2 hit counter", function () {
141 it("check running search hit counter", function () {
142 var max_time = jasmine_config.max_time; // in seconds
143 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
146 waitsFor(function () {
147 hits = get_hit_counter();
149 return hits > expected_hits;
150 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
154 debug("mkws pager found records: '" + hits + "'");
155 expect($("#mkwsPager").length).toBe(1);
156 expect(hits).toBeGreaterThan(expected_hits);
161 describe("Check Termlist", function () {
162 it("found Termlist", function () {
163 var termlist = $("div#mkwsTermlists");
164 debug("Termlist success: " + termlist.length);
165 expect(termlist.length).toBe(1);
167 waitsFor(function () {
168 return $("div#mkwsFacetSources").length == 1 ? true : false;
169 }, "check for facet sources", 2 * jasmine_config.second);
172 // everything displayed?
174 var sources = $("div#mkwsFacetSources");
175 debug("Termlist sources success: " + sources.length);
176 expect(sources.length).toBe(1);
178 var subjects = $("div#mkwsFacetSubjects");
179 expect(subjects.length).toBe(1);
181 var authors = $("div#mkwsFacetAuthors");
182 expect(authors.length).toBe(1);
185 waitsFor(function () {
186 return $("div#mkwsFacetAuthors div.term").length >= 2 ? true : false;
187 }, "At least one author link displayed", 2 * jasmine_config.second);
190 expect($("div#mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
194 it("limit search to first author", function () {
195 var hits_all_targets = get_hit_counter();
196 var author_number = 2; // 2=first author
197 var author_name = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").text();
198 // do not click on author with numbers, e.g.: Bower, James M. Beeman, David, 1938-
199 if (author_name.match(/[0-9].+[0-9]/)) {
203 var click = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
204 debug("limit author click is success: " + click.length);
205 expect(click.length).toBe(1);
207 waitsFor(function () {
208 return get_hit_counter() < hits_all_targets ? true : false;
209 }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
212 var hits_single_target = get_hit_counter();
213 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
214 expect(hits_all_targets).toBeGreaterThan(hits_single_target);
218 it("limit search to first source", function () {
219 var hits_all_targets = get_hit_counter();
220 var source_number = 2; // 2=first source
221 var source_name = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").text();
222 // do not click on wikipedia link - no author or subject facets possible
223 if (source_name.match(/wikipedia/i)) {
227 var click = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
228 debug("limit source click " + (source_number - 1) + " is success: " + click.length);
229 expect(click.length).toBe(1);
231 waitsFor(function () {
232 if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/Source: /)) {
237 }, "Search for source in navi bar", jasmine_config.second);
239 // Note: it may happens that limited source search returns the same number of hits
240 // as before. Thats not really an error, but unfortunate
241 waitsFor(function () {
242 return get_hit_counter() <= hits_all_targets ? true : false;
243 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
246 var hits_single_target = get_hit_counter();
247 debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
248 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
253 describe("Show record", function () {
254 var record_number = 1; // the Nth record in hit list
255 it("show record author", function () {
256 var click = $("div#mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
257 debug("show record click is success: " + click.length);
258 expect(click.length).toBe(1);
260 // wait until the record pops up
261 waitsFor(function () {
262 var show = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div");
263 return show != null && show.length ? true : false;
264 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
267 debug("show record pop up");
268 expect($("div#mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
272 it("extract URL", function () {
273 if (jasmine_config.show_record_url == false) {
274 debug("ignore test for URL in record")
278 var url = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a").text();
279 debug("extracted URL from record: " + url);
281 expect(url).not.toBe(null);
282 expect(url).toMatch(/^https?:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
286 describe("Check switch menu Records/Targets", function () {
287 it("check mkwsSwitch", function () {
288 expect($("div#mkwsSwitch").length).toBe(1);
290 // expect 2 clickable links
291 expect($("div#mkwsSwitch a").length).toBe(2);
294 it("switch to target view", function () {
295 var click = $("a#mkwsSwitch_targets").trigger("click");
296 debug("target view click is success: " + click.length);
297 expect(click.length).toBe(1);
299 // now the target table must be visible
300 expect($("div#mkwsBytarget").is(":visible")).toBe(true);
301 expect($("div#mkwsRecords").is(":visible")).toBe(false);
303 // wait a half second, to show the target view
304 var time = (new Date).getTime();
305 waitsFor(function () {
306 return (new Date).getTime() - time > 700 ? true : false;
307 }, "wait some miliseconds", 1 * jasmine_config.second);
309 // look for table header
311 expect($("div#mkwsBytarget").html()).toMatch(/Target ID/);
315 it("switch back to record view", function () {
316 var click = $("a#mkwsSwitch_records").trigger("click");
317 debug("record view click is success: " + click.length);
318 expect(click.length).toBe(1);
320 // now the target table must be visible
321 expect($("div#mkwsBytarget").is(":visible")).toBe(false);
322 expect($("div#mkwsRecords").is(":visible")).toBe(true);
326 describe("Check status client counter", function () {
327 function get_time() {
328 var date = new Date();
329 return date.getTime();
331 var time = get_time();
333 it("check status clients", function () {
334 waitsFor(function () {
335 var clients = $("div#mkwsStat span.clients");
336 if (clients.length == 1 && clients.text().match("0/1$")) {
342 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
346 var clients = $("div#mkwsStat span.clients");
347 debug("span.clients: " + clients.text());
348 expect(clients.text()).toEqual("0/1");
357 describe("All tests are done", function () {
358 it(">>> hooray <<<", function () {});