Logo

dev-resources.site

for different kinds of informations.

How To Simplify API Request Handling with Page Object Models in Cypress

Published at
12/15/2024
Categories
cypress
qa
automation
Author
martin_chudomel
Categories
3 categories in total
cypress
open
qa
open
automation
open
Author
15 person written this
martin_chudomel
open
How To Simplify API Request Handling with Page Object Models in Cypress

When writing Cypress tests, I often needed to use cy.intercept() and cy.wait() to monitor and assert specific API requests. However, I found myself repeatedly defining the same request interception in multiple tests. This repetition became a maintenance headache when the API URL changed, forcing me to update numerous test files.

To solve this, I applied the Page Object Model (POM) pattern, centralizing request interceptions and waits. This allowed me to manage API requests in one place, simplifying test maintenance and reducing code duplication.

The Problem: Repeated Interceptions

Here’s how my tests looked before adopting the POM approach:

it('Check trackings', () => {
    cy.intercept('GET', trackerUrl).as('tracker')
    cy.visit('/')
    cy.wait('@tracker').then((interception) => {
        expect(interception.request.body.registration.hashed_email).to.eq(hashed_email)
        expect(interception.request.body.registration.urid).not.to.be.undefined
    })
})
Enter fullscreen mode Exit fullscreen mode

While this worked, the same request interception was duplicated across many tests.

The Solution: Centralizing in a Page Object Model

I moved the interception and wait logic into a POM class. Here’s how the updated test looks:

// don´t forget to import your page object model first
it('Check trackings', () => {
    homePage.interceptTracker()
    cy.visit('/')
    homePage.waitTracker().then((interception) => {
        expect(interception.request.body.registration.hashed_email).to.eq(hashed_email)
        expect(interception.request.body.registration.urid).not.to.be.undefined
    })
})
Enter fullscreen mode Exit fullscreen mode

In the homePage object:

class HomePage {
    const trackerUrl = '...'

    // other functions

    interceptTracker() {
        return cy.intercept('GET', trackerUrl).as('tracker')
    }

    waitTracker() {
        return cy.wait('@tracker')
    }
}

const homePage = new HomePage()
Enter fullscreen mode Exit fullscreen mode

When to Use Custom Commands

If the same interception logic applies to multiple pages or components, defining Cypress custom commands could be a better approach. This keeps your POMs cleaner while ensuring request handling is still centralized.

Key Takeaway

The Page Object Model isn’t just for managing UI elements. By applying it to request interceptions, I simplified API monitoring in Cypress tests and reduced maintenance effort. If you’re struggling with repetitive Cypress test logic, consider extending your POMs beyond UI elements!

qa Article's
30 articles in total
Favicon
Starting testing
Favicon
Test Case Vs Test Scenario
Favicon
Stressify.jl Performance Testing
Favicon
Test Automation Best Practices
Favicon
staging and QA will not save your systems
Favicon
From Vision to Reality: How TestSenseAI's GitHub Actions Library Revolutionises Test Framework Development
Favicon
Test Scenario Vs Test Case
Favicon
How To Simplify API Request Handling with Page Object Models in Cypress
Favicon
Bridging the Gap: Why QA and Agile Skills are Inseparable
Favicon
Smart Ignore for Visual Testing: Focus on What Matters Most
Favicon
Introduction to QA Testing: Ensuring Software Excellence
Favicon
Learn Through Past!!!
Favicon
Automated Reporting in Automation Testing
Favicon
What I Learned in a Decade of Experience in Test Automation
Favicon
New to QA? How to Shine in Your First Interview Without Experience
Favicon
The Evolution of the QA Role in Software Development Teams
Favicon
Soft Skills Every QA Professional Should Have
Favicon
WICK-A11Y 1.4.0: Not Everything Needs to Fail a Test!
Favicon
What You Need To Do Before Opening a New Bug Ticket
Favicon
Checklist with Best Practices for Writing Automation Tests
Favicon
Testing in the Dark: How to Ensure Quality Without Written Requirements
Favicon
How to setup report portal dashboards using attributes for test observability
Favicon
CYPRESS-AJV-SCHEMA-VALIDATOR VIDEO TUTORIAL: Mastering API Schema Testing in Cypress
Favicon
Accessibility Marketing: Why It’s Essential for Businesses Today
Favicon
Your Ultimate Website QA Checklist
Favicon
Develop safer web services with integrated testing
Favicon
Steps to follow to become a modern QA Engineer
Favicon
Intro Post: How AI is Transforming the Testing Landscape
Favicon
Transforming Quality Assurance and Automation Testing with AI
Favicon
Test Plan vs Test Case: Key Differences

Featured ones: