Skip to content

Commit cbabed5

Browse files
committed
add implementation for UnitTestElement and ProtractorElement
1 parent 5f31c05 commit cbabed5

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

src/cdk/testing/protractor/protractor-element.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {ElementDimensions, ModifierKeys, TestElement, TestKey} from '@angular/cdk/testing';
10-
import {browser, ElementFinder, Key} from 'protractor';
10+
import {browser, by, ElementFinder, Key} from 'protractor';
1111

1212
/** Maps the `TestKey` constants to Protractor's `Key` constants. */
1313
const keyMap = {
@@ -129,8 +129,14 @@ export class ProtractorElement implements TestElement {
129129
return this.element.sendKeys(...keys);
130130
}
131131

132-
async text(): Promise<string> {
133-
return this.element.getText();
132+
async text(options?: {excludes?: string}): Promise<string> {
133+
if (options?.excludes) {
134+
return browser.executeScript(`var clone = arguments[0].cloneNode(true);
135+
var remove = clone.querySelectorAll(arguments[1]);
136+
remove.forEach(n => n.remove()); return clone.textContent`, this.element, options.excludes);
137+
} else {
138+
return this.element.getText();
139+
}
134140
}
135141

136142
async getAttribute(name: string): Promise<string|null> {

src/cdk/testing/test-element.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ export interface TestElement {
101101
*/
102102
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
103103

104-
/** Gets the text from the element. Optionally exclude specific selectors. */
105-
text(options?: {excludes: string}): Promise<string>;
104+
/**
105+
* Gets the text from the element.
106+
* @param options Options that affect what text is included
107+
* exclude: A selector for elements whose text should be excluded from the result.
108+
*/
109+
text(options?: {excludes?: string}): Promise<string>;
106110

107111
/** Gets the value for the given attribute from the element. */
108112
getAttribute(name: string): Promise<string | null>;

src/cdk/testing/testbed/unit-test-element.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,16 @@ export class UnitTestElement implements TestElement {
126126
await this._stabilize();
127127
}
128128

129-
async text(): Promise<string> {
130-
await this._stabilize();
131-
return (this.element.textContent || '').trim();
129+
async text(options?: {excludes?: string}): Promise<string> {
130+
await this._stabilize();
131+
if (options?.excludes) {
132+
const clone = this.element.cloneNode(true);
133+
const exclusions = this.element.querySelectorAll(options.excludes);
134+
exclusions.forEach(n => n.remove());
135+
return (clone.textContent || '').trim();
136+
} else {
137+
return (this.element.textContent || '').trim();
138+
}
132139
}
133140

134141
async getAttribute(name: string): Promise<string|null> {

tools/public_api_guard/cdk/testing/protractor.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export declare class ProtractorElement implements TestElement {
1717
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
1818
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
1919
setInputValue(value: string): Promise<void>;
20-
text(): Promise<string>;
20+
text(options?: {excludes?: string}): Promise<string>;
2121
}
2222

2323
export declare class ProtractorHarnessEnvironment extends HarnessEnvironment<ElementFinder> {

tools/public_api_guard/cdk/testing/testbed.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ export declare class UnitTestElement implements TestElement {
3434
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
3535
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;
3636
setInputValue(value: string): Promise<void>;
37-
text(): Promise<string>;
37+
text(options?: {excludes?: string}): Promise<string>;
3838
}

0 commit comments

Comments
 (0)