wait for new records HTML data before we click on a record, MKWS-137
[mkws-moved-to-github.git] / test / spec / mkws-pazpar2.js
1 /* Copyright (c) 2013-2014 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("Jasmine: " + 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 MOTD before search", function () {
79     // Check that the MOTD has been moved into its container, and
80     // is visible before the search.
81     // the mkwsMOTD div was originally inside a testMOTD div, which should
82     // now be emtpy
83     // Note that the testMOTD is a regular div, and uses #testMOTD,
84     // since the automagic class-making does not apply to it.
85     it("MOTD is hidden", function () {
86         expect($(".mkwsMOTD").length).toBe(1);
87         expect($("#testMOTD").length).toBe(1);
88         expect($("#testMOTD").text()).toMatch("^ *$");
89     });
90
91     it("mkwsMOTDContainer has received the text", function () {
92         expect($(".mkwsMOTDContainer").length).toBe(1);
93         expect($(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
94     });
95
96
97 });
98
99 describe("Check pazpar2 search", function () {
100     it("pazpar2 was successfully initialized", function () {
101         expect(mkws_config.error).toBe(undefined);
102     });
103
104     it("validate HTML id's", function () {
105         expect($("input.mkwsQuery").length).toBe(1);
106         expect($("input.mkwsButton").length).toBe(1);
107
108         expect($(".mkwsNext").length).not.toBe(1);
109         expect($(".mkwsPrev").length).not.toBe(1);
110     });
111
112     it("run search query", function () {
113         var search_query = jasmine_config.search_query; // short hit counter with some paging
114         $("input.mkwsQuery").val(search_query);
115         debug("set search query: " + search_query)
116         expect($("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
117
118         if (mkws_config.use_service_proxy) {
119             // wait for service proxy auth
120             waitsFor(function () {
121                 return mkws.authenticated;
122             }, "SP auth done", 10 * jasmine_config.second);
123         } else {
124             debug("running raw pp2, don't wait for mkws auth");
125         }
126
127         runs(function () {
128             debug("Click on submit button");
129             $("input.mkwsButton").trigger("click");
130         })
131     });
132 });
133
134 describe("Check MOTD after search", function () {
135     it("MOTD is hidden", function () {
136         expect($(".mkwsMOTD").length).toBe(1);
137         expect($(".mkwsMOTD").is(":hidden")).toBe(true);
138         //debug("motd " + $(".mkwsMOTD"));
139         debug("motd t=" + $(".mkwsMOTD").text());
140         debug("motd v=" + $(".mkwsMOTD").is(":visible"));
141         //expect($("div.mkwsBytarget").is(":visible")).toBe(true);
142     });
143 });
144
145
146
147 /*
148  * This part runs in background. It should be rewritten with
149  * async jasmine functions
150  *
151  */
152 describe("Check pazpar2 navigation", function () {
153     // Asynchronous part
154     it("check running search next/prev", function () {
155         expect($(".mkwsPager").length).toBe(1);
156
157         function my_click(id, time) {
158             setTimeout(function () {
159                 debug("trigger click on id: " + id);
160                 $(id).trigger("click");
161             }, time * jasmine_config.second);
162         }
163
164         waitsFor(function () {
165             return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
166         }, "Expect next link 2", 10 * jasmine_config.second);
167
168         runs(function () {
169             // click next/prev after N seconds
170             my_click(".mkwsNext", 0);
171         });
172
173         waitsFor(function () {
174             return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
175         }, "Expect next link 3", 5 * jasmine_config.second);
176
177         runs(function () {
178             // click next/prev after N seconds
179             my_click(".mkwsNext", 0);
180             my_click(".mkwsPrev", 0.2);
181         });
182     });
183 });
184
185 describe("Check pazpar2 hit counter", function () {
186     it("check running search hit counter", function () {
187         var max_time = jasmine_config.max_time; // in seconds
188         var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
189         var hits = 0;
190
191         waitsFor(function () {
192             hits = get_hit_counter();
193
194             return hits > expected_hits;
195         }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
196
197
198         runs(function () {
199             debug("mkws pager found records: '" + hits + "'");
200             expect($(".mkwsPager").length).toBe(1);
201             expect(hits).toBeGreaterThan(expected_hits);
202         });
203     });
204 });
205
206 describe("Check Termlist", function () {
207     it("found Termlist", function () {
208         var termlist = $("div.mkwsTermlists");
209         debug("Termlist success: " + termlist.length);
210         expect(termlist.length).toBe(1);
211
212         waitsFor(function () {
213             return $("div.mkwsFacetSources").length == 1 ? true : false;
214         }, "check for facet sources", 4 * jasmine_config.second);
215
216
217         // everything displayed?
218         runs(function () {
219             var sources = $("div.mkwsFacetSources");
220             debug("Termlist sources success: " + sources.length);
221             expect(sources.length).toBe(1);
222
223             var subjects = $("div.mkwsFacetSubjects");
224             expect(subjects.length).toBe(1);
225
226             var authors = $("div.mkwsFacetAuthors");
227             expect(authors.length).toBe(1);
228         });
229
230         waitsFor(function () {
231             return $("div.mkwsFacetAuthors div.term").length >= 2 ? true : false;
232         }, "At least one author link displayed", 4 * jasmine_config.second);
233
234         runs(function () {
235             expect($("div.mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
236         });
237     });
238
239     it("limit search to first author", function () {
240         if (mkws_config.disable_facet_authors_search) {
241             debug("Facets: ignore limit search for authors");
242             return;
243         }
244
245         var hits_all_targets = get_hit_counter();
246         var author_number = 2; // 2=first author
247         // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
248         // do not click on author names without a comma, e.g.: "Joe Barbara"
249         var terms = $("div.mkwsFacetAuthors div.term a");
250         for (var i = 0; i < terms.length; i++) {
251             var term = $(terms[i]).text();
252             if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
253                 debug("ignore author facet: " + term);
254                 author_number++;
255             } else {
256                 break;
257             }
258         }
259         if ($("div.mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").text().length == 0) {
260             debug("No good authors found. Not clicking on the bad ones");
261             return;
262         }
263
264         debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").text());
265         $("div.mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
266
267         waitsFor(function () {
268             return get_hit_counter() < hits_all_targets ? true : false;
269         }, "Limited author search for less than " + hits_all_targets + " hits", 6 * jasmine_config.second);
270
271         runs(function () {
272             var hits_single_target = get_hit_counter();
273             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
274             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
275         });
276     });
277
278     it("limit search to first source", function () {
279         var hits_all_targets = get_hit_counter();
280         var source_number = 2; // 2=first source
281         // do not click on wikipedia link - no author or subject facets possible
282         var terms = $("div.mkwsFacetSources div.term a");
283         for (var i = 0; i < terms.length; i++) {
284             var term = $(terms[i]).text();
285             if (term.match(/wikipedia/i)) {
286                 debug("ignore source facet: " + term);
287                 source_number++;
288             } else {
289                 break;
290             }
291         }
292         if ($("div.mkwsFacetSources div.term:nth-child(" + source_number + ") a").text().length == 0) {
293             debug("No good source found. Not clicking on the bad ones");
294             return;
295         }
296
297         $("div.mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
298
299         waitsFor(function () {
300             if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
301                 return true;
302             } else {
303                 return false;
304             }
305         }, "Search for source in navi bar", 4 * jasmine_config.second);
306
307         // Note: it may happens that limited source search returns the same number of hits
308         // as before. Thats not really an error, but unfortunate
309         waitsFor(function () {
310             return get_hit_counter() <= hits_all_targets ? true : false;
311         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
312
313         runs(function () {
314             var hits_single_target = get_hit_counter();
315             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
316             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
317         });
318     });
319 });
320
321
322 describe("Check record list", function () {
323     it("got a record", function () {
324         var waitcount = 0;
325
326         // wait for new records
327         $("div.mkwsRecords").bind("DOMSubtreeModified", function () {
328             waitcount++;
329             debug("DOM div.mkwsRecords changed");
330         });
331
332         waitsFor(function () {
333             return waitcount;
334         }, "wait until we see a record", 1.5 * jasmine_config.second);
335
336         runs(function () {
337             expect(waitcount).toBeGreaterThan(0);
338             $("div.mkwsRecords").unbind("DOMSubtreeModified");
339         });
340
341     });
342 });
343
344
345 describe("Show record", function () {
346     var record_number = 1; // the Nth record in hit list
347     it("show record author", function () {
348         var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
349         debug("show record click is success: " + click.length);
350         expect(click.length).toBe(1);
351
352         // wait until the record pops up
353         waitsFor(function () {
354             var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div");
355             return show != null && show.length ? true : false;
356         }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
357
358         runs(function () {
359             debug("show record pop up");
360             expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
361         });
362     });
363
364     it("extract URL", function () {
365         if (jasmine_config.show_record_url == false) {
366             debug("ignore test for URL in record")
367             return;
368         }
369
370         var urls = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
371         debug("number of extracted URL from record: " + urls.length);
372         // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
373         for (var i = 0; i < urls.length; i++) {
374             var url = $(urls[i]);
375             debug("URL: " + url.attr('href') + " text: " + url.text());
376
377             expect(url.attr('href')).not.toBe(null);
378             expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
379             expect(url.text()).not.toBe("");
380         }
381     });
382 });
383
384 describe("Check switch menu Records/Targets", function () {
385     it("check mkwsSwitch", function () {
386         expect($("div.mkwsSwitch").length).toBe(1);
387
388         // expect 2 clickable links
389         expect($("div.mkwsSwitch a").length).toBe(2);
390     });
391
392     it("switch to target view", function () {
393         $("div.mkwsSwitch").children('a').eq(1).trigger("click");
394
395         // now the target table must be visible
396         expect($("div.mkwsBytarget").is(":visible")).toBe(true);
397         expect($("div.mkwsRecords").is(":visible")).toBe(false);
398
399         // wait a half second, to show the target view
400         var time = (new Date).getTime();
401         waitsFor(function () {
402             return (new Date).getTime() - time > 700 ? true : false;
403         }, "wait some miliseconds", 1 * jasmine_config.second);
404
405         // look for table header
406         runs(function () {
407             expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
408         });
409     });
410
411     it("switch back to record view", function () {
412         $("div.mkwsSwitch").children('a').eq(0).trigger("click");
413
414         // now the target table must be visible
415         expect($("div.mkwsBytarget").is(":visible")).toBe(false);
416         expect($("div.mkwsRecords").is(":visible")).toBe(true);
417     });
418 });
419
420 describe("Check status client counter", function () {
421     function get_time() {
422         var date = new Date();
423         return date.getTime();
424     }
425     var time = get_time();
426
427     it("check status clients", function () {
428         waitsFor(function () {
429             var clients = $("div#mkwsStat span.clients");
430             if (clients.length == 1 && clients.text().match("0/1$")) {
431                 return true;
432             } else {
433                 return false;
434             }
435
436         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
437
438 /*
439         runs(function () {
440             var clients = $("div#mkwsStat span.clients");
441             debug("span.clients: " + clients.text());
442             expect(clients.text()).toEqual("0/1");
443         });
444         */
445
446     });
447
448 });
449
450 /* done */
451 describe("All tests are done", function () {
452     it(">>> hooray <<<", function () {
453         mkws.jasmine_done = true;
454     });
455 });