Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 1 | const chai = require('chai'); |
| 2 | const assert = chai.assert; |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 3 | |
| 4 | class KorAPRC { |
| 5 | korap_url = "" |
| 6 | |
| 7 | constructor(korap_url) { |
| 8 | this.korap_url = korap_url |
| 9 | } |
| 10 | |
| 11 | static new(korap_url) { |
| 12 | return new KorAPRC(korap_url) |
| 13 | } |
| 14 | |
| 15 | async login(page, username, password) { |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 16 | try { |
| 17 | await page.goto(this.korap_url, { waitUntil: 'domcontentloaded' }); |
| 18 | if (username == "") return false; |
| 19 | if (password == "") return false; |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 20 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 21 | await page.waitForSelector('.dropdown-btn', { visible: true }); |
| 22 | await page.click('.dropdown-btn'); |
| 23 | await page.waitForSelector('input[name=handle_or_email]', { visible: true }); |
| 24 | const username_field = await page.$("input[name=handle_or_email]") |
| 25 | if (username_field != null) { |
| 26 | await username_field.focus(); |
| 27 | await username_field.type(username); |
| 28 | const password_field = await page.$("input[name=pwd]") |
| 29 | await password_field.focus() |
| 30 | await page.keyboard.type(password) |
| 31 | await page.keyboard.press("Enter") |
| 32 | } else { |
| 33 | return false |
| 34 | } |
| 35 | |
| 36 | await page.waitForNavigation({ waitUntil: 'domcontentloaded' }); // Wait for navigation after login |
| 37 | await page.waitForSelector("#q-field", { visible: true }); // Wait for query field to confirm login |
| 38 | const logout = await page.$(".logout") |
| 39 | if (logout == null) { |
| 40 | return false |
| 41 | } |
| 42 | |
| 43 | return true |
| 44 | } catch (error) { |
| 45 | console.error(`Login failed: ${error.message}`); |
| 46 | return false; |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 47 | } |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | async search(page, query) { |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 51 | try { |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 52 | await page.waitForSelector("#q-field", { visible: true }); |
| 53 | const query_field = await page.$("#q-field"); |
| 54 | assert.notEqual(query_field, null, "Query field not found"); |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 55 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 56 | await query_field.click({ clickCount: 3 }); |
| 57 | await page.keyboard.type(query); |
| 58 | await page.keyboard.press("Enter"); |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 59 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 60 | await page.waitForNavigation({ waitUntil: 'domcontentloaded' }); |
| 61 | |
| 62 | // Wait for search results to be fully loaded |
| 63 | try { |
| 64 | await page.waitForSelector('ol li, #resultinfo, .result-item', { |
| 65 | visible: true, |
| 66 | timeout: 15000 |
| 67 | }); |
| 68 | // Give additional time for the results count to be populated |
| 69 | await new Promise(resolve => setTimeout(resolve, 2000)); |
| 70 | } catch (error) { |
| 71 | // Continue if timeout, fallback methods will handle it |
| 72 | } |
| 73 | |
| 74 | const resultsInfo = await page.evaluate(() => { |
| 75 | // Check common selectors for result counts |
| 76 | const selectors = [ |
| 77 | '#total-results', |
| 78 | '#resultinfo', |
| 79 | '.result-count', |
| 80 | '.total-results', |
| 81 | '[data-results]', |
| 82 | '.found' |
| 83 | ]; |
| 84 | |
| 85 | for (const selector of selectors) { |
| 86 | const element = document.querySelector(selector); |
| 87 | if (element) { |
| 88 | const text = element.textContent || element.innerText || ''; |
| 89 | const numbers = text.match(/\d+/g); |
| 90 | if (numbers && numbers.length > 0) { |
| 91 | return { |
| 92 | selector: selector, |
| 93 | numbers: numbers |
| 94 | }; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Look in the page title for results count |
| 100 | const title = document.title; |
| 101 | if (title) { |
| 102 | const numbers = title.match(/\d+/g); |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 103 | if (numbers && numbers.length > 0) { |
| 104 | return { |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 105 | selector: 'title', |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 106 | numbers: numbers |
| 107 | }; |
| 108 | } |
| 109 | } |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 110 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 111 | // Count the actual result items as fallback |
| 112 | const resultItems = document.querySelectorAll('ol li'); |
| 113 | if (resultItems.length > 0) { |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 114 | return { |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 115 | selector: 'counted-items', |
| 116 | numbers: [resultItems.length.toString()] |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 117 | }; |
| 118 | } |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 119 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 120 | return null; |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 121 | }); |
| 122 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 123 | if (!resultsInfo || !resultsInfo.numbers || resultsInfo.numbers.length === 0) { |
| 124 | // Final fallback: just count visible list items |
| 125 | const itemCount = await page.evaluate(() => { |
| 126 | return document.querySelectorAll('ol li').length; |
| 127 | }); |
| 128 | |
| 129 | if (itemCount > 0) { |
| 130 | return itemCount; |
| 131 | } |
| 132 | |
| 133 | throw new Error("Cannot find any results count on the page"); |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 136 | // Extract the largest number found (likely the total results) |
| 137 | const hits = Math.max(...resultsInfo.numbers.map(n => parseInt(n, 10))); |
| 138 | return hits; |
| 139 | } catch (error) { |
| 140 | throw new Error(`Failed to perform search: ${error.message}`); |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 141 | } |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | async logout(page) { |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 145 | try { |
| 146 | // Direct navigation to logout URL - most reliable method |
| 147 | const currentUrl = await page.url(); |
| 148 | const logoutUrl = currentUrl.replace(/\/$/, '') + '/logout'; |
| 149 | |
| 150 | await page.goto(logoutUrl, { waitUntil: 'domcontentloaded', timeout: 10000 }); |
| 151 | |
| 152 | // Navigate back to main page to ensure clean state for subsequent tests |
| 153 | await page.goto(this.korap_url, { waitUntil: 'domcontentloaded', timeout: 10000 }); |
| 154 | |
| 155 | return true; |
| 156 | } catch (error) { |
| 157 | return false; |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 158 | } |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | async assure_glimpse_off(page) { |
| 162 | const glimpse = await page.$("input[name=cutoff]") |
| 163 | const glimpse_value = await (await glimpse.getProperty('checked')).jsonValue() |
| 164 | if (glimpse_value) { |
| 165 | await page.click("#glimpse") |
| 166 | } |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 167 | } |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 168 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 169 | async check_corpus_statistics(page, minTokenThreshold = 1000) { |
| 170 | try { |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 171 | console.log(`Starting corpus statistics check with minTokenThreshold: ${minTokenThreshold}`); |
| 172 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 173 | // Navigate to the corpus view if not already there |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 174 | console.log(`Navigating to: ${this.korap_url}`); |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 175 | await page.goto(this.korap_url, { waitUntil: 'domcontentloaded' }); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 176 | console.log("Navigation completed"); |
| 177 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 178 | // Click the vc-choose element to open corpus selection |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 179 | console.log("Waiting for #vc-choose selector..."); |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 180 | await page.waitForSelector('#vc-choose', { visible: true, timeout: 90000 }); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 181 | console.log("Found #vc-choose, clicking..."); |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 182 | await page.click('#vc-choose'); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 183 | console.log("Clicked #vc-choose"); |
| 184 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 185 | // Wait a moment for the UI to respond |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 186 | console.log("Waiting 1 second for UI to respond..."); |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 187 | await new Promise(resolve => setTimeout(resolve, 1000)); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 188 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 189 | // Click the statistic element |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 190 | console.log("Waiting for .statistic selector..."); |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 191 | await page.waitForSelector('.statistic', { visible: true, timeout: 90000 }); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 192 | console.log("Found .statistic element, attempting to click..."); |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 193 | try { |
| 194 | await page.click('.statistic'); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 195 | console.log("Successfully clicked .statistic element"); |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 196 | } catch (error) { |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 197 | console.error(`Failed to click statistic element: ${error.message}`); |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 198 | throw new Error(`Failed to click statistic element: ${error.message}`); |
| 199 | } |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 200 | |
| 201 | // Wait for statistics to load with a more efficient approach |
| 202 | console.log("Waiting for token statistics to load..."); |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 203 | |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 204 | // First, wait for any dd elements to appear (basic structure) |
| 205 | await page.waitForSelector('dd', { visible: true, timeout: 30000 }); |
| 206 | |
| 207 | // Then wait for the specific token statistics with a simplified check |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 208 | await page.waitForFunction(() => { |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 209 | // Simplified check - look for any dd element with a large number |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 210 | const ddElements = document.querySelectorAll('dd'); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 211 | for (let i = 0; i < ddElements.length; i++) { |
| 212 | const text = ddElements[i].textContent || ddElements[i].innerText || ''; |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 213 | const cleanedText = text.replace(/[,\.]/g, ''); |
| 214 | const numbers = cleanedText.match(/\d+/g); |
| 215 | if (numbers && numbers.length > 0) { |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 216 | const num = parseInt(numbers[0], 10); |
| 217 | if (num > 1000) { // Found a substantial number, likely loaded |
| 218 | return true; |
| 219 | } |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | return false; |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 223 | }, { timeout: 90000, polling: 1000 }); // Poll every second instead of continuously |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 224 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 225 | // Look for the tokens count in a dd element that follows an element with title "tokens" |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 226 | console.log(`Starting token count extraction with minThreshold: ${minTokenThreshold}`); |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 227 | const tokenCount = await page.evaluate((minThreshold) => { |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 228 | // Find the element with title "tokens" first |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 229 | const tokenTitleElements = document.querySelectorAll('[title="tokens"], [title*="token"]'); |
| 230 | |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 231 | for (let i = 0; i < tokenTitleElements.length; i++) { |
| 232 | const element = tokenTitleElements[i]; |
| 233 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 234 | // Look for the next dd element |
| 235 | let nextElement = element.nextElementSibling; |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 236 | let siblingCount = 0; |
| 237 | while (nextElement && siblingCount < 10) { |
| 238 | siblingCount++; |
| 239 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 240 | if (nextElement.tagName.toLowerCase() === 'dd') { |
| 241 | const text = nextElement.textContent || nextElement.innerText || ''; |
| 242 | // Remove number separators (commas and periods) and extract number |
| 243 | const cleanedText = text.replace(/[,\.]/g, ''); |
| 244 | const numbers = cleanedText.match(/\d+/g); |
| 245 | if (numbers && numbers.length > 0) { |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 246 | const tokenValue = parseInt(numbers[0], 10); |
| 247 | return tokenValue; |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | nextElement = nextElement.nextElementSibling; |
| 251 | } |
| 252 | } |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 253 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 254 | // Alternative approach: look for dd elements that contain large numbers |
| 255 | const ddElements = document.querySelectorAll('dd'); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 256 | const candidateTokenCounts = []; |
| 257 | |
| 258 | for (let i = 0; i < ddElements.length; i++) { |
| 259 | const dd = ddElements[i]; |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 260 | const text = dd.textContent || dd.innerText || ''; |
| 261 | // Remove separators and check if it's a large number (likely token count) |
| 262 | const cleanedText = text.replace(/[,\.]/g, ''); |
| 263 | const numbers = cleanedText.match(/\d+/g); |
| 264 | if (numbers && numbers.length > 0) { |
| 265 | const num = parseInt(numbers[0], 10); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 266 | |
| 267 | // Use the provided threshold to filter candidates |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 268 | if (num > minThreshold) { |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 269 | candidateTokenCounts.push({ value: num, text: text, index: i }); |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 273 | |
| 274 | if (candidateTokenCounts.length > 0) { |
| 275 | // Return the largest candidate (most likely to be the total token count) |
| 276 | const bestCandidate = candidateTokenCounts.reduce((max, current) => |
| 277 | current.value > max.value ? current : max |
| 278 | ); |
| 279 | return bestCandidate.value; |
| 280 | } |
| 281 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 282 | return null; |
| 283 | }, minTokenThreshold); |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 284 | |
| 285 | console.log(`Token count extraction completed. Result: ${tokenCount}`); |
| 286 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 287 | if (tokenCount === null) { |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 288 | console.error("ERROR: Token count extraction returned null"); |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 289 | throw new Error("Could not find token count in corpus statistics"); |
| 290 | } |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 291 | |
| 292 | console.log(`SUCCESS: Found token count: ${tokenCount}, threshold was: ${minTokenThreshold}`); |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 293 | return tokenCount; |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 294 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 295 | } catch (error) { |
Marc Kupietz | 669c043 | 2025-07-12 12:33:42 +0200 | [diff] [blame^] | 296 | console.error(`ERROR in check_corpus_statistics: ${error.message}`); |
| 297 | console.error("Full error stack:", error.stack); |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 298 | throw new Error(`Failed to check corpus statistics: ${error.message}`); |
| 299 | } |
Marc Kupietz | 5e45a2f | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame] | 303 | module.exports = KorAPRC |