initial import

Change-Id: I6c0ca3860b9edaf30a282a07377f9b3cc0458a0c
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..0da6382
--- /dev/null
+++ b/package.json
@@ -0,0 +1,19 @@
+{
+  "name": "korap-e2e-tests",
+  "version": "0.0.1",
+  "description": "Performs end-to-end tests on a KorAP instance",
+  "main": "mocha",
+  "directories": {
+    "test": "test"
+  },
+  "scripts": {
+    "test": "mocha"
+  },
+  "author": "Marc Kupietz",
+  "license": "BSD-2-Clause",
+  "dependencies": {
+    "mocha": "^10.1.0",
+    "should": "^13.2.3"
+  },
+  "devDependencies": {}
+}
diff --git a/test/korap-ui.js b/test/korap-ui.js
new file mode 100644
index 0000000..3f27386
--- /dev/null
+++ b/test/korap-ui.js
@@ -0,0 +1,105 @@
+const { doesNotMatch } = require('assert');
+const { beforeEach } = require('mocha');
+const puppeteer = require('puppeteer')
+var chai = require('chai');
+var should = chai.should();
+var assert = chai.assert;
+const KORAP_URL =  process.env.KORAP_URL || "http://localhost:64543";
+const KORAP_LOGIN =  process.env.KORAP_LOGIN || "user2";
+const KORAP_PWD = process.env.KORAP_PWD || "password2";
+const KORAP_QUERIES = "geht"
+// const korap_rc = require('../lib/korap_rc.js');
+
+async function KorAPlogin(page, username, password) {
+    await page.goto(KORAP_URL, { waitUntil: 'networkidle0' });
+    const username_field = await page.$("body > aside > div > fieldset > form > input[type=text]")
+
+    if (username_field != null) {
+        await username_field.type(username);
+        await page.keyboard.press("Tab")
+        await page.keyboard.type(password)
+        await page.keyboard.press("Enter")
+    }
+
+    await page.waitForNavigation({ waitUntil: 'networkidle2' });
+    logout = await page.$("body > header > div > a[class=logout]")
+    if (logout == null) {
+        return false
+    }
+
+    let value = await page.evaluate(logout => logout.textContent, logout)
+    if (!value.match(/(Abmelden|Logout)/)) {
+        return false
+    }
+    return true
+}
+
+async function KorAPlogout(page) {
+    const logout_button = await page.$("a[class=logout]")
+    if (logout_button == null) {
+        return false
+    }
+    await page.click("a[class=logout]")
+    const username_field = await page.$("body > aside > div > fieldset > form > input[type=text]")
+    assert.notEqual(username_field, null)
+    return true
+}
+
+async function search(page, query) {
+    const query_field = await page.$("body > header > form > div > input[name=q]")
+    assert.notEqual(query_field, null)
+    await query_field.type(query)
+    await page.keyboard.press("Enter")
+    await page.waitForNavigation();
+    const total_results = await page.$("#total-results")
+    assert.notEqual(total_results, null, "cannot find total results")
+    const hits = Number(await page.evaluate(total_results => total_results.textContent, total_results))
+    return hits
+}
+
+describe('Running KorAP UI tests on ' + KORAP_URL, () => {
+    const screenshot = 'screenshot.png'
+
+    before(async () => {
+        browser = await puppeteer.launch()
+        page = await browser.newPage()
+    })
+
+    after(async () => {
+        await browser.close()
+    })
+
+    it('Login into KorAP with incorrect credentials fails',
+        (async () => {
+            const login_result = await KorAPlogin(page, KORAP_LOGIN, KORAP_PWD + "*")
+            login_result.should.be.false
+        })).timeout(10000)
+
+    it('Login into KorAP with correct credentials succeeds',
+        (async () => {
+            const login_result = await KorAPlogin(page, KORAP_LOGIN, KORAP_PWD)
+            login_result.should.be.true
+        })).timeout(10000)
+
+    const expected_hits = 724
+
+    it('Search for "' + KORAP_QUERIES + '" has approx. ' + expected_hits + ' hits',
+        (async () => {
+            glimpse = await page.$("input[name=cutoff]")
+            glimpse_value = await (await glimpse.getProperty('checked')).jsonValue()
+            if (glimpse_value) {
+                glimpse = await page.$("input[name=cutoff]")
+                await page.click("#glimpse")
+            }
+            const hits = await search(page, KORAP_QUERIES)
+            await page.screenshot({ path: screenshot })
+            hits.should.be.approximately(expected_hits, 10)
+        })).timeout(20000)
+
+    it('Logout works',
+        (async () => {
+            const logout_result = await KorAPlogout(page)
+            logout_result.should.be.true
+        })).timeout(10000)
+
+})
\ No newline at end of file