diff --git a/__test__/BasePage.test.ts b/__test__/BasePage.test.ts deleted file mode 100644 index ffcc4a4..0000000 --- a/__test__/BasePage.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { describe, test, expect } from 'vitest' -import { resolveElementName } from '../src/pages/BasePage' - -describe('resolveElementName', () => { - const cases = [ - { explicit: 'myName', caller: 'getter', fallback: '#sel', expected: 'myName' }, - { explicit: undefined, caller: 'getter', fallback: '#sel', expected: 'getter' }, - { explicit: undefined, caller: null, fallback: '#sel', expected: '#sel' }, - ] - - cases.forEach(({ explicit, caller, fallback, expected }) => { - test(`resolves to "${expected}" when explicit=${explicit}, caller=${caller}`, () => { - expect(resolveElementName(explicit, caller, fallback)).toBe(expected) - }) - }) -}) diff --git a/cypress/e2e/basepage/basepage.cy.ts b/cypress/e2e/basepage/basepage.cy.ts index d82d681..ae12414 100644 --- a/cypress/e2e/basepage/basepage.cy.ts +++ b/cypress/e2e/basepage/basepage.cy.ts @@ -2,15 +2,11 @@ import { BasePage } from '../../../src/pages/BasePage' class TestPage extends BasePage { get heading() { - return this.el('h1') + return this.el('h1') // Logs: heading } - get link() { - return this.elContains('Impressum') - } - - getExplicitName() { - return this.el('h1', 'explicitHeading') + get imprintLink() { + return this.el('a[href*="impressum"]') // Logs: imprintLink } } @@ -25,11 +21,7 @@ describe('BasePage', () => { page.heading.should('exist') }) - it('logs getter name for elContains()', () => { - page.link.should('exist') - }) - - it('uses explicit name when provided', () => { - page.getExplicitName().should('exist') + it('logs getter name for imprintLink', () => { + page.imprintLink.should('exist') }) }) diff --git a/dist/commands.js b/dist/commands.js index 4ad738d..bb7ac12 100644 --- a/dist/commands.js +++ b/dist/commands.js @@ -21,6 +21,8 @@ const tt_validate_language_tag_1 = require("./commands/tt-validate-language-tag" const tt_validate_no_google_services_1 = require("./commands/tt-validate-no-google-services"); const tt_validate_page_content_1 = require("./commands/tt-validate-page-content"); const tt_validate_subpages_and_images_1 = require("./commands/tt-validate-subpages-and-images"); +const tt_el_1 = require("./commands/tt-el"); +Cypress.Commands.add('ttEl', tt_el_1.ttEl); Cypress.Commands.add('ttAccessibility', tt_accessibility_1.ttAccessibility); Cypress.Commands.add('ttClickIfElementExist', tt_click_if_element_exist_1.ttClickIfElementExist); Cypress.Commands.add('ttCookieAllAcceptClick', tt_cookie_all_accept_1.ttCookieAllAcceptClick); diff --git a/dist/commands/tt-el.d.ts b/dist/commands/tt-el.d.ts new file mode 100644 index 0000000..2f6b439 --- /dev/null +++ b/dist/commands/tt-el.d.ts @@ -0,0 +1 @@ +export declare const ttEl: (selector: string, name?: string) => Cypress.Chainable>; diff --git a/dist/commands/tt-el.js b/dist/commands/tt-el.js new file mode 100644 index 0000000..5d43dd9 --- /dev/null +++ b/dist/commands/tt-el.js @@ -0,0 +1,8 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ttEl = void 0; +const ttEl = (selector, name) => { + cy.log(name || selector); + return cy.get(selector); +}; +exports.ttEl = ttEl; diff --git a/dist/commands/tt-only-one-h1.js b/dist/commands/tt-only-one-h1.js index eec1592..cac141c 100644 --- a/dist/commands/tt-only-one-h1.js +++ b/dist/commands/tt-only-one-h1.js @@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.ttOnlyOneH1 = void 0; const ttOnlyOneH1 = () => { cy.log('ttOnlyOneH1 - NCA TESTIFY'); - cy.get('h1').its('length').should('eq', 1); + // @ts-ignore - Custom command registered at runtime + cy.ttEl('h1', 'h1Heading').its('length').should('eq', 1); }; exports.ttOnlyOneH1 = ttOnlyOneH1; diff --git a/dist/commands/tt-validate-language-tag.js b/dist/commands/tt-validate-language-tag.js index 4f71330..9d0b2b6 100644 --- a/dist/commands/tt-validate-language-tag.js +++ b/dist/commands/tt-validate-language-tag.js @@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.ttValidateLanguageTag = void 0; const ttValidateLanguageTag = (language = 'de') => { cy.log('ttValidateLanguageTag - NCA TESTIFY'); - cy.get('html').should('have.attr', 'lang'); - cy.get('html') + // @ts-ignore - Custom command registered at runtime + cy.ttEl('html', 'htmlElement').should('have.attr', 'lang'); + // @ts-ignore - Custom command registered at runtime + cy.ttEl('html', 'htmlElement') .invoke('attr', 'lang') .then((langTag) => { - cy.wrap(langTag).then((lang) => { - expect(lang.toLowerCase()).to.contain(language); - }); + expect(langTag.toLowerCase()).to.contain(language); }); }; exports.ttValidateLanguageTag = ttValidateLanguageTag; diff --git a/dist/index.d.ts b/dist/index.d.ts index 36e95a7..ec7fda0 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -9,6 +9,7 @@ interface DomainMappingConfig { declare namespace Cypress { interface Chainable { + ttEl(selector: string, name?: string): Chainable> ttAccessibility(context?: any, options?: any): Chainable ttClickIfElementExist(element: string): Chainable ttCookieAllAcceptClick(): Chainable @@ -47,8 +48,4 @@ export abstract class BasePage { selector: string, name?: string ): Cypress.Chainable> - protected elContains( - text: string, - name?: string - ): Cypress.Chainable> } diff --git a/dist/pages/BasePage.d.ts b/dist/pages/BasePage.d.ts index cdc6e28..e40d4b4 100644 --- a/dist/pages/BasePage.d.ts +++ b/dist/pages/BasePage.d.ts @@ -1,6 +1,4 @@ -export declare function resolveElementName(explicit: string | undefined, caller: string | null, fallback: string): string; export declare abstract class BasePage { protected el(selector: string, name?: string): Cypress.Chainable>; - protected elContains(text: string, name?: string): Cypress.Chainable; - private extractCallerName; + private extractGetterName; } diff --git a/dist/pages/BasePage.js b/dist/pages/BasePage.js index 3e92d3c..913e2e9 100644 --- a/dist/pages/BasePage.js +++ b/dist/pages/BasePage.js @@ -2,20 +2,13 @@ /// Object.defineProperty(exports, "__esModule", { value: true }); exports.BasePage = void 0; -exports.resolveElementName = resolveElementName; -function resolveElementName(explicit, caller, fallback) { - return explicit || caller || fallback; -} class BasePage { el(selector, name) { - cy.log(`🔹 ${resolveElementName(name, this.extractCallerName(), selector)}`); + const logName = name || this.extractGetterName() || selector; + cy.log(logName); return cy.get(selector); } - elContains(text, name) { - cy.log(`🔹 ${resolveElementName(name, this.extractCallerName(), text)}`); - return cy.contains(text); - } - extractCallerName() { + extractGetterName() { var _a, _b; const stack = new Error().stack; if (!stack) diff --git a/package.json b/package.json index 63d1341..273fa5b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cypress-ncatestify-plugin", - "version": "2.1.70", + "version": "2.1.71", "description": "NCA TESTIFY commands and tests", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/readme.md b/readme.md index 9056bc0..566eada 100644 --- a/readme.md +++ b/readme.md @@ -76,32 +76,41 @@ That's it! You now have professional website tests. --- -## Page Object Model with BasePage +## Semantic Element Logging -For larger projects, organize your tests using the **Page Object Model**. This makes tests more readable and maintainable. +### The Problem -### Why use BasePage? +Cypress logs show CSS selectors which are hard to understand: -Instead of seeing cryptic CSS selectors in your test logs: ``` -cy.get('#ext-comp-1234') // What is this? +cy.get('#ext-comp-1234') // What element is this? +cy.get('.btn-primary') // Which button? ``` -You see meaningful names: -``` -usernameInput // Much clearer! +### The Solution: cy.ttEl + +Use `cy.ttEl(selector, name)` for readable logs: + +```ts +cy.ttEl('#ext-comp-1234', 'usernameInput') // Logs: usernameInput +cy.ttEl('.btn-primary', 'submitButton') // Logs: submitButton ``` -### How to use BasePage +Now your test logs show meaningful names instead of selectors. + +--- + +## Page Object Model with BasePage + +For larger projects, use the **Page Object Model** pattern with `BasePage`. -**1. Create a Page class:** +The getter name automatically becomes the log name: ```ts // cypress/pages/LoginPage.ts import { BasePage } from 'cypress-ncatestify-plugin' export class LoginPage extends BasePage { - // Define your page elements as getters get usernameInput() { return this.el('#username') // Logs: usernameInput } @@ -111,10 +120,9 @@ export class LoginPage extends BasePage { } get submitButton() { - return this.elContains('Login') // Logs: submitButton + return this.el('button[type="submit"]') // Logs: submitButton } - // Create reusable actions login(user: string, pass: string) { this.usernameInput.type(user) this.passwordInput.type(pass) @@ -123,10 +131,9 @@ export class LoginPage extends BasePage { } ``` -**2. Use in your tests:** +**Use in tests:** ```ts -// cypress/e2e/login.cy.ts import { LoginPage } from '../pages/LoginPage' const loginPage = new LoginPage() @@ -135,24 +142,22 @@ describe('Login', () => { it('logs in successfully', () => { cy.visit('/login') loginPage.login('admin', 'secret') - cy.url().should('include', '/dashboard') }) }) ``` -### BasePage Methods - -| Method | Description | Example | -|--------|-------------|---------| -| `this.el(selector)` | Find element by CSS selector | `this.el('#email')` | -| `this.el(selector, name)` | Find element with custom log name | `this.el('#x1', 'email')` | -| `this.elContains(text)` | Find element containing text | `this.elContains('Submit')` | -| `this.elContains(text, name)` | Find by text with custom log name | `this.elContains('OK', 'confirm')` | - --- ## All Commands Reference +### Element Selection + +```ts +// Select element with semantic logging +cy.ttEl('#username', 'usernameInput') // Logs: usernameInput +cy.ttEl('h1') // Logs: h1 +``` + ### Link Validation ```ts diff --git a/src/commands.ts b/src/commands.ts index 71e8ece..6cc93b2 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -19,7 +19,9 @@ import { ttValidateLanguageTag } from './commands/tt-validate-language-tag' import { ttValidateNoGoogleServices } from './commands/tt-validate-no-google-services' import { ttValidatePageContent } from './commands/tt-validate-page-content' import { ttValidateSubpagesAndImages } from './commands/tt-validate-subpages-and-images' +import { ttEl } from './commands/tt-el' +Cypress.Commands.add('ttEl', ttEl) Cypress.Commands.add('ttAccessibility', ttAccessibility) Cypress.Commands.add('ttClickIfElementExist', ttClickIfElementExist) Cypress.Commands.add('ttCookieAllAcceptClick', ttCookieAllAcceptClick) diff --git a/src/commands/tt-el.ts b/src/commands/tt-el.ts new file mode 100644 index 0000000..92ed544 --- /dev/null +++ b/src/commands/tt-el.ts @@ -0,0 +1,7 @@ +export const ttEl = ( + selector: string, + name?: string +): Cypress.Chainable> => { + cy.log(name || selector) + return cy.get(selector) +} diff --git a/src/commands/tt-only-one-h1.ts b/src/commands/tt-only-one-h1.ts index a890b03..47d7e0a 100644 --- a/src/commands/tt-only-one-h1.ts +++ b/src/commands/tt-only-one-h1.ts @@ -1,4 +1,5 @@ export const ttOnlyOneH1 = () => { cy.log('ttOnlyOneH1 - NCA TESTIFY') - cy.get('h1').its('length').should('eq', 1) + // @ts-ignore - Custom command registered at runtime + cy.ttEl('h1', 'h1Heading').its('length').should('eq', 1) } diff --git a/src/commands/tt-validate-language-tag.ts b/src/commands/tt-validate-language-tag.ts index 81fcb23..7481491 100644 --- a/src/commands/tt-validate-language-tag.ts +++ b/src/commands/tt-validate-language-tag.ts @@ -1,11 +1,11 @@ export const ttValidateLanguageTag = (language: string = 'de'): void => { cy.log('ttValidateLanguageTag - NCA TESTIFY') - cy.get('html').should('have.attr', 'lang') - cy.get('html') + // @ts-ignore - Custom command registered at runtime + cy.ttEl('html', 'htmlElement').should('have.attr', 'lang') + // @ts-ignore - Custom command registered at runtime + cy.ttEl('html', 'htmlElement') .invoke('attr', 'lang') - .then((langTag) => { - cy.wrap(langTag).then((lang) => { - expect(lang.toLowerCase()).to.contain(language) - }) + .then((langTag: string) => { + expect(langTag.toLowerCase()).to.contain(language) }) } diff --git a/src/index.d.ts b/src/index.d.ts index 36e95a7..ec7fda0 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -9,6 +9,7 @@ interface DomainMappingConfig { declare namespace Cypress { interface Chainable { + ttEl(selector: string, name?: string): Chainable> ttAccessibility(context?: any, options?: any): Chainable ttClickIfElementExist(element: string): Chainable ttCookieAllAcceptClick(): Chainable @@ -47,8 +48,4 @@ export abstract class BasePage { selector: string, name?: string ): Cypress.Chainable> - protected elContains( - text: string, - name?: string - ): Cypress.Chainable> } diff --git a/src/pages/BasePage.ts b/src/pages/BasePage.ts index 3a89a74..e78ac01 100644 --- a/src/pages/BasePage.ts +++ b/src/pages/BasePage.ts @@ -1,25 +1,13 @@ /// -export function resolveElementName( - explicit: string | undefined, - caller: string | null, - fallback: string -): string { - return explicit || caller || fallback -} - export abstract class BasePage { protected el(selector: string, name?: string) { - cy.log(`🔹 ${resolveElementName(name, this.extractCallerName(), selector)}`) + const logName = name || this.extractGetterName() || selector + cy.log(logName) return cy.get(selector) } - protected elContains(text: string, name?: string) { - cy.log(`🔹 ${resolveElementName(name, this.extractCallerName(), text)}`) - return cy.contains(text) - } - - private extractCallerName(): string | null { + private extractGetterName(): string | null { const stack = new Error().stack if (!stack) return null return stack.split('\n')[3]?.match(/get (\w+)/)?.[1] || null diff --git a/update.md b/update.md index 33187f7..cfce18e 100644 --- a/update.md +++ b/update.md @@ -1,13 +1,17 @@ # Update md file +2.1.71 + +- Add cy.ttEl(selector, name) command for semantic element logging +- BasePage auto-extracts getter name for logging (no name parameter needed) +- Plugin commands (tt-only-one-h1, tt-validate-language-tag) now use cy.ttEl +- Remove elContains method from BasePage + 2.1.70 - Add BasePage abstract class for Page Object Model with semantic logging -- Implement el(selector, name?) and elContains(text, name?) protected methods -- Auto-extract getter names from stack trace for readable Cypress logs -- Shows semantic names (e.g., usernameInput) instead of CSS selectors -- Export resolveElementName pure function for testability -- Add unit tests with table-driven test pattern +- Implement el(selector, name?) protected method +- Shows semantic names instead of CSS selectors - Add integration tests for BasePage functionality 2.1.69