|
1 | 1 | const { setDefaultTimeout, Given, When, Then, After } = require('@cucumber/cucumber'); |
2 | 2 | const { Builder, By, until } = require('selenium-webdriver'); |
| 3 | +const chrome = require('selenium-webdriver/chrome'); // <-- Add this import |
3 | 4 | const { openHomepage, verifyHomepageElements } = require('./helpers/homepageHelper'); |
4 | | -const { openAddRoomPage, fillAddRoomForm, verifyAddRoomSuccess, verifyAddRoomFormFields, verifySubmitButton } = require('./helpers/addRoomHelper'); |
5 | | -const { openRoomsPage, verifyRoomList, verifyRoomDetails, verifyTableColumns, verifyRoomsStoredAlert } = require('./helpers/roomsHelper'); |
| 5 | +const { |
| 6 | + openAddRoomPage, |
| 7 | + fillAddRoomForm, |
| 8 | + verifyAddRoomSuccess, |
| 9 | + verifyAddRoomFormFields, |
| 10 | + verifySubmitButton |
| 11 | +} = require('./helpers/addRoomHelper'); |
| 12 | +const { |
| 13 | + openRoomsPage, |
| 14 | + verifyRoomList, |
| 15 | + verifyRoomDetails, |
| 16 | + verifyTableColumns, |
| 17 | + verifyRoomsStoredAlert |
| 18 | +} = require('./helpers/roomsHelper'); |
6 | 19 |
|
7 | 20 | let driver; |
8 | 21 |
|
9 | 22 | setDefaultTimeout(120 * 1000); // needed for the time it takes to spin up the remote web driver if used |
10 | 23 |
|
11 | 24 | // Function to build and return a WebDriver instance |
12 | 25 | const buildDriver = async () => { |
13 | | - const gridUrl = process.env.GRID_URL || null; |
14 | | - |
15 | | - if (gridUrl) { |
16 | | - console.log(`Using Selenium Grid at: ${gridUrl}`); |
17 | | - driver = await new Builder() |
18 | | - .usingServer(gridUrl) // Use the Selenium Grid URL here |
19 | | - .forBrowser('chrome') |
20 | | - .build(); |
21 | | - } else { |
22 | | - // If no GRID_URL is set, run the browser locally |
23 | | - driver = await new Builder().forBrowser('chrome').build(); |
| 26 | + const gridUrl = process.env.GRID_URL || null; |
| 27 | + |
| 28 | + if (gridUrl) { |
| 29 | + console.log(`Using Selenium Grid at: ${gridUrl}`); |
| 30 | + driver = await new Builder() |
| 31 | + .usingServer(gridUrl) // Use the Selenium Grid URL here |
| 32 | + .forBrowser('chrome') |
| 33 | + .build(); |
| 34 | + } else { |
| 35 | + // If no GRID_URL is set, run the browser locally |
| 36 | + let options = new chrome.Options(); |
| 37 | + |
| 38 | + if (process.env.CI) { |
| 39 | + // Running in CI: add headless flags |
| 40 | + options = options |
| 41 | + .headless() |
| 42 | + .addArguments('--disable-gpu') |
| 43 | + .addArguments('--no-sandbox') |
| 44 | + .addArguments('--disable-dev-shm-usage'); |
24 | 45 | } |
25 | 46 |
|
26 | | - // Common settings for the driver |
27 | | - await driver.manage().setTimeouts({ implicit: 10000 }); |
28 | | - await driver.manage().window().setRect({ width: 1920, height: 1080 }); // Full HD resolution |
| 47 | + driver = await new Builder() |
| 48 | + .forBrowser('chrome') |
| 49 | + .setChromeOptions(options) |
| 50 | + .build(); |
| 51 | + } |
| 52 | + |
| 53 | + // Common settings for the driver |
| 54 | + await driver.manage().setTimeouts({ implicit: 10000 }); |
| 55 | + await driver.manage().window().setRect({ width: 1920, height: 1080 }); // Full HD resolution |
29 | 56 | }; |
30 | 57 |
|
31 | 58 | // Step Definitions |
32 | 59 | Given('I am on the homepage', async function () { |
33 | | - await buildDriver(); // Create WebDriver instance |
34 | | - const baseUrl = process.env.BASE_URL || 'http://localhost:3000'; |
35 | | - await openHomepage(driver, baseUrl); // Use helper function to open the homepage |
| 60 | + await buildDriver(); // Create WebDriver instance |
| 61 | + const baseUrl = process.env.BASE_URL || 'http://localhost:3000'; |
| 62 | + await openHomepage(driver, baseUrl); // Use helper function to open the homepage |
36 | 63 | }); |
37 | 64 |
|
38 | 65 | Then('I should see the page title {string}', async function (title) { |
39 | | - await verifyHomepageElements(driver, 'title', title); // Use helper function to verify the title |
| 66 | + await verifyHomepageElements(driver, 'title', title); // Use helper function to verify the title |
40 | 67 | }); |
41 | 68 |
|
42 | 69 | Then('I should see a navbar with {string}, {string}, and {string} options', async function (home, rooms, add) { |
43 | | - await verifyHomepageElements(driver, 'navbar'); // Use helper function to verify the navbar with the given options |
| 70 | + await verifyHomepageElements(driver, 'navbar'); // Use helper function to verify the navbar |
44 | 71 | }); |
45 | 72 |
|
46 | 73 | Then('I should see the heading {string}', async function (headingText) { |
47 | | - await verifyHomepageElements(driver, 'heading', headingText); // Use helper function to verify the heading |
| 74 | + await verifyHomepageElements(driver, 'heading', headingText); // Use helper function to verify the heading |
48 | 75 | }); |
49 | 76 |
|
50 | 77 | When('I click on {string} in the navbar', async function (linkText) { |
51 | | - if (linkText === "Rooms") { |
52 | | - await openRoomsPage(driver); // Use helper to open the Rooms page |
53 | | - } else if (linkText === "Add") { |
54 | | - await openAddRoomPage(driver); // Use helper to open the Add Room page |
55 | | - } else { |
56 | | - throw new Error(`Link text ${linkText} is not supported.`); |
57 | | - } |
| 78 | + if (linkText === "Rooms") { |
| 79 | + await openRoomsPage(driver); // Use helper to open the Rooms page |
| 80 | + } else if (linkText === "Add") { |
| 81 | + await openAddRoomPage(driver); // Use helper to open the Add Room page |
| 82 | + } else { |
| 83 | + throw new Error(`Link text ${linkText} is not supported.`); |
| 84 | + } |
58 | 85 | }); |
59 | 86 |
|
60 | 87 | Then('I should be on the {string} page', async function (pageTitle) { |
61 | | - await verifyRoomList(driver, 'title', pageTitle); // Use helper function to verify the page title |
| 88 | + await verifyRoomList(driver, 'title', pageTitle); // Use helper function to verify the page title |
62 | 89 | }); |
63 | 90 |
|
64 | 91 | Then('I should see a table with the list of rooms', async function () { |
65 | | - await verifyRoomList(driver, 'room_table'); // Use helper function to verify room table is present |
| 92 | + await verifyRoomList(driver, 'room_table'); // Use helper function to verify room table is present |
66 | 93 | }); |
67 | 94 |
|
68 | 95 | Then('the table should contain columns for {string}, {string}, and {string}', async function (column1, column2, column3) { |
69 | | - await verifyTableColumns(driver); // Use helper function to verify table columns |
| 96 | + await verifyTableColumns(driver); // Use helper function to verify table columns |
70 | 97 | }); |
71 | 98 |
|
72 | 99 | When('I enter {string} in the {string} field', async function (value, fieldName) { |
73 | | - if (fieldName === "Room number") { |
74 | | - await fillAddRoomForm(driver, "room_number", value); |
75 | | - } else if (fieldName === "Floor number") { |
76 | | - await fillAddRoomForm(driver, "floor_number", value); |
77 | | - } else { |
78 | | - throw new Error(`Unsupported field name: ${fieldName}`); |
79 | | - } |
| 100 | + if (fieldName === "Room number") { |
| 101 | + await fillAddRoomForm(driver, "room_number", value); |
| 102 | + } else if (fieldName === "Floor number") { |
| 103 | + await fillAddRoomForm(driver, "floor_number", value); |
| 104 | + } else { |
| 105 | + throw new Error(`Unsupported field name: ${fieldName}`); |
| 106 | + } |
80 | 107 | }); |
81 | 108 |
|
82 | 109 | When('I select {string} from the "Good View" dropdown', async function (value) { |
83 | | - await fillAddRoomForm(driver, "good_view", value); |
| 110 | + await fillAddRoomForm(driver, "good_view", value); |
84 | 111 | }); |
85 | 112 |
|
86 | 113 | When('I click the "Add room" button', async function () { |
87 | | - await fillAddRoomForm(driver, "submit"); |
| 114 | + await fillAddRoomForm(driver, "submit"); |
88 | 115 | }); |
89 | 116 |
|
90 | 117 | Then('the new room should be added successfully', async function () { |
91 | | - await verifyAddRoomSuccess(driver); |
| 118 | + await verifyAddRoomSuccess(driver); |
92 | 119 | }); |
93 | 120 |
|
94 | 121 | Then('I should see a room with the room number {string}, on floor {string}, with {string} under Good View', async function (roomNumber, floorNumber, viewStatus) { |
95 | | - await verifyRoomDetails(driver, roomNumber, floorNumber, viewStatus); |
| 122 | + await verifyRoomDetails(driver, roomNumber, floorNumber, viewStatus); |
96 | 123 | }); |
97 | 124 |
|
98 | 125 | Then('I should see an alert displaying the number of rooms stored in the database', async function () { |
99 | | - await verifyRoomsStoredAlert(driver); |
| 126 | + await verifyRoomsStoredAlert(driver); |
100 | 127 | }); |
101 | 128 |
|
102 | 129 | Then('I should see a form with fields for {string}, {string}, and {string}', { timeout: 20000 }, async function (field1, field2, field3) { |
103 | | - await verifyAddRoomFormFields(driver); |
| 130 | + await verifyAddRoomFormFields(driver); |
104 | 131 | }); |
105 | 132 |
|
106 | 133 | Then('I should see a submit button labeled {string}', { timeout: 20000 }, async function (buttonLabel) { |
107 | | - await verifySubmitButton(driver, buttonLabel); |
| 134 | + await verifySubmitButton(driver, buttonLabel); |
108 | 135 | }); |
109 | 136 |
|
110 | 137 | // Tear Down |
111 | 138 | After(async function () { |
112 | | - if (driver) { |
113 | | - await driver.quit(); |
114 | | - } |
| 139 | + if (driver) { |
| 140 | + await driver.quit(); |
| 141 | + } |
115 | 142 | }); |
0 commit comments