|
1 |
| -// Basic step definitions for BDD tests |
2 |
| -// These are intentionally minimal placeholders to demonstrate the test framework setup |
| 1 | +// Basic step definitions for BDD tests using Playwright |
| 2 | +const { Given, When, Then, BeforeAll, AfterAll } = require('@cucumber/cucumber'); |
| 3 | +const { chromium } = require('playwright'); |
3 | 4 |
|
4 |
| -const { Given, When, Then } = require('@cucumber/cucumber'); |
| 5 | +let browser; |
| 6 | +let context; |
| 7 | +let page; |
5 | 8 |
|
6 |
| -// This step definition is intentionally empty to show the framework works |
7 |
| -// but tests still fail due to missing implementation |
8 |
| -Given('the Staging site is started', function () { |
9 |
| - // TODO: Implement step to start/verify staging site |
10 |
| - return 'pending'; |
| 9 | +// Set up BASE_URL with default value for staging service in Docker network |
| 10 | +const BASE_URL = process.env.BASE_URL || 'http://staging'; |
| 11 | + |
| 12 | +BeforeAll(async function () { |
| 13 | + browser = await chromium.launch({ |
| 14 | + headless: true, |
| 15 | + args: ['--no-sandbox', '--disable-setuid-sandbox'] |
| 16 | + }); |
| 17 | + context = await browser.newContext({ |
| 18 | + // Increase timeout for slower Docker environments |
| 19 | + timeout: 30000 |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +AfterAll(async function () { |
| 24 | + if (context) await context.close(); |
| 25 | + if (browser) await browser.close(); |
| 26 | +}); |
| 27 | + |
| 28 | +Given('the Staging site is started', async function () { |
| 29 | + page = await context.newPage(); |
| 30 | + |
| 31 | + try { |
| 32 | + // Verify that the BASE_URL can be reached (allow redirects) |
| 33 | + const response = await page.goto(BASE_URL, { |
| 34 | + waitUntil: 'networkidle', |
| 35 | + timeout: 30000 |
| 36 | + }); |
| 37 | + |
| 38 | + if (!response || !response.ok()) { |
| 39 | + throw new Error(`Failed to reach staging site at ${BASE_URL}. Status: ${response ? response.status() : 'No response'}`); |
| 40 | + } |
| 41 | + } catch (error) { |
| 42 | + throw new Error(`Unable to connect to staging site at ${BASE_URL}: ${error.message}`); |
| 43 | + } |
11 | 44 | });
|
12 | 45 |
|
13 |
| -When('the index page is visited', function () { |
14 |
| - // TODO: Implement step to visit index page |
15 |
| - return 'pending'; |
| 46 | +When('the index page is visited', async function () { |
| 47 | + if (!page) { |
| 48 | + throw new Error('Page not initialized. Make sure "Given the Staging site is started" step is executed first.'); |
| 49 | + } |
| 50 | + |
| 51 | + // Navigate to the index page and ensure it loads without HTTP errors |
| 52 | + const response = await page.goto(BASE_URL, { |
| 53 | + waitUntil: 'networkidle', |
| 54 | + timeout: 30000 |
| 55 | + }); |
| 56 | + |
| 57 | + if (!response || !response.ok()) { |
| 58 | + throw new Error(`Index page failed to load. Status: ${response ? response.status() : 'No response'}`); |
| 59 | + } |
| 60 | + |
| 61 | + // Verify the page has loaded by checking for basic HTML structure |
| 62 | + await page.waitForSelector('html', { timeout: 10000 }); |
16 | 63 | });
|
17 | 64 |
|
18 |
| -Then('the Logo should be displayed in the top left corner', function () { |
19 |
| - // TODO: Implement step to verify logo presence and position |
20 |
| - return 'pending'; |
| 65 | +Then('the Logo should be displayed in the top left corner', async function () { |
| 66 | + if (!page) { |
| 67 | + throw new Error('Page not initialized. Make sure previous steps are executed first.'); |
| 68 | + } |
| 69 | + |
| 70 | + // Check that favicon.png is somewhere nested under a <nav> element |
| 71 | + // This covers both relative paths (favicon.png) and absolute URLs containing favicon.png |
| 72 | + try { |
| 73 | + const faviconInNav = await page.locator('nav img[src*="favicon.png"]').first(); |
| 74 | + await faviconInNav.waitFor({ state: 'visible', timeout: 10000 }); |
| 75 | + } catch (error) { |
| 76 | + throw new Error('Logo (favicon.png) is not displayed in a nav element'); |
| 77 | + } |
21 | 78 | });
|
0 commit comments