Mastering Selenium for Business Logic Testing: Seamlessly Connect Test Cases with Real-world Scenarios

Adarrsh Paul
2 min readMay 28, 2023

--

Feature: Testing Checkout Flow

Feature: Use Check Out Flow

Scenario: User completes a purchase
Given the user is on the product page
When the user adds item A to the cart
And the user adds item B to the cart
And the user goes to the checkout page
And the user selects a shipping address
And the user provides payment information
And the user confirms the purchase
Then the user should receive an order confirmation

Example `features/step-definitions/api-steps.js` file:


const { Given, When, Then } = require('cucumber');
const { Builder, By, until } = require('selenium-webdriver');

let driver;

Given('the user is on the product page', async function () {
driver = new Builder().forBrowser('chrome').build();
await driver.get('https://example.com/product');
});

When('the user adds item A to the cart', async function () {
const itemAButton = await driver.findElement(By.id('add-to-cart-item-a'));
await itemAButton.click();
});

When('the user adds item B to the cart', async function () {
const itemBButton = await driver.findElement(By.id('add-to-cart-item-b'));
await itemBButton.click();
});

When('the user goes to the checkout page', async function () {
const checkoutButton = await driver.findElement(By.id('checkout-button'));
await checkoutButton.click();
});

When('the user selects a shipping address', async function () {
const shippingAddressRadio = await driver.findElement(By.id('shipping-address-123'));
await shippingAddressRadio.click();
});

When('the user provides payment information', async function () {
const creditCardNumberInput = await driver.findElement(By.id('credit-card-number'));
await creditCardNumberInput.sendKeys('1234567890');
});

When('the user confirms the purchase', async function () {
const confirmPurchaseButton = await driver.findElement(By.id('confirm-purchase-button'));
await confirmPurchaseButton.click();
});

Then('the user should receive an order confirmation', async function () {
const orderConfirmationMessage = await driver.wait(until.elementLocated(By.id('order-confirmation-message')), 5000);
const messageText = await orderConfirmationMessage.getText();
expect(messageText).toBe('Your order has been confirmed.');
});

After(async function () {
await driver.quit();
});

--

--

No responses yet