1
1
const { setDefaultTimeout, Given, When, Then, After } = require ( '@cucumber/cucumber' ) ;
2
2
const { Builder, By, until } = require ( 'selenium-webdriver' ) ;
3
3
const chrome = require ( 'selenium-webdriver/chrome' ) ;
4
+ const { ServiceBuilder } = require ( 'selenium-webdriver/chrome' ) ;
4
5
const { openHomepage, verifyHomepageElements } = require ( './helpers/homepageHelper' ) ;
5
6
const {
6
7
openAddRoomPage,
@@ -34,15 +35,15 @@ const buildDriver = async () => {
34
35
} else {
35
36
// If no GRID_URL is set, run the browser locally
36
37
let options = new chrome . Options ( ) ;
38
+ options . addArguments (
39
+ '--no-sandbox' ,
40
+ '--disable-dev-shm-usage' ,
41
+ '--disable-gpu'
42
+ ) ;
37
43
38
44
if ( process . env . CI ) {
39
45
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' ) ;
46
47
}
47
48
48
49
driver = await new Builder ( )
@@ -59,7 +60,7 @@ const buildDriver = async () => {
59
60
// Step Definitions
60
61
Given ( 'I am on the homepage' , async function ( ) {
61
62
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 ' ;
63
64
await openHomepage ( driver , baseUrl ) ; // Use helper function to open the homepage
64
65
} ) ;
65
66
@@ -76,31 +77,40 @@ Then('I should see the heading {string}', async function (headingText) {
76
77
} ) ;
77
78
78
79
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
+ }
104
114
} ) ;
105
115
106
116
0 commit comments