blob: 3f273861837d2c33facfbe2fb373ec4d0fe752cd [file] [log] [blame]
Marc Kupietzc4077822022-12-03 15:32:40 +01001const { doesNotMatch } = require('assert');
2const { beforeEach } = require('mocha');
3const puppeteer = require('puppeteer')
4var chai = require('chai');
5var should = chai.should();
6var assert = chai.assert;
7const KORAP_URL = process.env.KORAP_URL || "http://localhost:64543";
8const KORAP_LOGIN = process.env.KORAP_LOGIN || "user2";
9const KORAP_PWD = process.env.KORAP_PWD || "password2";
10const KORAP_QUERIES = "geht"
11// const korap_rc = require('../lib/korap_rc.js');
12
13async 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
37async 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
48async 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
60describe('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})