Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 1 | var chai = require('chai'); |
| 2 | var should = chai.should(); |
| 3 | var assert = chai.assert; |
| 4 | |
| 5 | class KorAPRC { |
| 6 | korap_url = "" |
| 7 | |
| 8 | constructor(korap_url) { |
| 9 | this.korap_url = korap_url |
| 10 | } |
| 11 | |
| 12 | static new(korap_url) { |
| 13 | return new KorAPRC(korap_url) |
| 14 | } |
| 15 | |
| 16 | async login(page, username, password) { |
| 17 | await page.goto(this.korap_url, { waitUntil: 'networkidle0' }); |
| 18 | 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 | |
| 27 | await page.waitForNavigation({ waitUntil: 'networkidle2' }); |
| 28 | 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) |
| 43 | await query_field.type(query) |
| 44 | await page.keyboard.press("Enter") |
| 45 | await page.waitForNavigation(); |
| 46 | const total_results = await page.$("#total-results") |
| 47 | assert.notEqual(total_results, null, "cannot find total results") |
| 48 | const hits = Number(await page.evaluate(total_results => total_results.textContent, total_results)) |
| 49 | return hits |
| 50 | } |
| 51 | |
| 52 | async logout(page) { |
| 53 | const logout_button = await page.$("a[class=logout]") |
| 54 | if (logout_button == null) { |
| 55 | return false |
| 56 | } |
| 57 | await page.click("a[class=logout]") |
| 58 | const username_field = await page.$("body > aside > div > fieldset > form > input[type=text]") |
| 59 | assert.notEqual(username_field, null) |
| 60 | return true |
| 61 | } |
| 62 | |
| 63 | async assure_glimpse_off(page) { |
| 64 | const glimpse = await page.$("input[name=cutoff]") |
| 65 | const glimpse_value = await (await glimpse.getProperty('checked')).jsonValue() |
| 66 | if (glimpse_value) { |
| 67 | await page.click("#glimpse") |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | module.exports = KorAPRC |