blob: 409e39eaf45239ad6d2eb288dfaab54c3cab2fc1 [file] [log] [blame]
Marc Kupietz55fc3162022-12-04 16:25:49 +01001const chai = require('chai');
2const assert = chai.assert;
Marc Kupietz5e45a2f2022-12-03 15:32:40 +01003
4class KorAPRC {
5 korap_url = ""
6
7 constructor(korap_url) {
8 this.korap_url = korap_url
9 }
10
11 static new(korap_url) {
12 return new KorAPRC(korap_url)
13 }
14
15 async login(page, username, password) {
Marc Kupietz55fc3162022-12-04 16:25:49 +010016 if (this.login == "") return false;
17 await page.goto(this.korap_url);
Marc Kupietz5e45a2f2022-12-03 15:32:40 +010018 const username_field = await page.$("body > aside > div > fieldset > form > input[type=text]")
19
20 if (username_field != null) {
21 await username_field.type(username);
22 await page.keyboard.press("Tab")
23 await page.keyboard.type(password)
24 await page.keyboard.press("Enter")
25 }
26
Marc Kupietz55fc3162022-12-04 16:25:49 +010027 await page.waitForNavigation();
Marc Kupietz5e45a2f2022-12-03 15:32:40 +010028 const logout = await page.$("body > header > div > a[class=logout]")
29 if (logout == null) {
30 return false
31 }
32
33 let value = await page.evaluate(logout => logout.textContent, logout)
34 if (!value.match(/(Abmelden|Logout)/)) {
35 return false
36 }
37 return true
38 }
39
40 async search(page, query) {
41 const query_field = await page.$("body > header > form > div > input[name=q]")
42 assert.notEqual(query_field, null)
Marc Kupietz359a77b2022-12-04 19:02:57 +010043 await query_field.click({ clickCount: 3 })
Marc Kupietz5e45a2f2022-12-03 15:32:40 +010044 await query_field.type(query)
45 await page.keyboard.press("Enter")
46 await page.waitForNavigation();
47 const total_results = await page.$("#total-results")
48 assert.notEqual(total_results, null, "cannot find total results")
Marc Kupietz514d1542022-12-04 19:02:57 +010049 const hits = Number((await page.evaluate(total_results => total_results.textContent, total_results)).replace(/[,.]/g, ""))
Marc Kupietz5e45a2f2022-12-03 15:32:40 +010050 return hits
51 }
52
53 async logout(page) {
54 const logout_button = await page.$("a[class=logout]")
55 if (logout_button == null) {
56 return false
57 }
58 await page.click("a[class=logout]")
59 const username_field = await page.$("body > aside > div > fieldset > form > input[type=text]")
60 assert.notEqual(username_field, null)
61 return true
62 }
63
64 async assure_glimpse_off(page) {
65 const glimpse = await page.$("input[name=cutoff]")
66 const glimpse_value = await (await glimpse.getProperty('checked')).jsonValue()
67 if (glimpse_value) {
68 await page.click("#glimpse")
69 }
70
71 }
72}
73
74module.exports = KorAPRC