Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame^] | 1 | const { doesNotMatch } = require('assert'); |
| 2 | const { beforeEach } = require('mocha'); |
| 3 | const puppeteer = require('puppeteer') |
| 4 | var chai = require('chai'); |
| 5 | var should = chai.should(); |
| 6 | var assert = chai.assert; |
| 7 | const KORAP_URL = process.env.KORAP_URL || "http://localhost:64543"; |
| 8 | const KORAP_LOGIN = process.env.KORAP_LOGIN || "user2"; |
| 9 | const KORAP_PWD = process.env.KORAP_PWD || "password2"; |
| 10 | const KORAP_QUERIES = "geht" |
| 11 | // const korap_rc = require('../lib/korap_rc.js'); |
| 12 | |
| 13 | async function KorAPlogin(page, username, password) { |
| 14 | await page.goto(KORAP_URL, { waitUntil: 'networkidle0' }); |
| 15 | const username_field = await page.$("body > aside > div > fieldset > form > input[type=text]") |
| 16 | |
| 17 | if (username_field != null) { |
| 18 | await username_field.type(username); |
| 19 | await page.keyboard.press("Tab") |
| 20 | await page.keyboard.type(password) |
| 21 | await page.keyboard.press("Enter") |
| 22 | } |
| 23 | |
| 24 | await page.waitForNavigation({ waitUntil: 'networkidle2' }); |
| 25 | logout = await page.$("body > header > div > a[class=logout]") |
| 26 | if (logout == null) { |
| 27 | return false |
| 28 | } |
| 29 | |
| 30 | let value = await page.evaluate(logout => logout.textContent, logout) |
| 31 | if (!value.match(/(Abmelden|Logout)/)) { |
| 32 | return false |
| 33 | } |
| 34 | return true |
| 35 | } |
| 36 | |
| 37 | async function KorAPlogout(page) { |
| 38 | const logout_button = await page.$("a[class=logout]") |
| 39 | if (logout_button == null) { |
| 40 | return false |
| 41 | } |
| 42 | await page.click("a[class=logout]") |
| 43 | const username_field = await page.$("body > aside > div > fieldset > form > input[type=text]") |
| 44 | assert.notEqual(username_field, null) |
| 45 | return true |
| 46 | } |
| 47 | |
| 48 | async function search(page, query) { |
| 49 | const query_field = await page.$("body > header > form > div > input[name=q]") |
| 50 | assert.notEqual(query_field, null) |
| 51 | await query_field.type(query) |
| 52 | await page.keyboard.press("Enter") |
| 53 | await page.waitForNavigation(); |
| 54 | const total_results = await page.$("#total-results") |
| 55 | assert.notEqual(total_results, null, "cannot find total results") |
| 56 | const hits = Number(await page.evaluate(total_results => total_results.textContent, total_results)) |
| 57 | return hits |
| 58 | } |
| 59 | |
| 60 | describe('Running KorAP UI tests on ' + KORAP_URL, () => { |
| 61 | const screenshot = 'screenshot.png' |
| 62 | |
| 63 | before(async () => { |
| 64 | browser = await puppeteer.launch() |
| 65 | page = await browser.newPage() |
| 66 | }) |
| 67 | |
| 68 | after(async () => { |
| 69 | await browser.close() |
| 70 | }) |
| 71 | |
| 72 | it('Login into KorAP with incorrect credentials fails', |
| 73 | (async () => { |
| 74 | const login_result = await KorAPlogin(page, KORAP_LOGIN, KORAP_PWD + "*") |
| 75 | login_result.should.be.false |
| 76 | })).timeout(10000) |
| 77 | |
| 78 | it('Login into KorAP with correct credentials succeeds', |
| 79 | (async () => { |
| 80 | const login_result = await KorAPlogin(page, KORAP_LOGIN, KORAP_PWD) |
| 81 | login_result.should.be.true |
| 82 | })).timeout(10000) |
| 83 | |
| 84 | const expected_hits = 724 |
| 85 | |
| 86 | it('Search for "' + KORAP_QUERIES + '" has approx. ' + expected_hits + ' hits', |
| 87 | (async () => { |
| 88 | glimpse = await page.$("input[name=cutoff]") |
| 89 | glimpse_value = await (await glimpse.getProperty('checked')).jsonValue() |
| 90 | if (glimpse_value) { |
| 91 | glimpse = await page.$("input[name=cutoff]") |
| 92 | await page.click("#glimpse") |
| 93 | } |
| 94 | const hits = await search(page, KORAP_QUERIES) |
| 95 | await page.screenshot({ path: screenshot }) |
| 96 | hits.should.be.approximately(expected_hits, 10) |
| 97 | })).timeout(20000) |
| 98 | |
| 99 | it('Logout works', |
| 100 | (async () => { |
| 101 | const logout_result = await KorAPlogout(page) |
| 102 | logout_result.should.be.true |
| 103 | })).timeout(10000) |
| 104 | |
| 105 | }) |