Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions features/login.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Feature: Login Feature
Given I open the "https://www.saucedemo.com/" page

Scenario: Validate the login page title
# TODO: Fix this failing scenario
Then I should see the title "Labs Swag"
# TODO: Fix this failing scenario-added correct title
Then I should see the title "Swag Labs"

Scenario: Validate login error message
Then I will login as 'locked_out_user'
# TODO: Add a step to validate the error message received
# TODO: Add a step to validate the error message received-added step to validate error msg
Then I should the error message "Epic sadface: Sorry, this user has been locked out."
7 changes: 6 additions & 1 deletion features/product.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ Feature: Product Feature
Scenario Outline: Validate product sort by price <sort>
Then I will login as 'standard_user'
# TODO: Sort the items by <sort>
Then I will sort items by "<sort>"
# TODO: Validate all 6 items are sorted correctly by price
Then I should see all 6 items sorted correctly by price "<order>"
Examples:
# TODO: extend the datatable to paramterize this test
| sort |
| sort |
| sort | order |
|Price (low to high) | asc |
|Price (low to high) | desc |
8 changes: 7 additions & 1 deletion features/purchase.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ Feature: Purchase Feature
# TODO: Fill in the First Name, Last Name, and Zip/Postal Code
# TODO: Select Continue
# TODO: Select Finish
# TODO: Validate the text 'Thank you for your order!'
# TODO: Validate the text 'Thank you for your order!'
Then I will open the cart
Then I will select Checkout
Then I will fill in the checkout info with "Patrice", "Jackson", and "11222"
Then I will select Continue
Then I will select Finish
Then I will validate the purchase message "Thank you for your order!"
2 changes: 1 addition & 1 deletion hooks/globalHooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { After, Before, setDefaultTimeout } from "@cucumber/cucumber";
import { closeBrowser, initializeBrowser, initializePage } from "../playwrightUtilities";

setDefaultTimeout(15000);
setDefaultTimeout(60000);

Before( async () => {
await initializeBrowser();
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"@types/node": "^20.10.3",
"cucumber-html-reporter": "^7.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.3.2"
"typescript": "^5.9.3"
}
}
8 changes: 8 additions & 0 deletions pages/login.page.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Page } from "@playwright/test"

