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('');

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

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

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

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

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

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

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

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