Skip to content

feat(cdk/testing): add optional excludes to TestElement text method #20145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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: 13 additions & 3 deletions src/cdk/testing/protractor/protractor-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ElementDimensions, ModifierKeys, TestElement, TestKey} from '@angular/cdk/testing';
import {
ElementDimensions,
getTextWithExcludedElements,
ModifierKeys,
TestElement,
TestKey
} from '@angular/cdk/testing';
import {browser, ElementFinder, Key} from 'protractor';

/** Maps the `TestKey` constants to Protractor's `Key` constants. */
Expand Down Expand Up @@ -129,8 +135,12 @@ export class ProtractorElement implements TestElement {
return this.element.sendKeys(...keys);
}

async text(): Promise<string> {
return this.element.getText();
async text(options?: {excludes?: string}): Promise<string> {
if (options?.excludes) {
return browser.executeScript(getTextWithExcludedElements, this.element, options.excludes);
} else {
return this.element.getText();
}
}

async getAttribute(name: string): Promise<string|null> {
Expand Down
18 changes: 16 additions & 2 deletions src/cdk/testing/test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ export interface TestElement {
*/
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;

/** Gets the text from the element. */
text(): Promise<string>;
/**
* Gets the text from the element.
* @param options Options that affect what text is included
* exclude: A selector for elements whose text should be excluded from the result.
*/
text(options?: {excludes?: string}): Promise<string>;

/** Gets the value for the given attribute from the element. */
getAttribute(name: string): Promise<string | null>;
Expand All @@ -128,3 +132,13 @@ export interface TestElement {
/** Checks whether the element is focused. */
isFocused(): Promise<boolean>;
}

export function getTextWithExcludedElements(element: Element, excludeSelector: string) {
const clone = element.cloneNode(true) as Element;
const exclusions = clone.querySelectorAll(excludeSelector);
for (let i = 0; i < exclusions.length; i++) {
let child = exclusions[i];
child.parentNode?.removeChild(child);
}
return (clone.textContent || '').trim();
}
16 changes: 13 additions & 3 deletions src/cdk/testing/testbed/unit-test-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
*/

import * as keyCodes from '@angular/cdk/keycodes';
import {ElementDimensions, ModifierKeys, TestElement, TestKey} from '@angular/cdk/testing';
import {
ElementDimensions,
getTextWithExcludedElements,
ModifierKeys,
TestElement,
TestKey
} from '@angular/cdk/testing';
import {
clearElement,
dispatchMouseEvent,
Expand Down Expand Up @@ -126,9 +132,13 @@ export class UnitTestElement implements TestElement {
await this._stabilize();
}

async text(): Promise<string> {
async text(options?: {excludes?: string}): Promise<string> {
await this._stabilize();
return (this.element.textContent || '').trim();
if (options?.excludes) {
return getTextWithExcludedElements(this.element, options.excludes);
} else {
return (this.element.textContent || '').trim();
}
}

async getAttribute(name: string): Promise<string|null> {
Expand Down
9 changes: 9 additions & 0 deletions src/cdk/testing/tests/cross-environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,15 @@ export function crossEnvironmentSpecs(
expect(await button.matchesSelector('button:disabled')).toBe(false);
});


it('should get correct text excluding certain selectors', async () => {
const results = await harness.subcomponentAndSpecialHarnesses();
const subHarnessHost = await results[0].host();

expect(await subHarnessHost.text({excludes: 'h2'})).toBe('ProtractorTestBedOther');
expect(await subHarnessHost.text({excludes: 'li'})).toBe('List of test tools');
});

it('should get TestElements and ComponentHarnesses', async () => {
const results = await harness.subcomponentHarnessesAndElements();
expect(results.length).toBe(5);
Expand Down
8 changes: 8 additions & 0 deletions src/cdk/testing/tests/protractor.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ describe('ProtractorHarnessEnvironment', () => {
const globalEl = await harness.globalEl();
expect(await globalEl.text()).toBe('I am a sibling!');
});

it('should get correct text excluding certain selectors', async () => {
const results = await harness.subcomponentAndSpecialHarnesses();
const subHarnessHost = await results[0].host();

expect(await subHarnessHost.text({excludes: 'h2'})).toBe('ProtractorTestBedOther');
expect(await subHarnessHost.text({excludes: 'li'})).toBe('List of test tools');
});
});

describe('shadow DOM interaction', () => {
Expand Down
6 changes: 5 additions & 1 deletion tools/public_api_guard/cdk/testing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface ElementDimensions {
width: number;
}

export declare function getTextWithExcludedElements(element: Element, excludeSelector: string): string;

export declare abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
protected rawRootElement: E;
rootElement: TestElement;
Expand Down Expand Up @@ -131,7 +133,9 @@ export interface TestElement {
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
setInputValue?(value: string): Promise<void>;
text(): Promise<string>;
text(options?: {
excludes?: string;
}): Promise<string>;
}

export declare enum TestKey {
Expand Down
4 changes: 3 additions & 1 deletion tools/public_api_guard/cdk/testing/protractor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export declare class ProtractorElement implements TestElement {
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
setInputValue(value: string): Promise<void>;
text(): Promise<string>;
text(options?: {
excludes?: string;
}): Promise<string>;
}

export declare class ProtractorHarnessEnvironment extends HarnessEnvironment<ElementFinder> {
Expand Down
4 changes: 3 additions & 1 deletion tools/public_api_guard/cdk/testing/testbed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ export declare class UnitTestElement implements TestElement {
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
setInputValue(value: string): Promise<void>;
text(): Promise<string>;
text(options?: {
excludes?: string;
}): Promise<string>;
}