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