Skip to content

Commit 7d1a103

Browse files
committed
add hooks, world, login feature and steps definition
1 parent 50edadf commit 7d1a103

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

tests/features/login.feature

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
Feature: User Login
22

33
Scenario: Verify successful login with valid credentials
4-
Given user is on the login page
4+
Given user navigates to the login page
55
When user enters valid username and password
66
And user click the login button
77
Then user should see the user dashboard
88

9-
Scenario: Verify unsuccessful login with invalid password
10-
Given user is on the login page
11-
When user enters invalid username or password
12-
And user click the login button
13-
Then user should see an error message
9+
# Scenario: Verify unsuccessful login with invalid password
10+
# Given user navigates to the login page
11+
# When user enters invalid username or password
12+
# And user click the login button
13+
# Then user should see an error message
1414

15-
Scenario: Verify unsuccessful login with invalid username and password
16-
Given user is on the login page
17-
When user enters invalid username and password
18-
And user click the login button
19-
Then user should see an error message
15+
# Scenario: Verify unsuccessful login with invalid username and password
16+
# Given user navigates to the login page
17+
# When user enters invalid username and password
18+
# And user click the login button
19+
# Then user should see an error message

tests/hooks/hooks.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Before, After, setDefaultTimeout } from "@cucumber/cucumber";
2+
import { chromium } from "playwright";
3+
import { CustomWorld } from "../support/world";
4+
5+
// Increase default step timeout to 10s to allow slow navigation in dev
6+
setDefaultTimeout(10 * 1000);
7+
8+
Before(async function (this: CustomWorld) {
9+
// Launch a browser and new page for each scenario
10+
const browser = await chromium.launch({ headless: false });
11+
const page = await browser.newPage();
12+
13+
// Attach to World so step defs can access via this.browser / this.page
14+
this.browser = browser;
15+
this.page = page;
16+
});
17+
18+
After(async function (this: CustomWorld) {
19+
// Close page and browser if present
20+
if (this.page) await this.page.close();
21+
if (this.browser) await this.browser.close();
22+
});

tests/steps/login.steps.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//import { page } from "./world";
2+
import settings from "../config/settings.json"; // adjust path if needed
3+
import { Given, When, Then } from "@cucumber/cucumber";
4+
import { expect } from "@playwright/test";
5+
6+
Given("user navigates to the login page", async function () {
7+
await this.page.goto(settings.LOGIN_URL);
8+
});
9+
10+
When("user enters valid username and password", async function () {
11+
console.log("Filling in username and password fields");
12+
console.log(`Using email: ${settings.EMAIL}`);
13+
console.log(`Using password: ${settings.PASSWORD}`);
14+
15+
// Wait for elements to be ready
16+
await this.page.waitForSelector("#email", { state: "visible" });
17+
await this.page.waitForSelector("#password", { state: "visible" });
18+
19+
await this.page.locator("#email").fill(settings.EMAIL);
20+
await this.page.locator("#password").fill(settings.PASSWORD);
21+
});
22+
23+
When("user click the login button", async function () {
24+
// Use the data-test attribute - most reliable
25+
const submitButton = this.page.locator('[data-test="login-submit"]');
26+
27+
// Wait for button to be visible and enabled
28+
await submitButton.waitFor({ state: "visible" });
29+
30+
console.log("Login button found, clicking...");
31+
await submitButton.click();
32+
33+
// Wait a moment for the login to process
34+
await this.page.waitForTimeout(3000);
35+
36+
console.log("Current URL after login:", this.page.url());
37+
});
38+
39+
Then("user should see the user dashboard", async function () {
40+
// Wait for dashboard element to be visible
41+
await this.page.waitForSelector("h1", { state: "visible" });
42+
43+
// Get the text content of the dashboard header
44+
const headingText = await this.page.locator("h1").textContent();
45+
46+
// Assert the heading text matches expectation
47+
expect(headingText).toBe("My account");
48+
});

tests/support/world.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { setWorldConstructor, World } from "@cucumber/cucumber";
2+
import { Browser, Page } from "playwright";
3+
4+
export class CustomWorld extends World {
5+
browser!: Browser;
6+
page!: Page;
7+
}
8+
setWorldConstructor(CustomWorld);

0 commit comments

Comments
 (0)