Skip to content
Merged

V 71 #141

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions __test__/BasePage.test.ts

This file was deleted.

18 changes: 5 additions & 13 deletions cypress/e2e/basepage/basepage.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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')
})
})
2 changes: 2 additions & 0 deletions dist/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions dist/commands/tt-el.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const ttEl: (selector: string, name?: string) => Cypress.Chainable<JQuery<HTMLElement>>;
8 changes: 8 additions & 0 deletions dist/commands/tt-el.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion dist/commands/tt-only-one-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
10 changes: 5 additions & 5 deletions dist/commands/tt-validate-language-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 1 addition & 4 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface DomainMappingConfig {

declare namespace Cypress {
interface Chainable<Subject = any> {
ttEl(selector: string, name?: string): Chainable<JQuery<HTMLElement>>
ttAccessibility(context?: any, options?: any): Chainable<Subject>
ttClickIfElementExist(element: string): Chainable<Subject>
ttCookieAllAcceptClick(): Chainable<Subject>
Expand Down Expand Up @@ -47,8 +48,4 @@ export abstract class BasePage {
selector: string,
name?: string
): Cypress.Chainable<JQuery<HTMLElement>>
protected elContains(
text: string,
name?: string
): Cypress.Chainable<JQuery<HTMLElement>>
}
4 changes: 1 addition & 3 deletions dist/pages/BasePage.d.ts
Original file line number Diff line number Diff line change
@@ -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<JQuery<HTMLElement>>;
protected elContains(text: string, name?: string): Cypress.Chainable<undefined>;
private extractCallerName;
private extractGetterName;
}
13 changes: 3 additions & 10 deletions dist/pages/BasePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@
/// <reference types="cypress" />
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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
55 changes: 30 additions & 25 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/commands/tt-el.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const ttEl = (
selector: string,
name?: string
): Cypress.Chainable<JQuery<HTMLElement>> => {
cy.log(name || selector)
return cy.get(selector)
}
3 changes: 2 additions & 1 deletion src/commands/tt-only-one-h1.ts
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 6 additions & 6 deletions src/commands/tt-validate-language-tag.ts
Original file line number Diff line number Diff line change
@@ -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)
})
}
5 changes: 1 addition & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface DomainMappingConfig {

declare namespace Cypress {
interface Chainable<Subject = any> {
ttEl(selector: string, name?: string): Chainable<JQuery<HTMLElement>>
ttAccessibility(context?: any, options?: any): Chainable<Subject>
ttClickIfElementExist(element: string): Chainable<Subject>
ttCookieAllAcceptClick(): Chainable<Subject>
Expand Down Expand Up @@ -47,8 +48,4 @@ export abstract class BasePage {
selector: string,
name?: string
): Cypress.Chainable<JQuery<HTMLElement>>
protected elContains(
text: string,
name?: string
): Cypress.Chainable<JQuery<HTMLElement>>
}
18 changes: 3 additions & 15 deletions src/pages/BasePage.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
/// <reference types="cypress" />

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
Expand Down
14 changes: 9 additions & 5 deletions update.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading