Skip to content

Commit 8739a98

Browse files
committed
fix(cdk/testing): protractor element not extracting hidden text
We use Protractor's `getText` method to extract text, but the problem is that it's likely based on `HTMLElement.innerText` which excludes text from hidden elements. These changes switch to using `textContent` to bring the behavior in-line with the `UnitTestElement`.
1 parent ddc2e23 commit 8739a98

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ export class ProtractorElement implements TestElement {
139139
if (options?.exclude) {
140140
return browser.executeScript(_getTextWithExcludedElements, this.element, options.exclude);
141141
}
142-
return this.element.getText();
142+
// We don't go through Protractor's `getText`, because it excludes text from hidden elements.
143+
return browser.executeScript(`return (arguments[0].textContent || '')`, this.element);
143144
}
144145

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

src/cdk/testing/tests/cross-environment.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ export function crossEnvironmentSpecs(
597597
await button.blur();
598598
expect(await button.isFocused()).toBe(false);
599599
});
600+
601+
it('should be able to get the text of a hidden element', async () => {
602+
const hiddenElement = await harness.hidden();
603+
expect(await hiddenElement.text()).toBe('Hello');
604+
});
605+
600606
});
601607
}
602608

src/cdk/testing/tests/harnesses/main-component-harness.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class MainComponentHarness extends ComponentHarness {
9595
readonly hoverTest = this.locatorFor('#hover-box');
9696
readonly customEventBasic = this.locatorFor('#custom-event-basic');
9797
readonly customEventObject = this.locatorFor('#custom-event-object');
98+
readonly hidden = this.locatorFor('.hidden-element');
9899

99100
private _testTools = this.locatorFor(SubComponentHarness);
100101

src/cdk/testing/tests/test-main-component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ <h1 style="height: 100px; width: 200px;">Main Component</h1>
6969
(mouseenter)="isHovering = true"
7070
(mouseleave)="isHovering = false"
7171
style="width: 50px; height: 50px; background: hotpink;"></div>
72+
<div class="hidden-element" style="display: none;">Hello</div>

0 commit comments

Comments
 (0)