Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 1 | const chai = require('chai'); |
| 2 | const assert = chai.assert; |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 3 | |
| 4 | class 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 Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 16 | if (this.login == "") return false; |
| 17 | await page.goto(this.korap_url); |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 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") |
Marc Kupietz | e533ca8 | 2022-12-06 07:53:22 +0100 | [diff] [blame] | 25 | } else { |
| 26 | return false |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 29 | await page.waitForNavigation(); |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 30 | const logout = await page.$("body > header > div > a[class=logout]") |
| 31 | if (logout == null) { |
| 32 | return false |
| 33 | } |
| 34 | |
| 35 | let value = await page.evaluate(logout => logout.textContent, logout) |
| 36 | if (!value.match(/(Abmelden|Logout)/)) { |
| 37 | return false |
| 38 | } |
| 39 | return true |
| 40 | } |
| 41 | |
| 42 | async search(page, query) { |
| 43 | const query_field = await page.$("body > header > form > div > input[name=q]") |
| 44 | assert.notEqual(query_field, null) |
Marc Kupietz | 359a77b | 2022-12-04 19:02:57 +0100 | [diff] [blame] | 45 | await query_field.click({ clickCount: 3 }) |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 46 | await query_field.type(query) |
| 47 | await page.keyboard.press("Enter") |
| 48 | await page.waitForNavigation(); |
| 49 | const total_results = await page.$("#total-results") |
| 50 | assert.notEqual(total_results, null, "cannot find total results") |
Marc Kupietz | 514d154 | 2022-12-04 19:02:57 +0100 | [diff] [blame] | 51 | const hits = Number((await page.evaluate(total_results => total_results.textContent, total_results)).replace(/[,.]/g, "")) |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 52 | return hits |
| 53 | } |
| 54 | |
| 55 | async logout(page) { |
| 56 | const logout_button = await page.$("a[class=logout]") |
| 57 | if (logout_button == null) { |
| 58 | return false |
| 59 | } |
| 60 | await page.click("a[class=logout]") |
Marc Kupietz | 57e2442 | 2024-07-13 10:40:52 +0200 | [diff] [blame] | 61 | await page.waitForSelector('article.alertify-log'); |
| 62 | const hasSuccessClass = await page.evaluate(() => { |
| 63 | const article = document.querySelector('article.alertify-log'); |
| 64 | return article.classList.contains('alertify-log-success'); |
| 65 | }); |
| 66 | return hasSuccessClass |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | async assure_glimpse_off(page) { |
| 70 | const glimpse = await page.$("input[name=cutoff]") |
| 71 | const glimpse_value = await (await glimpse.getProperty('checked')).jsonValue() |
| 72 | if (glimpse_value) { |
| 73 | await page.click("#glimpse") |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | module.exports = KorAPRC |