|
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 | +}); |
| 19 | + |
| 20 | +AfterAll(async function () { |
| 21 | + if (context) await context.close(); |
| 22 | + if (browser) await browser.close(); |
| 23 | +}); |
| 24 | + |
| 25 | +Given('the Staging site is started', async function () { |
| 26 | + page = await context.newPage(); |
| 27 | + |
| 28 | + try { |
| 29 | + // Verify that the BASE_URL can be reached |
| 30 | + const response = await page.goto(BASE_URL, { waitUntil: 'networkidle' }); |
| 31 | + |
| 32 | + if (!response || !response.ok()) { |
| 33 | + throw new Error(`Failed to reach staging site at ${BASE_URL}. Status: ${response ? response.status() : 'No response'}`); |
| 34 | + } |
| 35 | + } catch (error) { |
| 36 | + throw new Error(`Unable to connect to staging site at ${BASE_URL}: ${error.message}`); |
| 37 | + } |
11 | 38 | }); |
12 | 39 |
|
13 | | -When('the index page is visited', function () { |
14 | | - // TODO: Implement step to visit index page |
15 | | - return 'pending'; |
| 40 | +When('the index page is visited', async function () { |
| 41 | + if (!page) { |
| 42 | + throw new Error('Page not initialized. Make sure "Given the Staging site is started" step is executed first.'); |
| 43 | + } |
| 44 | + |
| 45 | + // Navigate to the index page and ensure it loads without HTTP errors |
| 46 | + const response = await page.goto(BASE_URL, { waitUntil: 'networkidle' }); |
| 47 | + |
| 48 | + if (!response || !response.ok()) { |
| 49 | + throw new Error(`Index page failed to load. Status: ${response ? response.status() : 'No response'}`); |
| 50 | + } |
| 51 | + |
| 52 | + // Verify the page has loaded by checking for basic HTML structure |
| 53 | + await page.waitForSelector('html'); |
16 | 54 | }); |
17 | 55 |
|
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'; |
| 56 | +Then('the Logo should be displayed in the top left corner', async function () { |
| 57 | + if (!page) { |
| 58 | + throw new Error('Page not initialized. Make sure previous steps are executed first.'); |
| 59 | + } |
| 60 | + |
| 61 | + // Check that favicon.png is somewhere nested under a <nav> element |
| 62 | + const faviconInNav = await page.locator('nav img[src*="favicon.png"]').first(); |
| 63 | + |
| 64 | + const isVisible = await faviconInNav.isVisible().catch(() => false); |
| 65 | + |
| 66 | + if (!isVisible) { |
| 67 | + throw new Error('Logo (favicon.png) is not displayed in a nav element'); |
| 68 | + } |
21 | 69 | }); |
0 commit comments