create for me a cypress NTLM login test. Debate if using pom or custom commands it's…
- User
create for me a cypress NTLM login test. Debate if using pom or custom commands it's better follow best practices
Assistant
create for me a cypress NTLM login test. Debate if using pom or custom commands it's better follow best practices
A fantastic request! Let's dive into creating a Cypress NTLM login test, and then we'll weigh the pros and cons of using Page Object Model (POM) vs Custom Commands—following best practices, of course.
Prerequisites:
npm install cypress --save-devcypress-ntlm-auth plugin: npm install cypress-ntlm-auth --save-dev.env file (we'll use it for storing NTLM credentials):NTLM_USERNAME=your_username
NTLM_PASSWORD=your_password
NTLM_DOMAIN=your_domain
NTLM_WORKSTATION=your_workstation # optional
TARGET_URL=https://your-ntlm-protected-site.com
Step 1: Configure cypress-ntlm-auth
In your cypress/support/index.js file, add:
import 'cypress-ntlm-auth/dist/commands';
This registers the cy.ntlm() command globally.
Step 2: Write the NTLM Login Test (Raw Version)
Create a test file cypress/integration/ntlm-login.spec.js:
/// <reference types="cypress" />
describe('NTLM Login Test', () => {
beforeEach(() => {
cy.ntlm(
Cypress.env('TARGET_URL'),
Cypress.env('NTLM_USERNAME'),
Cypress.env('NTLM_PASSWORD'),
Cypress.env('NTLM_DOMAIN'),
Cypress.env('NTLM_WORKSTATION') // optional
);
});
it('should login successfully and verify the dashboard', () => {
cy.visit(Cypress.env('TARGET_URL'));
// Assuming your NTLM-protected site redirects to a dashboard or shows a specific element post-auth
cy.get('[data-testid="dashboard-header"]').should('be.visible');
});
});
This works, but it's not ideal:
Cypress.env().Now, let's explore two refactored approaches:
Custom commands are Cypress's recommended way to encapsulate reusable logic. They live in cypress/support/commands.js.
cypress/support/commands.js:
Cypress.Commands.add('loginWithNTLM', () => {
const { NTLM_USERNAME, NTLM_PASSWORD, NTLM_DOMAIN, NTLM_WORKSTATION, TARGET_URL } = Cypress.env();
cy.ntlm(TARGET_URL, NTLM_USERNAME, NTLM_PASSWORD, NTLM_DOMAIN, NTLM_WORKSTATION);
cy.visit(TARGET_URL);
});
Cypress.Commands.add('assertLoggedIn', () => {
cy.get('[data-testid="dashboard-header"]').should('be.visible');
});
Refactored Test: cypress/integration/ntlm-login.spec.js:
describe('NTLM Login Test', () => {
it('should login successfully', () => {
cy.loginWithNTLM();
cy.assertLoggedIn();
});
});
Pros of Custom Commands:
cy.loginWithNTLM() is accessible from any test file without imports.cy.").Cons:
cy.fillUsername(), cy.clickSubmit(), ...), your commands.js turns into a messy kitchen sink.Best Practice Guidance: Use custom commands for:
cy.createUserViaAPI()).Avoid turning them into a "shorthand for every single selector."
POM is a classic pattern from Selenium/WebDriver world, now widely adopted in Cypress.
Create cypress/page-objects/LoginPage.js:
export class LoginPage {
visit() {
const { TARGET_URL } = Cypress.env();
cy.ntlm(
TARGET_URL,
Cypress.env('NTLM_USERNAME'),
Cypress.env('NTLM_PASSWORD'),
Cypress.env('NTLM_DOMAIN'),
Cypress.env('NTLM_WORKSTATION')
);
cy.visit(TARGET_URL);
return this; // Chaining
}
assertLoggedIn() {
cy.get('[data-testid="dashboard-header"]').should('be.visible');
return this; // Chaining
}
}
Refactored Test: cypress/integration/ntlm-login.spec.js:
import { LoginPage } from '../page-objects/LoginPage';
describe('NTLM Login Test', () => {
const loginPage = new LoginPage();
it('should login successfully', () => {
loginPage.visit().assertLoggedIn();
});
});
Pros of POM:
LoginPage.js.Cons:
HeaderPage, FooterPage, etc., for trivial elements.Best Practice Guidance: Use POM for:
login(), then navigateToDashboard(), then assertUserProfile()).| Aspect | Custom Commands | Page Object Model (POM) |
|---|---|---|
| Reusability | Globally available. Easy to reuse across tests. | Reusable, but requires imports. More structured reuse. |
| Readability | Tests read like cy.doMagicHappensNow(). Simple, but "magic". | Tests read like page.doLogicalStep(). More context, less magic. |
| Maintainability | If auth changes, update 1 command. Easy. | If a page changes, update 1 class. Equally easy, but scoped to that "page". |
| Overhead | Low. Just add a line in commands.js. | Higher. Create new files, manage imports. |
| Best For | Auth, API helpers, super-generic actions. | Complex pages, multi-step workflows, OOP enthusiasts. |
The Verdict (Not a Mutually Exclusive Choice):
In Your Case (NTLM Login): Since NTLM auth is a global prerequisite rather than a page-specific interaction, I'd lean towards:
// commands.js
Cypress.Commands.add('loginWithNTLM', () => { /* NTLM logic */ });
// tests/
describe('Dashboard Features', () => {
beforeEach(() => cy.loginWithNTLM());
// Now test dashboard-specific things
});
But if your login page has additional complexities (e.g., 2FA, error handling, multiple forms), then:
// page-objects/LoginPage.js
export class LoginPage {
loginWithNTLM() { /* NTLM + visit */ }
fillMFA(code) { /* ... */ }
}
// tests/
const loginPage = new LoginPage();
loginPage.loginWithNTLM().assertLoggedIn().navigateTo2FA().fillMFA('123456');
TL;DR:
Happy testing! 🚀