export class Login {

private readonly page: Page
private readonly password: string = 'secret_sauce'
private readonly passwordField: string = 'input[id="password"]'
private readonly userNameField: string = 'input[id="user-name"]'
private readonly loginButton: string = 'input[id="login-button"]'
private readonly errorMessage: string = 'h3[data-test="error"]'

constructor(page: Page) {
this.page = page;
Expand All @@ -23,4 +25,10 @@ export class Login {
await this.page.locator(this.passwordField).fill(this.password)
await this.page.locator(this.loginButton).click()
}
public async validateErrorMessage(expectedMessage: string) {
const errorText = await this.page.locator(this.errorMessage).textContent();
if (errorText?.trim() !== expectedMessage) {
throw new Error(`Expected error message to be "${expectedMessage}" but found "${errorText?.trim()}"`);
}
}
}
21 changes: 21 additions & 0 deletions pages/product.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Page } from "@playwright/test"
export class Product {
private readonly page: Page
private readonly addToCart: string = 'button[id="add-to-cart-sauce-labs-backpack"]'
private readonly sortDropdown: string = '[data-test="product-sort-container"]';
private readonly itemPrices: string = '[data-test="inventory-item-price"]';

constructor(page: Page) {
this.page = page;
Expand All @@ -11,4 +13,23 @@ export class Product {
public async addBackPackToCart() {
await this.page.locator(this.addToCart).click()
}
public async sortItemsBy(sortOption: string) {
await this.page.locator(this.sortDropdown).selectOption({ label: sortOption });
}

public async validateItemsSortedByPrice(order: string) {
const priceElements = await this.page.locator(this.itemPrices).allTextContents();
if (priceElements.length !== 6) {
throw new Error(`Expected 6 items but found ${priceElements.length}`);
}
const prices = priceElements.map(p => parseFloat(p.replace('$', '')));
for (let i = 0; i < prices.length - 1; i++) {
if (order === 'asc' && prices[i] > prices[i + 1]) {
throw new Error(`Items are not sorted by price (low to high). Found ${prices[i]} before ${prices[i + 1]}`);
}
if (order === 'desc' && prices[i] < prices[i + 1]) {
throw new Error(`Items are not sorted by price (high to low). Found ${prices[i]} before ${prices[i + 1]}`);
}
}
}
}
46 changes: 46 additions & 0 deletions pages/purchase.page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Page } from "@playwright/test"

export class Purchase {
private readonly page: Page
private readonly cartLink: string = 'a.shopping_cart_link'
private readonly checkoutButton: string = 'button[id="checkout"]'
private readonly firstNameField: string = 'input[id="first-name"]'
private readonly lastNameField: string = 'input[id="last-name"]'
private readonly postalCodeField: string = 'input[id="postal-code"]'
private readonly continueButton: string = 'input[id="continue"]'
private readonly finishButton: string = 'button[id="finish"]'
private readonly completeHeader: string = 'h2.complete-header'

constructor(page: Page) {
this.page = page;
}

public async openCart() {
await this.page.locator(this.cartLink).click()
}

public async selectCheckout() {
await this.page.locator(this.checkoutButton).click()
}

public async fillCheckoutInfo(firstName: string, lastName: string, postalCode: string) {
await this.page.locator(this.firstNameField).fill(firstName)
await this.page.locator(this.lastNameField).fill(lastName)
await this.page.locator(this.postalCodeField).fill(postalCode)
}

public async selectContinue() {
await this.page.locator(this.continueButton).click()
}

public async selectFinish() {
await this.page.locator(this.finishButton).click()
}

public async validateThankYouText(expectedText: string) {
const text = await this.page.locator(this.completeHeader).textContent()
if (text?.trim() !== expectedText) {
throw new Error(`Expected purchase message to be "${expectedText}" but found "${text?.trim()}"`)
}
}
}
4 changes: 4 additions & 0 deletions steps/login.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ Then('I should see the title {string}', async (expectedTitle) => {

Then('I will login as {string}', async (userName) => {
await new Login(getPage()).loginAsUser(userName);
});

Then('I should the error message {string}', async (expectedMessage) => {
await new Login(getPage()).validateErrorMessage(expectedMessage);
});
7 changes: 7 additions & 0 deletions steps/product.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ import { Product } from '../pages/product.page';

Then('I will add the backpack to the cart', async () => {
await new Product(getPage()).addBackPackToCart();
});
Then('I will sort the items by {string}', async (sortOption) => {
await new Product(getPage()).sortItemsBy(sortOption);
});

Then('I should see all 6 items sorted by price {string}', async (order) => {
await new Product(getPage()).validateItemsSortedByPrice(order);
});
27 changes: 27 additions & 0 deletions steps/purchase.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Then } from '@cucumber/cucumber';
import { getPage } from '../playwrightUtilities';
import { Purchase } from '../pages/purchase.page';

Then('I will open the cart', async () => {
await new Purchase(getPage()).openCart();
});

Then('I will select Checkout', async () => {
await new Purchase(getPage()).selectCheckout();
});

Then('I will fill in the checkout info with {string}, {string}, and {string}', async (firstName, lastName, postal) => {
await new Purchase(getPage()).fillCheckoutInfo(firstName, lastName, postal);
});

Then('I will select Continue', async () => {
await new Purchase(getPage()).selectContinue();
});

Then('I will select Finish', async () => {
await new Purchase(getPage()).selectFinish();
});

Then('I will validate the purchase message {string}', async (expectedMessage) => {
await new Purchase(getPage()).validateThankYouText(expectedMessage);
});
Loading