put korap rc function into a class

Change-Id: Ib0c83611bb153af29fcb8286862ea4c7cf94f64e
diff --git a/lib/korap_rc.js b/lib/korap_rc.js
new file mode 100644
index 0000000..c5356b8
--- /dev/null
+++ b/lib/korap_rc.js
@@ -0,0 +1,73 @@
+var chai = require('chai');
+var should = chai.should();
+var assert = chai.assert;
+
+class KorAPRC {
+    korap_url = ""
+
+    constructor(korap_url) {
+        this.korap_url = korap_url
+    }
+
+    static new(korap_url) {
+        return new KorAPRC(korap_url)
+    }
+
+    async login(page, username, password) {
+        await page.goto(this.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' });
+        const 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 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
+    }
+
+    async logout(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 assure_glimpse_off(page) {
+        const glimpse = await page.$("input[name=cutoff]")
+        const glimpse_value = await (await glimpse.getProperty('checked')).jsonValue()
+        if (glimpse_value) {
+            await page.click("#glimpse")
+        }
+
+    }
+}
+
+module.exports = KorAPRC
diff --git a/package.json b/package.json
index 0da6382..63c30e4 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,8 @@
   "description": "Performs end-to-end tests on a KorAP instance",
   "main": "mocha",
   "directories": {
-    "test": "test"
+    "test": "test",
+    "lib": "lib"
   },
   "scripts": {
     "test": "mocha"
@@ -12,6 +13,7 @@
   "author": "Marc Kupietz",
   "license": "BSD-2-Clause",
   "dependencies": {
+    "KorAPRC": "file:./lib/korap_rc.ts",
     "mocha": "^10.1.0",
     "should": "^13.2.3"
   },
diff --git a/test/korap-ui.js b/test/korap-ui.js
index 3f27386..8d5a819 100644
--- a/test/korap-ui.js
+++ b/test/korap-ui.js
@@ -8,54 +8,7 @@
 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
-}
+const korap_rc = require('../lib/korap_rc.js').new(KORAP_URL);
 
 describe('Running KorAP UI tests on ' + KORAP_URL, () => {
     const screenshot = 'screenshot.png'
@@ -71,13 +24,13 @@
 
     it('Login into KorAP with incorrect credentials fails',
         (async () => {
-            const login_result = await KorAPlogin(page, KORAP_LOGIN, KORAP_PWD + "*")
+            const login_result = await  korap_rc.login(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)
+            const login_result = await korap_rc.login(page, KORAP_LOGIN, KORAP_PWD)
             login_result.should.be.true
         })).timeout(10000)
 
@@ -85,20 +38,15 @@
 
     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 korap_rc.assure_glimpse_off(page)
+            const hits = await korap_rc.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)
+            const logout_result = await korap_rc.logout(page)
             logout_result.should.be.true
         })).timeout(10000)