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]);
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("Init jasmine config", function () {
66 it("jasmine was successfully initialized", function () {
67 init_jasmine_config();
69 expect(jasmine_config.search_query).toMatch(/\w/);
70 expect(jasmine_config.second).toBeGreaterThan(100);
71 expect(jasmine_config.max_time).toBeGreaterThan(1);
72 expect(jasmine_config.expected_hits).toBeGreaterThan(1);
76 describe("Check pazpar2 search", function () {
77 it("pazpar2 was successfully initialize", function () {
78 expect(mkws_config.error).toBe(undefined);
81 it("validate HTML id's", function () {
82 expect($("input#mkwsQuery").length).toBe(1);
83 expect($("input#mkwsButton").length).toBe(1);
85 expect($("#mkwsNext").length).not.toBe(1);
86 expect($("#mkwsPrev").length).not.toBe(1);
89 it("run search query", function () {
90 var search_query = jasmine_config.search_query; // short hit counter with some paging
91 $("input#mkwsQuery").val(search_query);
92 debug("set search query: " + search_query)
93 expect($("input#mkwsQuery").val()).toMatch("^" + search_query + "$");
95 if (mkws_config.use_service_proxy) {
96 // wait for service proxy auth
97 waitsFor(function () {
98 return mkws.authenticated;
99 }, "SP auth done", 10 * jasmine_config.second);
101 debug("running raw pp2, don't wait for mkws auth");
105 debug("Click on submit button");
106 var click = $("input#mkwsButton").trigger("click");
107 expect(click.length).toBe(1);
114 * This part runs in background. It should be rewritten with
115 * async jasmine functions
118 describe("Check pazpar2 navigation", function () {
120 it("check running search next/prev", function () {
121 expect($("#mkwsPager").length).toBe(1);
123 function my_click(id, time) {
124 setTimeout(function () {
125 debug("trigger click on id: " + id);
126 var click = $(id).trigger("click");
128 debug("next/prev: " + id + " click is success: " + click.length);
129 expect(click.length).toBe(1);
130 }, time * jasmine_config.second);
133 waitsFor(function () {
134 return $("div#mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
135 }, "Expect next link 2", 10 * jasmine_config.second);
138 // click next/prev after N seconds
139 my_click("#mkwsNext", 0);
142 waitsFor(function () {
143 return $("div#mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
144 }, "Expect next link 3", 5 * jasmine_config.second);
147 // click next/prev after N seconds
148 my_click("#mkwsNext", 0);
149 my_click("#mkwsPrev", 0.2);
154 describe("Check pazpar2 hit counter", function () {
155 it("check running search hit counter", function () {
156 var max_time = jasmine_config.max_time; // in seconds
157 var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
160 waitsFor(function () {
161 hits = get_hit_counter();
163 return hits > expected_hits;
164 }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
168 debug("mkws pager found records: '" + hits + "'");
169 expect($("#mkwsPager").length).toBe(1);
170 expect(hits).toBeGreaterThan(expected_hits);
175 describe("Check Termlist", function () {
176 it("found Termlist", function () {
177 var termlist = $("div#mkwsTermlists");
178 debug("Termlist success: " + termlist.length);
179 expect(termlist.length).toBe(1);
181 waitsFor(function () {
182 return $("div#mkwsFacetSources").length == 1 ? true : false;
183 }, "check for facet sources", 2 * jasmine_config.second);
186 // everything displayed?
188 var sources = $("div#mkwsFacetSources");
189 debug("Termlist sources success: " + sources.length);
190 expect(sources.length).toBe(1);
192 var subjects = $("div#mkwsFacetSubjects");
193 expect(subjects.length).toBe(1);
195 var authors = $("div#mkwsFacetAuthors");
196 expect(authors.length).toBe(1);
199 waitsFor(function () {
200 return $("div#mkwsFacetAuthors div.term").length >= 2 ? true : false;
201 }, "At least one author link displayed", 4 * jasmine_config.second);
204 expect($("div#mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
208 it("limit search to first author", function () {
209 var hits_all_targets = get_hit_counter();
210 var author_number = 2; // 2=first author
211 var author_name = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").text();
212 // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
213 // do not click on author names without a comma, e.g.: "Joe Barbara"
214 if (author_name.match(/[0-9].+[0-9]/) || !author_name.match(/,/)) {
218 var click = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
219 debug("limit author click is success: " + click.length);
220 expect(click.length).toBe(1);
222 waitsFor(function () {
223 return get_hit_counter() < hits_all_targets ? true : false;
224 }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
227 var hits_single_target = get_hit_counter();
228 debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
229 expect(hits_all_targets).toBeGreaterThan(hits_single_target);
233 it("limit search to first source", function () {
234 var hits_all_targets = get_hit_counter();
235 var source_number = 2; // 2=first source
236 // do not click on wikipedia link - no author or subject facets possible
237 var terms = $("div#mkwsFacetSources div.term a");
238 for (var i = 0; i < terms.length; i++) {
239 if (terms[i].text.match(/wikipedia/i)) {
240 debug("ignore source facet: " + terms[i].text);
247 var click = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
248 debug("limit source click " + (source_number - 1) + " is success: " + click.length);
249 expect(click.length).toBe(1);
251 waitsFor(function () {
252 if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/Source: /)) {
257 }, "Search for source in navi bar", jasmine_config.second);
259 // Note: it may happens that limited source search returns the same number of hits
260 // as before. Thats not really an error, but unfortunate
261 waitsFor(function () {
262 return get_hit_counter() <= hits_all_targets ? true : false;
263 }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
266 var hits_single_target = get_hit_counter();
267 debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
268 expect(hits_all_targets).not.toBeLessThan(hits_single_target);
273 describe("Show record", function () {
274 var record_number = 1; // the Nth record in hit list
275 it("show record author", function () {
276 var click = $("div#mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
277 debug("show record click is success: " + click.length);
278 expect(click.length).toBe(1);
280 // wait until the record pops up
281 waitsFor(function () {
282 var show = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div");
283 return show != null && show.length ? true : false;
284 }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
287 debug("show record pop up");
288 expect($("div#mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
292 it("extract URL", function () {
293 if (jasmine_config.show_record_url == false) {
294 debug("ignore test for URL in record")
298 var urls = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
299 debug("number of extracted URL from record: " + urls.length);
300 for (var i = 0; i < urls.length; i++) {
301 debug("URL: " + urls[i].href);
302 expect(urls[i].href).not.toBe(null);
303 expect(urls[i].href).toMatch(/^https?:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
304 expect(urls[i].href).toBe(urls[i].text);
309 describe("Check switch menu Records/Targets", function () {
310 it("check mkwsSwitch", function () {
311 expect($("div#mkwsSwitch").length).toBe(1);
313 // expect 2 clickable links
314 expect($("div#mkwsSwitch a").length).toBe(2);
317 it("switch to target view", function () {
318 var click = $("a#mkwsSwitch_targets").trigger("click");
319 debug("target view click is success: " + click.length);
320 expect(click.length).toBe(1);
322 // now the target table must be visible
323 expect($("div#mkwsBytarget").is(":visible")).toBe(true);
324 expect($("div#mkwsRecords").is(":visible")).toBe(false);
326 // wait a half second, to show the target view
327 var time = (new Date).getTime();
328 waitsFor(function () {
329 return (new Date).getTime() - time > 700 ? true : false;
330 }, "wait some miliseconds", 1 * jasmine_config.second);
332 // look for table header
334 expect($("div#mkwsBytarget").html()).toMatch(/Target ID/);
338 it("switch back to record view", function () {
339 var click = $("a#mkwsSwitch_records").trigger("click");
340 debug("record view click is success: " + click.length);
341 expect(click.length).toBe(1);
343 // now the target table must be visible
344 expect($("div#mkwsBytarget").is(":visible")).toBe(false);
345 expect($("div#mkwsRecords").is(":visible")).toBe(true);
349 describe("Check status client counter", function () {
350 function get_time() {
351 var date = new Date();
352 return date.getTime();
354 var time = get_time();
356 it("check status clients", function () {
357 waitsFor(function () {
358 var clients = $("div#mkwsStat span.clients");
359 if (clients.length == 1 && clients.text().match("0/1$")) {
365 }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
369 var clients = $("div#mkwsStat span.clients");
370 debug("span.clients: " + clients.text());
371 expect(clients.text()).toEqual("0/1");
380 describe("All tests are done", function () {
381 it(">>> hooray <<<", function () {});