indent
[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 var get_hit_counter = function () {
13         // not yet here
14         if ($("#mkwsPager").length == 0) return -1;
15
16         var found = $("#mkwsPager").text();
17         var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
18         re.exec(found);
19         var hits = -1;
20
21         if (RegExp.$1) {
22             hits = parseInt(RegExp.$1);
23             expect(hits).toBeGreaterThan(0);
24         }
25
26         //debug("Hits: " + hits);
27         return hits;
28     }
29
30 describe("Check pazpar2 search", function () {
31     it("pazpar2 was successfully initialize", function () {
32         expect(mkws_config.error).toBe(undefined);
33     });
34
35     it("validate HTML id's", function () {
36         expect($("input#mkwsQuery").length).toBe(1);
37         expect($("input#mkwsButton").length).toBe(1);
38
39         expect($("#mkwsNext").length).not.toBe(1);
40         expect($("#mkwsPrev").length).not.toBe(1);
41     });
42
43     it("run search query", function () {
44         var search_query = "freebsd"; // short hit counter with some paging
45         $("input#mkwsQuery").val(search_query);
46         debug("set search query: " + search_query)
47         expect($("input#mkwsQuery").val()).toMatch("^" + search_query + "$");
48
49         if (mkws_config.use_service_proxy) {
50             // wait for service proxy auth
51             waitsFor(function () {
52                 return mkws.authenticated;
53             }, "SP auth done", 10 * 1000);
54         } else {
55             debug("running raw pp2, don't wait for mkws auth");
56         }
57
58         runs(function () {
59             debug("Click on submit button");
60             var click = $("input#mkwsButton").trigger("click");
61             expect(click.length).toBe(1);
62         })
63     });
64 });
65
66
67 /*
68  * This part runs in background. It should be rewritten with
69  * async jasmine functions
70  *
71  */
72 describe("Check pazpar2 navigation", function () {
73     // Asynchronous part
74     it("check running search next/prev", function () {
75         expect($("#mkwsPager").length).toBe(1);
76
77         function my_click(id, time) {
78             setTimeout(function () {
79                 debug("trigger click on id: " + id);
80                 var click = $(id).trigger("click");
81
82                 debug("next/prev: " + id + " click is success: " + click.length);
83                 expect(click.length).toBe(1);
84             }, time * 1000);
85         }
86
87         waitsFor(function () {
88             return $("div#mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
89         }, "Expect next link 2", 10 * 1000);
90
91         runs(function () {
92             // click next/prev after N seconds
93             my_click("#mkwsNext", 0);
94         });
95
96         waitsFor(function () {
97             return $("div#mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
98         }, "Expect next link 3", 5 * 1000);
99
100         runs(function () {
101             // click next/prev after N seconds
102             my_click("#mkwsNext", 0);
103             my_click("#mkwsPrev", 0.2);
104         });
105     });
106 });
107
108 describe("Check pazpar2 hit counter", function () {
109     it("check running search hit counter", function () {
110         var max_time = 16; // in seconds
111         var expected_hits = 80; // at least expected hit counter
112         var hits = 0;
113
114         waitsFor(function () {
115             hits = get_hit_counter();
116
117             return hits > expected_hits;
118         }, "Expect " + expected_hits + " hits", max_time * 1000);
119
120
121         runs(function () {
122             debug("mkws pager found records: '" + hits + "'");
123             expect($("#mkwsPager").length).toBe(1);
124             expect(hits).toBeGreaterThan(expected_hits);
125         });
126     });
127 });
128
129 describe("Check Termlist", function () {
130     it("found Termlist", function () {
131         var termlist = $("div#mkwsTermlists");
132         debug("Termlist success: " + termlist.length);
133         expect(termlist.length).toBe(1);
134
135         waitsFor(function () {
136             return $("div#mkwsFacetSources").length == 1 ? true : false;
137         }, "check for facet sources", 2 * 1000);
138
139
140         // everything displayed?
141         runs(function () {
142             var sources = $("div#mkwsFacetSources");
143             debug("Termlist sources success: " + sources.length);
144             expect(sources.length).toBe(1);
145
146             var subjects = $("div#mkwsFacetSubjects");
147             expect(subjects.length).toBe(1);
148
149             var authors = $("div#mkwsFacetAuthors");
150             expect(authors.length).toBe(1);
151         });
152
153         waitsFor(function () {
154             return $("div#mkwsFacetAuthors div.term").length >= 2 ? true : false;
155         }, "At least one author link displayed", 2 * 1000);
156
157         runs(function () {
158             expect($("div#mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
159         });
160     });
161
162     it("limit search to first author", function () {
163         var hits_all_targets = get_hit_counter();
164         var author_number = 2; // 2=first author
165         var author_name = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").text();
166         // do not click on author with numbers, e.g.: Bower, James M. Beeman, David, 1938-
167         if (author_name.match(/[0-9].+[0-9]/)) {
168             author_number++;
169         }
170
171         var click = $("div#mkwsFacetAuthors div.term:nth-child(" + author_number + ") a").trigger("click");
172         debug("limit author click is success: " + click.length);
173         expect(click.length).toBe(1);
174
175         waitsFor(function () {
176             return get_hit_counter() < hits_all_targets ? true : false;
177         }, "Limited author search for less than " + hits_all_targets + " hits", 6 * 1000);
178
179         runs(function () {
180             var hits_single_target = get_hit_counter();
181             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
182             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
183         });
184     });
185
186     it("limit search to first source", function () {
187         var hits_all_targets = get_hit_counter();
188         var source_number = 2; // 2=first source
189         var source_name = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").text();
190         // do not click on wikipedia link - no author or subject facets possible
191         if (source_name.match(/wikipedia/i)) {
192             source_number++;
193         }
194
195         var click = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
196         debug("limit source click " + (source_number - 1) + " is success: " + click.length);
197         expect(click.length).toBe(1);
198
199         waitsFor(function () {
200             if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/Source: /)) {
201                 return true;
202             } else {
203                 return false;
204             }
205         }, "Search for source in navi bar", 1000);
206
207         // Note: it may happens that limited source search returns the same number of hits
208         // as before. Thats not really an error, but unfortunate
209         waitsFor(function () {
210             return get_hit_counter() <= hits_all_targets ? true : false;
211         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * 1000);
212
213         runs(function () {
214             var hits_single_target = get_hit_counter();
215             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
216             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
217         });
218     });
219 });
220
221 describe("Show record", function () {
222     var record_number = 1; // the Nth record in hit list
223     it("show record author", function () {
224         var click = $("div#mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
225         debug("show record click is success: " + click.length);
226         expect(click.length).toBe(1);
227
228         // wait until the record pops up
229         waitsFor(function () {
230             var show = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div");
231             return show != null && show.length ? true : false;
232         }, "wait some miliseconds to show up a record", 2 * 1000);
233
234         runs(function () {
235             debug("show record pop up");
236             expect($("div#mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
237         });
238     });
239
240     it("extract URL", function () {
241         if (mkws_config.jasmine && mkws_config.jasmine.show_record_url == false) {
242             debug("ignore test for URL in record")
243             return;
244         }
245
246         var url = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a").text();
247         debug("extracted URL from record: " + url);
248
249         expect(url).not.toBe(null);
250         expect(url).toMatch(/^http:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
251     });
252 });
253
254 describe("Check switch menu Records/Targets", function () {
255     it("check mkwsSwitch", function () {
256         expect($("div#mkwsSwitch").length).toBe(1);
257
258         // expect 2 clickable links
259         expect($("div#mkwsSwitch a").length).toBe(2);
260     });
261
262     it("switch to target view", function () {
263         var click = $("a#mkwsSwitch_targets").trigger("click");
264         debug("target view click is success: " + click.length);
265         expect(click.length).toBe(1);
266
267         // now the target table must be visible
268         expect($("div#mkwsBytarget").is(":visible")).toBe(true);
269         expect($("div#mkwsRecords").is(":visible")).toBe(false);
270
271         // wait a half second, to show the target view
272         var time = (new Date).getTime();
273         waitsFor(function () {
274             return (new Date).getTime() - time > 700 ? true : false;
275         }, "wait some miliseconds", 1 * 1000);
276
277         // look for table header
278         runs(function () {
279             expect($("div#mkwsBytarget").html()).toMatch(/Target ID/);
280         });
281     });
282
283     it("switch back to record view", function () {
284         var click = $("a#mkwsSwitch_records").trigger("click");
285         debug("record view click is success: " + click.length);
286         expect(click.length).toBe(1);
287
288         // now the target table must be visible
289         expect($("div#mkwsBytarget").is(":visible")).toBe(false);
290         expect($("div#mkwsRecords").is(":visible")).toBe(true);
291     });
292 });
293
294 describe("Check status client counter", function () {
295     function get_time() {
296         var date = new Date();
297         return date.getTime();
298     }
299     var time = get_time();
300
301     it("check status clients", function () {
302         waitsFor(function () {
303             var clients = $("div#mkwsStat span.clients");
304             if (clients.length == 1 && clients.text().match("0/1$")) {
305                 return true;
306             } else {
307                 return false;
308             }
309
310         }, "wait for Active clients: 0/1", 4 * 1000);
311
312 /*
313         runs(function () {
314             var clients = $("div#mkwsStat span.clients");
315             debug("span.clients: " + clients.text());
316             expect(clients.text()).toEqual("0/1");
317         });
318         */
319
320     });
321
322 });
323
324 /* dummy EOF */
325 describe("All tests are done", function () {
326     it(">>> hooray <<<", function () {});
327 });