From 554054b2145bd7311e73a7ad1b7cb4787d1163a6 Mon Sep 17 00:00:00 2001 From: Mohammed Nihal <57055998+nihal467@users.noreply.github.com> Date: Mon, 13 Jan 2025 14:56:49 +0530 Subject: [PATCH] Modified the cypress file structure docs (#9925) --- cypress/docs/best-practices.md | 22 +++++++++++++++- cypress/docs/file-structure.md | 26 ++++++++++++++----- .../pageObject/facility/FacilityCreation.ts | 9 +++++-- cypress/support/commands.ts | 2 +- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/cypress/docs/best-practices.md b/cypress/docs/best-practices.md index f567b8a9ea8..30a746ce8bc 100644 --- a/cypress/docs/best-practices.md +++ b/cypress/docs/best-practices.md @@ -1,32 +1,52 @@ # Best Practices ## Test Independence + - Each test should be independent and isolated - Clean up test data after tests - Don't rely on the state from other tests ## API Testing + - Use cy.intercept() for API verification - Use waitUntil() for API completion - Avoid cy.wait() except for API responses ## Element Interaction + - Always verify element state before interaction - Use data-cy attributes for selectors - Verify button text before clicking +- Always verify loading states before and after interactions ## Code Organization + - Keep tests focused and concise - Follow AAA pattern (Arrange, Act, Assert) - Use meaningful test descriptions ## Common Pitfalls to Avoid + - Redundant visibility checks with verifyAndClickElement - Hardcoded values in page objects - Unnecessary waits - Test interdependencies +- Skipping API verifications +- Using arbitrary timeouts instead of proper waits ## Performance Considerations + - Minimize unnecessary API calls - Use efficient selectors -- Batch similar operations \ No newline at end of file +- Batch similar operations + +## Testing Checklist + +Before submitting your test, verify: + +- [ ] All API calls are intercepted and verified +- [ ] Loading states are handled properly +- [ ] Success/error states are verified +- [ ] No arbitrary timeouts used +- [ ] Search operations include debounce handling +- [ ] Form submissions verify both request and response diff --git a/cypress/docs/file-structure.md b/cypress/docs/file-structure.md index 8edee0d93f6..0526a478bd5 100644 --- a/cypress/docs/file-structure.md +++ b/cypress/docs/file-structure.md @@ -1,6 +1,7 @@ # File Structure and Organization ## Directory Structure + ``` cypress/ ├── docs/ @@ -9,31 +10,42 @@ cypress/ │ ├── patterns.md │ └── best-practices.md ├── e2e/ # Test files grouped by modules -│ ├── patient/ -│ ├── facility/ -│ └── user/ -├── fixtures/ -├── pageObject/ -└── support/ +│ ├── patient/ +│ ├── facility/ +│ └── user/ +├── fixtures/ +├── pageObject/ # Page Objects grouped by modules +│ ├── patient/ +│ ├── facility/ +│ └── user/ +├── utils/ # Utility functions and helpers +│ ├── facilityData.ts # Facility-related utility functions +│ └── commonUtils.ts # Shared utility functions +└── support/ ``` ## Module Organization + Each module (patient, facility, user, etc.) should have: + - Test files in `e2e//` - Page Object in `pageObject//` - Fixtures in `fixtures//` ## File Naming Conventions + - Test files: `feature-name.cy.ts` - Page Object: `FeatureNamePage.ts` - Custom Commands: `feature-name.ts` - Fixtures: `feature-name-data.json` ## Support Files + - `commands.ts`: Custom Cypress commands - `e2e.ts`: e2e configurations - `index.ts`: Main support file ## Storage Management + - Use cy.saveLocalStorage() and cy.restoreLocalStorage() -- Manage test data cleanup \ No newline at end of file +- Manage test data cleanup diff --git a/cypress/pageObject/facility/FacilityCreation.ts b/cypress/pageObject/facility/FacilityCreation.ts index 2f86a0eaeca..0ef7cb918e8 100644 --- a/cypress/pageObject/facility/FacilityCreation.ts +++ b/cypress/pageObject/facility/FacilityCreation.ts @@ -95,7 +95,13 @@ export class FacilityCreation { } searchFacility(facilityName: string) { - cy.typeIntoField('[data-cy="search-facility"]', facilityName); + cy.intercept("GET", `**/api/v1/facility/?**`).as("searchFacility"); + + cy.get('[data-cy="search-facility"]') + .focus() + .type(facilityName, { force: true }); + + cy.wait("@searchFacility").its("response.statusCode").should("eq", 200); } verifyFacilityNameInCard(facilityName: string) { @@ -106,6 +112,5 @@ export class FacilityCreation { cy.get('[data-cy="facility-cards"]', { timeout }) .should("be.visible") .should("not.be.empty"); - return this; } } diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index a73e27b9664..31f46ed5762 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -244,7 +244,7 @@ Cypress.Commands.add( const { clearBeforeTyping = false, skipVerification = false, - delay = 50, + delay = 0, } = options; const inputField = cy.get(selector);