Marc Kupietz | 490b053 | 2024-09-05 09:36:21 +0200 | [diff] [blame] | 1 | const puppeteer = require('puppeteer-extra'); |
| 2 | puppeteer.use(require('puppeteer-extra-plugin-user-preferences')({ |
| 3 | userPrefs: { |
| 4 | safebrowsing: { |
| 5 | enabled: false, |
| 6 | enhanced: false |
| 7 | } |
| 8 | } |
| 9 | })); |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 10 | const chai = require('chai'); |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 11 | const { afterEach } = require('mocha'); |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 12 | const assert = chai.assert; |
| 13 | const should = chai.should(); |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 14 | var slack = null; |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 15 | |
Marc Kupietz | 0f6c54d | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 16 | const KORAP_URL = process.env.KORAP_URL || "http://localhost:64543"; |
Marc Kupietz | bfb2301 | 2025-06-03 15:47:10 +0200 | [diff] [blame] | 17 | const KORAP_LOGIN = 'KORAP_USERNAME' in process.env ? process.env.KORAP_USERNAME : 'KORAP_LOGIN' in process.env ? process.env.KORAP_LOGIN : "user2" |
| 18 | const KORAP_PWD = process.env.KORAP_PWD || process.env.KORAP_PASSWORD || "password2"; |
Marc Kupietz | 2698238 | 2022-12-04 19:02:57 +0100 | [diff] [blame] | 19 | const KORAP_QUERIES = process.env.KORAP_QUERIES || 'geht, [orth=geht & cmc/pos=VVFIN]' |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 20 | const KORAP_MIN_TOKENS_IN_CORPUS = parseInt(process.env.KORAP_MIN_TOKENS_IN_CORPUS || "100000", 10); |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 21 | const korap_rc = require('../lib/korap_rc.js').new(KORAP_URL) |
| 22 | |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 23 | const slack_webhook = process.env.SLACK_WEBHOOK_URL; |
Marc Kupietz | 4d335a3 | 2024-09-04 16:13:48 +0200 | [diff] [blame] | 24 | |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 25 | if (slack_webhook) { |
| 26 | slack = require('slack-notify')(slack_webhook); |
| 27 | } |
| 28 | |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 29 | function ifConditionIt(title, condition, test) { |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 30 | return condition ? it(title, test) : it.skip(title + " (skipped)", test) |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 31 | } |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 32 | |
Marc Kupietz | 0f6c54d | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 33 | describe('Running KorAP UI end-to-end tests on ' + KORAP_URL, () => { |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 34 | |
| 35 | before(async () => { |
Marc Kupietz | 69e0280 | 2023-11-08 14:37:22 +0100 | [diff] [blame] | 36 | browser = await puppeteer.launch({ |
Marc Kupietz | bfb2301 | 2025-06-03 15:47:10 +0200 | [diff] [blame] | 37 | headless: "shell", |
| 38 | args: [ |
| 39 | '--no-sandbox', |
| 40 | '--disable-setuid-sandbox', |
| 41 | '--disable-dev-shm-usage', |
| 42 | '--disable-accelerated-2d-canvas', |
| 43 | '--no-first-run', |
| 44 | '--no-zygote', |
| 45 | '--disable-gpu' |
| 46 | ] |
Marc Kupietz | 69e0280 | 2023-11-08 14:37:22 +0100 | [diff] [blame] | 47 | }) |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 48 | page = await browser.newPage() |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 49 | await page.setViewport({ |
Marc Kupietz | 9e0f519 | 2025-03-09 12:12:16 +0100 | [diff] [blame] | 50 | width: 1980, |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 51 | height: 768, |
| 52 | deviceScaleFactor: 1, |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 53 | }); |
Marc Kupietz | 4d335a3 | 2024-09-04 16:13:48 +0200 | [diff] [blame] | 54 | console.log("Browser version: " + await browser.version() + " started") |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 55 | }) |
| 56 | |
| 57 | after(async () => { |
| 58 | await browser.close() |
| 59 | }) |
| 60 | |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 61 | afterEach(async function () { |
| 62 | if (this.currentTest.state == "failed") { |
Marc Kupietz | 8f7c204 | 2025-06-24 09:55:03 +0200 | [diff] [blame^] | 63 | const screenshotPath = "failed_" + this.currentTest.title.replaceAll(/[ &\/]/g, "_") + '.png'; |
| 64 | await page.screenshot({ path: screenshotPath }); |
| 65 | |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 66 | if (slack) { |
Marc Kupietz | 8f7c204 | 2025-06-24 09:55:03 +0200 | [diff] [blame^] | 67 | try { |
| 68 | // First send text notification via webhook |
| 69 | slack.alert({ |
| 70 | text: `🚨 Test on ${KORAP_URL} failed: *${this.currentTest.title}*`, |
| 71 | attachments: [{ |
| 72 | color: 'danger', |
| 73 | fields: [{ |
| 74 | title: 'Failed Test', |
| 75 | value: this.currentTest.title, |
| 76 | short: false |
| 77 | }, { |
| 78 | title: 'URL', |
| 79 | value: KORAP_URL, |
| 80 | short: true |
| 81 | }] |
| 82 | }] |
| 83 | }); |
| 84 | } catch (slackError) { |
| 85 | console.error('Failed to send notification to Slack:', slackError.message); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Try to upload screenshot via Slack Web API if token is available |
| 90 | const slackToken = process.env.SLACK_TOKEN; |
| 91 | if (slackToken) { |
| 92 | try { |
| 93 | const { WebClient } = require('@slack/web-api'); |
| 94 | const fs = require('fs'); const web = new WebClient(slackToken); |
| 95 | const channelId = process.env.SLACK_CHANNEL_ID || 'C07CM4JS48H'; |
| 96 | |
| 97 | // Upload the file to Slack |
| 98 | const result = await web.files.uploadV2({ |
| 99 | channel_id: channelId, |
| 100 | file: fs.createReadStream(screenshotPath), |
| 101 | filename: screenshotPath, |
| 102 | title: `Screenshot: ${this.currentTest.title}`, |
| 103 | initial_comment: `📸 Screenshot of failed test: ${this.currentTest.title} on ${KORAP_URL}` |
| 104 | }); |
| 105 | |
| 106 | console.log('Screenshot uploaded to Slack successfully'); |
| 107 | |
| 108 | } catch (uploadError) { |
| 109 | console.error('Failed to upload screenshot to Slack:', uploadError.message); |
| 110 | } |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 111 | } |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 112 | } |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 113 | }) |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 114 | |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 115 | it('KorAP UI is up and running', |
| 116 | (async () => { |
Marc Kupietz | 9e0f519 | 2025-03-09 12:12:16 +0100 | [diff] [blame] | 117 | page.goto(KORAP_URL); |
| 118 | await page.waitForNavigation({ waitUntil: 'networkidle2' }); |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 119 | const query_field = await page.$("#q-field") |
| 120 | assert.isNotNull(query_field, "#q-field not found. Kalamar not running?"); |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 121 | })) |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 122 | |
| 123 | |
| 124 | ifConditionIt('Login into KorAP with incorrect credentials fails', |
| 125 | KORAP_LOGIN != "", |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 126 | (async () => { |
Marc Kupietz | 0f6c54d | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 127 | const login_result = await korap_rc.login(page, KORAP_LOGIN, KORAP_PWD + "*") |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 128 | login_result.should.be.false |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 129 | })) |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 130 | |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 131 | ifConditionIt('Login into KorAP with correct credentials succeeds', |
| 132 | KORAP_LOGIN != "", |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 133 | (async () => { |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 134 | const login_result = await korap_rc.login(page, KORAP_LOGIN, KORAP_PWD) |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 135 | login_result.should.be.true |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 136 | })) |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 137 | |
Marc Kupietz | 0f6c54d | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 138 | it('Can turn glimpse off', |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 139 | (async () => { |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 140 | await korap_rc.assure_glimpse_off(page) |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 141 | })) |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 142 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 143 | it('Corpus statistics show sufficient tokens', |
| 144 | (async () => { |
| 145 | const tokenCount = await korap_rc.check_corpus_statistics(page, KORAP_MIN_TOKENS_IN_CORPUS); |
| 146 | console.log(`Found ${tokenCount} tokens in corpus, minimum required: ${KORAP_MIN_TOKENS_IN_CORPUS}`); |
| 147 | tokenCount.should.be.above(KORAP_MIN_TOKENS_IN_CORPUS - 1, |
| 148 | `Corpus should have at least ${KORAP_MIN_TOKENS_IN_CORPUS} tokens, but found ${tokenCount}`); |
Marc Kupietz | ae9c724 | 2025-06-24 09:54:37 +0200 | [diff] [blame] | 149 | })).timeout(50000) |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 150 | |
Marc Kupietz | 0f6c54d | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 151 | describe('Running searches that should have hits', () => { |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 152 | |
| 153 | before(async () => { await korap_rc.login(page, KORAP_LOGIN, KORAP_PWD) }) |
| 154 | |
Marc Kupietz | 0f6c54d | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 155 | KORAP_QUERIES.split(/[;,] */).forEach((query, i) => { |
| 156 | it('Search for "' + query + '" has hits', |
| 157 | (async () => { |
| 158 | await korap_rc.assure_glimpse_off(page) |
| 159 | const hits = await korap_rc.search(page, query) |
| 160 | hits.should.be.above(0) |
| 161 | })).timeout(20000) |
| 162 | }) |
| 163 | }) |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 164 | |
| 165 | ifConditionIt('Logout works', |
| 166 | KORAP_LOGIN != "", |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 167 | (async () => { |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 168 | const logout_result = await korap_rc.logout(page) |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 169 | logout_result.should.be.true |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 170 | })).timeout(15000) |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 171 | |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 172 | }) |