Skip to content

Commit 1008263

Browse files
author
Robert Bradley
committed
fixing tests
1 parent e91be5b commit 1008263

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"@aws-sdk/client-dynamodb": "^3.651.1",
1212
"@cucumber/cucumber": "^11.0.1",
13-
"chromedriver": "^138.0.0",
13+
1414
"cookie-parser": "^1.4.6",
1515
"debug": "~4.4.0",
1616
"dotenv": "^16.5.0",

test/integration-tests/features/hotel_management.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ Feature: AWS App Runner Hotel Web Application
4242
And I select "Yes" from the "Good View" dropdown
4343
And I click the "Add room" button
4444
When I click on "Rooms" in the navbar
45-
Then I should see a room with the room number "101", on floor "5", with "Yes" under Good View
45+
Then I should see a room with the room number "101", on floor "5", with "Yes" under Good View

test/integration-tests/features/step_definitions/helpers/homepageHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function verifyHomepageElements(driver, element, value = null) {
1515
}
1616

1717
const navbarText = await navbar.getText();
18-
if (!navbarText.includes("Home") || !navbarText.includes("Rooms") || !navbarText.includes("Book")) {
18+
if (!navbarText.includes("Home") || !navbarText.includes("Rooms") || !navbarText.includes("Add")) {
1919
throw new Error(`Navbar links missing. Got: ${navbarText}`);
2020
}
2121
} else if (element === "heading") {

test/integration-tests/features/step_definitions/hotel_steps.js

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { setDefaultTimeout, Given, When, Then, After } = require('@cucumber/cucumber');
22
const { Builder, By, until } = require('selenium-webdriver');
33
const chrome = require('selenium-webdriver/chrome');
4+
const { ServiceBuilder } = require('selenium-webdriver/chrome');
45
const { openHomepage, verifyHomepageElements } = require('./helpers/homepageHelper');
56
const {
67
openAddRoomPage,
@@ -34,15 +35,15 @@ const buildDriver = async () => {
3435
} else {
3536
// If no GRID_URL is set, run the browser locally
3637
let options = new chrome.Options();
38+
options.addArguments(
39+
'--no-sandbox',
40+
'--disable-dev-shm-usage',
41+
'--disable-gpu'
42+
);
3743

3844
if (process.env.CI) {
3945
console.log('Running in CI, enabling headless mode for Chrome');
40-
options.addArguments(
41-
'--headless', // Run in headless mode
42-
'--disable-gpu',
43-
'--no-sandbox',
44-
'--disable-dev-shm-usage'
45-
);
46+
options.addArguments('--headless');
4647
}
4748

4849
driver = await new Builder()
@@ -59,7 +60,7 @@ const buildDriver = async () => {
5960
// Step Definitions
6061
Given('I am on the homepage', async function () {
6162
await buildDriver(); // Create WebDriver instance
62-
const baseUrl = process.env.BASE_URL || 'http://localhost:3000';
63+
const baseUrl = process.env.BASE_URL || 'http://localhost:8081';
6364
await openHomepage(driver, baseUrl); // Use helper function to open the homepage
6465
});
6566

@@ -76,31 +77,40 @@ Then('I should see the heading {string}', async function (headingText) {
7677
});
7778

7879
When('I click on {string} in the navbar', async function (linkText) {
79-
// Wait explicitly for the navbar link to be clickable
80-
const navbarLinkLocator = By.linkText(linkText);
81-
const navbarLink = await driver.wait(
82-
until.elementLocated(navbarLinkLocator),
83-
10000,
84-
`Navbar link "${linkText}" not found`
85-
);
86-
87-
// Ensure the element is visible and clickable
88-
await driver.wait(
89-
until.elementIsVisible(navbarLink),
90-
10000,
91-
`Navbar link "${linkText}" not visible`
92-
);
93-
await driver.wait(
94-
until.elementIsEnabled(navbarLink),
95-
10000,
96-
`Navbar link "${linkText}" not enabled`
97-
);
98-
99-
// Click the navbar link
100-
await navbarLink.click();
101-
102-
// Wait briefly for the new page to load completely
103-
await driver.sleep(500); // adjust as necessary or wait explicitly for next page element
80+
// Retry mechanism for stale element reference
81+
let attempts = 0;
82+
const maxAttempts = 3;
83+
84+
while (attempts < maxAttempts) {
85+
try {
86+
// Wait for page to be ready
87+
await driver.sleep(1000);
88+
89+
// Re-find the navbar link to avoid stale element reference
90+
const navbarLinkLocator = By.linkText(linkText);
91+
await driver.wait(
92+
until.elementLocated(navbarLinkLocator),
93+
10000,
94+
`Navbar link "${linkText}" not found`
95+
);
96+
97+
// Find the element fresh each time
98+
const navbarLink = await driver.findElement(navbarLinkLocator);
99+
100+
// Click the navbar link
101+
await navbarLink.click();
102+
103+
// Wait for page to load
104+
await driver.sleep(1000);
105+
break;
106+
} catch (error) {
107+
attempts++;
108+
if (attempts >= maxAttempts) {
109+
throw error;
110+
}
111+
await driver.sleep(500);
112+
}
113+
}
104114
});
105115

106116

views/layout.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ html
2020
li.nav-item
2121
a.nav-link(href='/rooms') Rooms
2222
li.nav-item
23-
a.nav-link(href='/add') Book
23+
a.nav-link(href='/add') Add
2424

2525

2626

0 commit comments

Comments
 (0)