Skip to content

Commit

Permalink
Merge pull request #62 from elgentos/gitlab-24
Browse files Browse the repository at this point in the history
Added verify check for calculations
  • Loading branch information
shayfaber authored Mar 3, 2025
2 parents 7ef9520 + 2bd05a5 commit 8f57494
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
24 changes: 22 additions & 2 deletions tests/base/checkout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test.describe('Checkout (login required)', () => {
await loginPage.login(emailInputValue, passwordInputValue);
await page.goto(slugs.checkoutSlug);
});

/**
* @feature Automatically fill in certain data in checkout (if user is logged in)
* @scenario When the user navigates to the checkout (with a product), their name and address should be filled in.
Expand Down Expand Up @@ -124,6 +124,27 @@ test.describe('Checkout (guest)', () => {
await checkout.applyDiscountCodeCheckout(discountCode);
});

test('Verify price calculations in checkout', { tag: ['@checkout', '@price-calculation'] }, async ({ page }) => {
const productPage = new ProductPage(page);
const checkoutPage = new CheckoutPage(page);

// Add product to cart and go to checkout
await productPage.addSimpleProductToCart(UIReference.productPage.simpleProductTitle, slugs.productpage.simpleProductSlug);
await page.goto(slugs.checkoutSlug);

// Select shipping method to trigger price calculations
await checkoutPage.shippingMethodOptionFixed.check();

// Wait for totals to update
await page.waitForFunction(() => {
const element = document.querySelector('.magewire\\.messenger');
return element && getComputedStyle(element).height === '0px';
});

// Get all price components using the verifyPriceCalculations method from the CheckoutPage fixture
await checkoutPage.verifyPriceCalculations();
});

/**
* @feature Remove discount code from checkout
* @scenario User has added a discount code, then removes it
Expand Down Expand Up @@ -165,4 +186,3 @@ test.describe('Checkout (guest)', () => {
await checkout.enterWrongCouponCode("incorrect discount code");
});
});

40 changes: 37 additions & 3 deletions tests/base/fixtures/checkout.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class CheckoutPage {
readonly showDiscountFormButton: Locator;
readonly placeOrderButton: Locator;
readonly continueShoppingButton: Locator;
readonly subtotalElement: Locator;
readonly shippingElement: Locator;
readonly taxElement: Locator;
readonly grandTotalElement: Locator;

constructor(page: Page){
this.page = page;
Expand All @@ -20,6 +24,10 @@ export class CheckoutPage {
this.showDiscountFormButton = this.page.getByRole('button', {name: UIReference.checkout.openDiscountFormLabel});
this.placeOrderButton = this.page.getByRole('button', { name: UIReference.checkout.placeOrderButtonLabel });
this.continueShoppingButton = this.page.getByRole('link', { name: UIReference.checkout.continueShoppingLabel });
this.subtotalElement = page.getByText('Subtotal $');
this.shippingElement = page.getByText('Shipping & Handling (Flat Rate - Fixed) $');
this.taxElement = page.getByText('Tax $');
this.grandTotalElement = page.getByText('Grand Total $');
}

// ==============================================
Expand Down Expand Up @@ -78,7 +86,7 @@ export class CheckoutPage {

let applyCouponCheckoutButton = this.page.getByRole('button', { name: UIReference.checkout.applyDiscountButtonLabel });
let checkoutDiscountField = this.page.getByPlaceholder(UIReference.checkout.discountInputFieldLabel);

await checkoutDiscountField.fill(code);
await applyCouponCheckoutButton.click();

Expand Down Expand Up @@ -106,7 +114,7 @@ export class CheckoutPage {
// discount field is not open.
await this.showDiscountFormButton.click();
}

let cancelCouponButton = this.page.getByRole('button', {name: UIReference.cart.cancelCouponButtonLabel});
await cancelCouponButton.click();

Expand All @@ -116,4 +124,30 @@ export class CheckoutPage {
let checkoutDiscountField = this.page.getByPlaceholder(UIReference.checkout.discountInputFieldLabel);
await expect(checkoutDiscountField).toBeEditable();
}
}

// ==============================================
// Price summary methods
// ==============================================

async getPriceValue(element: Locator): Promise<number> {
const priceText = await element.innerText();
// Extract just the price part after the $ symbol
const match = priceText.match(/\$\s*([\d.]+)/);
return match ? parseFloat(match[1]) : 0;
}

async verifyPriceCalculations() {
const subtotal = await this.getPriceValue(this.subtotalElement);
const shipping = await this.getPriceValue(this.shippingElement);
const tax = await this.getPriceValue(this.taxElement);
const grandTotal = await this.getPriceValue(this.grandTotalElement);

const calculatedTotal = +(subtotal + shipping + tax).toFixed(2);

expect(subtotal, `Subtotal (${subtotal}) should be greater than 0`).toBeGreaterThan(0);
expect(shipping, `Shipping cost (${shipping}) should be greater than 0`).toBeGreaterThan(0);
// Enable when tax settings are set.
//expect(tax, `Tax (${tax}) should be greater than 0`).toBeGreaterThan(0);
expect(grandTotal, `Grand total (${grandTotal}) should equal calculated total (${calculatedTotal})`).toBe(calculatedTotal);
}
}
16 changes: 12 additions & 4 deletions tests/base/setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ base('Enable multiple Magento admin logins', {tag: '@setup',}, async ({ page, br
});

base('Setup Magento environment for tests', {tag: '@setup',}, async ({ page, browserName }, testInfo) => {
if (process.env[`SETUP_COMPLETE_${browserName?.toUpperCase()}`]) {
const browserEngine = browserName?.toUpperCase() || "UNKNOWN";
const setupCompleteVar = `SETUP_COMPLETE_${browserEngine}`;
const isSetupComplete = process.env[setupCompleteVar];

if(isSetupComplete === 'DONE') {
testInfo.skip(true, `Skipping because configuration is only needed once.`);
}

Expand Down Expand Up @@ -83,10 +87,14 @@ base('Setup Magento environment for tests', {tag: '@setup',}, async ({ page, bro
if (fs.existsSync(envPath)) {
const envContent = fs.readFileSync(envPath, 'utf-8');
const browserEngine = browserName?.toUpperCase() || "UNKNOWN";
if (!envContent.includes(`SETUP_COMPLETE_${browserEngine}=true`)) {
fs.appendFileSync(envPath, `\nSETUP_COMPLETE_${browserEngine}=true`);
console.log(`Environment setup completed successfully. 'SETUP_COMPLETE_${browserEngine}=true' added to .env file.`);
if (!envContent.includes(`SETUP_COMPLETE_${browserEngine}='DONE'`)) {
fs.appendFileSync(envPath, `\nSETUP_COMPLETE_${browserEngine}='DONE'`);
console.log(`Environment setup completed successfully. 'SETUP_COMPLETE_${browserEngine}='DONE'' added to .env file.`);
}
// if (!envContent.includes(`SETUP_COMPLETE_${browserEngine}=true`)) {
// fs.appendFileSync(envPath, `\nSETUP_COMPLETE_${browserEngine}=true`);
// console.log(`Environment setup completed successfully. 'SETUP_COMPLETE_${browserEngine}=true' added to .env file.`);
// }
} else {
throw new Error('.env file not found. Please ensure it exists in the root directory.');
}
Expand Down

0 comments on commit 8f57494

Please sign in to comment.