|
1 | | -const { log, args, createPage, closePage, takeScreenshot, waitForServerReady, dismissDevmode } = require('./test-utils'); |
| 1 | +const { chromium, webkit } = require('playwright'); |
| 2 | +const screenshots = "screenshots.out" |
| 3 | + |
| 4 | +let headless = false, host = 'localhost', port = '8080', mode = 'prod', name; |
| 5 | +process.argv.forEach(a => { |
| 6 | + if (/^--headless/.test(a)) { |
| 7 | + headless = true; |
| 8 | + } else if (/^--port=/.test(a)) { |
| 9 | + port = a.split('=')[1]; |
| 10 | + } else if (/^--mode=/.test(a)) { |
| 11 | + mode = a.split('=')[1]; |
| 12 | + } else if (/^--name=/.test(a)) { |
| 13 | + name = a.split('=')[1]; |
| 14 | + } |
| 15 | +}); |
| 16 | + |
| 17 | +let sscount = 0; |
| 18 | +async function takeScreenshot(page, name) { |
| 19 | + const path = `${screenshots}/${++sscount}-${name}-${mode}.png`; |
| 20 | + await page.screenshot({ path }); |
| 21 | + log(`Screenshot taken: ${path}\n`); |
| 22 | +} |
| 23 | +const log = s => process.stderr.write(`\x1b[1m=> TEST: \x1b[0;33m${s}\x1b[0m`); |
2 | 24 |
|
3 | 25 | (async () => { |
4 | | - const arg = args(); |
5 | | - |
6 | | - const page = await createPage(arg.headless); |
7 | | - |
8 | | - // Wait for server and vaadin ready |
9 | | - await waitForServerReady(page, arg.url, {selector: '#outlet > * > *:not(style):not(script)'}); |
10 | | - |
11 | | - // Dismiss dev mode notification if present |
12 | | - await dismissDevmode(page); |
13 | | - |
14 | | - const text = 'Greet'; |
15 | | - |
16 | | - await takeScreenshot(page, __filename, 'initial-view'); |
17 | | - |
18 | | - // Click input[type="text"] |
19 | | - try { |
20 | | - await page.locator('input[type="text"]').click({timeout:10000}); |
21 | | - } catch (error) { |
22 | | - // skeleton-starter-flow-cdi wildfly:run sometimes does not load the page correctly |
23 | | - log(`Error looking for input[type="text"], sleeping and reloading page`); |
24 | | - await page.reload(); |
25 | | - await page.waitForLoadState('load') |
26 | | - await page.waitForTimeout(10000); |
27 | | - await takeScreenshot(page, __filename, 'initial-view-after-reload'); |
28 | | - await page.locator('input[type="text"]').click({timeout:60000}); |
29 | | - } |
30 | | - |
31 | | - // Fill input[type="text"] |
32 | | - await page.locator('input[type="text"]').fill(text); |
33 | | - await takeScreenshot(page, __filename, 'input-filled'); |
34 | | - |
35 | | - // Click text=Say hello |
36 | | - await page.locator('vaadin-button').click(); |
37 | | - await takeScreenshot(page, __filename, 'button-clicked'); |
38 | | - |
39 | | - // Look for the text, sometimes rendered in an alert, sometimes in the dom |
40 | | - let m; |
41 | | - try { |
42 | | - m = await page.getByRole('alert').nth(1).innerText({timeout:60}); |
43 | | - log(`Found ${text} in an 'alert' role: ${m}`); |
44 | | - } catch (e) { |
45 | | - log(`Not Found ${text} in an 'alert' role, looking in DOM`); |
46 | | - m = await page.locator(`text=/${text}/`).first().innerText({timeout:5000}); |
47 | | - } |
48 | | - |
49 | | - if (! new RegExp(text).test(m)) { |
50 | | - throw new Error(`${text} text not found in ${m}`); |
51 | | - } |
52 | | - |
53 | | - log(`Found ${m} text in the dom`); |
54 | | - await takeScreenshot(page, __filename, 'greeting-found'); |
55 | | - |
56 | | - await closePage(page); |
| 26 | + const browser = await chromium.launch({ |
| 27 | + headless: headless, |
| 28 | + chromiumSandbox: false |
| 29 | + }); |
| 30 | + const context = await browser.newContext(); |
| 31 | + const text = 'Greet'; |
| 32 | + |
| 33 | + // Open new page |
| 34 | + const page = await context.newPage(); |
| 35 | + page.on('console', msg => console.log("> CONSOLE:", (msg.text() + ' - ' + msg.location().url).replace(/\s+/g, ' '))); |
| 36 | + page.on('pageerror', err => console.log("> PAGEERROR:", ('' + err).replace(/\s+/g, ' '))); |
| 37 | + |
| 38 | + // Go to http://localhost:8080/ |
| 39 | + await page.goto(`http://${host}:${port}/`); |
| 40 | + // Wait for vaadin ready |
| 41 | + await page.waitForSelector('#outlet > * > *:not(style):not(script)'); |
| 42 | + |
| 43 | + await takeScreenshot(page, 'initial-view'); |
| 44 | + // Click input[type="text"] |
| 45 | + try { |
| 46 | + await page.locator('input[type="text"]').click({timeout:10000}); |
| 47 | + } catch (error) { |
| 48 | + // skeleton-starter-flow-cdi wildfly:run sometimes does not load the page correctly |
| 49 | + log(`Error looking for input[type="text"], sleeping and reloading page\n`); |
| 50 | + await page.reload(); |
| 51 | + await page.waitForLoadState('load') |
| 52 | + await page.waitForTimeout(10000); |
| 53 | + await takeScreenshot(page, 'initial-view-after-reload'); |
| 54 | + await page.locator('input[type="text"]').click({timeout:60000}); |
| 55 | + } |
| 56 | + |
| 57 | + // Fill input[type="text"] |
| 58 | + await page.locator('input[type="text"]').fill(text); |
| 59 | + |
| 60 | + // Click text=Say hello |
| 61 | + await page.locator('vaadin-button').click(); |
| 62 | + await takeScreenshot(page, 'button-clicked'); |
| 63 | + |
| 64 | + |
| 65 | + // Look for the text, sometimes rendered in an alert, sometimes in the dom |
| 66 | + let m; |
| 67 | + try { |
| 68 | + m = await page.getByRole('alert').nth(1).innerText({timeout:500}); |
| 69 | + } catch (e) { |
| 70 | + console.log(`Not Found ${text} in an 'alert' role`); |
| 71 | + m = await page.locator(`text=/${text}/`).innerText({timeout:5000}); |
| 72 | + } |
| 73 | + if (! new RegExp(text).test(m)) { |
| 74 | + throw new Error(`${text} text not found in ${m}`); |
| 75 | + } |
| 76 | + console.log(`Found ${m} text in the dom`); |
| 77 | + |
| 78 | + // Close everything |
| 79 | + // --------------------- |
| 80 | + await context.close(); |
| 81 | + await browser.close(); |
57 | 82 | })(); |
0 commit comments