Use child-counting to find Targets and Records links.
[mkws-moved-to-github.git] / test / spec / mkws-pazpar2.js
1 /* Copyright (c) 2013 IndexData ApS. http://indexdata.com
2  *
3  * perform papzpar2 / pz2.js search & retrieve request in the browser
4  *
5  */
6
7 // get references from mkws.js, lazy evaluation
8 var debug = function (text) {
9         mkws.debug_function(text)
10     }
11
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 = {};
15 }
16
17 /* check config for jasmine test
18  *
19  * you can override the default values in the config
20  * object: jasmine_config = {};
21  *
22  */
23 function init_jasmine_config() {
24
25     var jasmine_config_default = {
26         search_query: "freebsd",
27         max_time: 16,
28         // in seconds
29         expected_hits: 80,
30         // at least expected hit counter
31         second: 1000,
32         // miliseconds to seconds
33         show_record_url: true,
34         // check for valid URL in records
35         dummy: false
36     };
37
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];
42         }
43         debug("jasmine config: " + key + " => " + jasmine_config[key]);
44     }
45
46     mkws.jasmine_done = false;
47 }
48
49 var get_hit_counter = function () {
50         // not yet here
51         if ($("#mkwsPager").length == 0) return -1;
52
53         var found = $("#mkwsPager").text();
54         var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
55         re.exec(found);
56         var hits = -1;
57
58         if (RegExp.$1) {
59             hits = parseInt(RegExp.$1);
60             expect(hits).toBeGreaterThan(0);
61         }
62
63         //debug("Hits: " + hits);
64         return hits;
65     }
66
67 describe("Init jasmine config", function () {
68     it("jasmine was successfully initialized", function () {
69         init_jasmine_config();
70
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);
75     });
76 });
77
78 describe("Check pazpar2 search", function () {
79     it("pazpar2 was successfully initialize", function () {
80         expect(mkws_config.error).toBe(undefined);
81     });
82
83     it("validate HTML id's", function () {
84         expect($("input#mkwsQuery").length).toBe(1);
85         expect($("input#mkwsButton").length).toBe(1);
86
87         expect($("#mkwsNext").length).not.toBe(1);
88         expect($("#mkwsPrev").length).not.toBe(1);
89     });
90
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 + "$");
96
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);
102         } else {
103             debug("running raw pp2, don't wait for mkws auth");
104         }
105
106         runs(function () {
107             debug("Click on submit button");
108             var click = $("input#mkwsButton").trigger("click");
109             expect(click.length).toBe(1);
110         })
111     });
112 });
113
114
115 /*
116  * This part runs in background. It should be rewritten with
117  * async jasmine functions
118  *
119  */
120 describe("Check pazpar2 navigation", function () {
121     // Asynchronous part
122     it("check running search next/prev", function () {
123         expect($("#mkwsPager").length).toBe(1);
124
125         function my_click(id, time) {
126             setTimeout(function () {
127                 debug("trigger click on id: " + id);
128                 var click = $(id).trigger("click");
129
130                 debug("next/prev: " + id + " click is success: " + click.length);
131                 expect(click.length).toBe(1);
132             }, time * jasmine_config.second);
133         }
134
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);
138
139         runs(function () {
140             // click next/prev after N seconds
141             my_click("#mkwsNext", 0);
142         });
143
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);
147
148         runs(function () {
149             // click next/prev after N seconds
150             my_click("#mkwsNext", 0);
151             my_click("#mkwsPrev", 0.2);
152         });
153     });
154 });
155
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
160         var hits = 0;
161
162         waitsFor(function () {
163             hits = get_hit_counter();
164
165             return hits > expected_hits;
166         }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
167
168
169         runs(function () {
170             debug("mkws pager found records: '" + hits + "'");
171             expect($("#mkwsPager").length).toBe(1);
172             expect(hits).toBeGreaterThan(expected_hits);
173         });
174     });
175 });
176
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);
182
183         waitsFor(function () {
184             return $("div#mkwsFacetSources").length == 1 ? true : false;
185         }, "check for facet sources", 4 * jasmine_config.second);
186
187
188         // everything displayed?
189         runs(function () {
190             var sources = $("div#mkwsFacetSources");
191             debug("Termlist sources success: " + sources.length);
192             expect(sources.length).toBe(1);
193
194             var subjects = $("div#mkwsFacetSubjects");
195             expect(subjects.length).toBe(1);
196
197             var authors = $("div#mkwsFacetAuthors");
198             expect(authors.length).toBe(1);
199         });
200
201         waitsFor(function () {
202             return $("div#mkwsFacetAuthors div.term").length >= 2 ? true : false;
203         }, "At least one author link displayed", 4 * jasmine_config.second);
204
205         runs(function () {
206             expect($("div#mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
207         });
208     });
209
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);
220                 author_number++;
221             } else {
222                 break;
223             }
224         }
225
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);
229
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);
233
234         runs(function () {
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);
238         });
239     });
240
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);
250                 source_number++;
251             } else {
252                 break;
253             }
254         }
255
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);
259
260         waitsFor(function () {
261             if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/Source: /)) {
262                 return true;
263             } else {
264                 return false;
265             }
266         }, "Search for source in navi bar", 4 * jasmine_config.second);
267
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);
273
274         runs(function () {
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);
278         });
279     });
280 });
281
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);
288
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);
294
295         runs(function () {
296             debug("show record pop up");
297             expect($("div#mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
298         });
299     });
300
301     it("extract URL", function () {
302         if (jasmine_config.show_record_url == false) {
303             debug("ignore test for URL in record")
304             return;
305         }
306
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());
315         }
316     });
317 });
318
319 describe("Check switch menu Records/Targets", function () {
320     it("check mkwsSwitch", function () {
321         expect($("div.mkwsSwitch").length).toBe(1);
322
323         // expect 2 clickable links
324         expect($("div.mkwsSwitch a").length).toBe(2);
325     });
326
327     it("switch to target view", function () {
328         var click = $("div.mkwsSwitch").children('a').eq(1).trigger("click");
329         debug("target view click is success: " + click.length);
330         expect(click.length).toBe(1);
331
332         // now the target table must be visible
333         expect($("div.mkwsBytarget").is(":visible")).toBe(true);
334         expect($("div#mkwsRecords").is(":visible")).toBe(false);
335
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);
341
342         // look for table header
343         runs(function () {
344             expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
345         });
346     });
347
348     it("switch back to record view", function () {
349         var click = $("div.mkwsSwitch").children('a').eq(0).trigger("click");
350         debug("record view click is success: " + click.length);
351         expect(click.length).toBe(1);
352
353         // now the target table must be visible
354         expect($("div#mkwsBytarget").is(":visible")).toBe(false);
355         expect($("div#mkwsRecords").is(":visible")).toBe(true);
356     });
357 });
358
359 describe("Check status client counter", function () {
360     function get_time() {
361         var date = new Date();
362         return date.getTime();
363     }
364     var time = get_time();
365
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$")) {
370                 return true;
371             } else {
372                 return false;
373             }
374
375         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
376
377 /*
378         runs(function () {
379             var clients = $("div#mkwsStat span.clients");
380             debug("span.clients: " + clients.text());
381             expect(clients.text()).toEqual("0/1");
382         });
383         */
384
385     });
386
387 });
388
389 /* done */
390 describe("All tests are done", function () {
391     it(">>> hooray <<<", function () {
392         mkws.jasmine_done = true;
393     });
394 });