Skip to content

Commit ae96075

Browse files
committed
feat(cdk-experimental): method to get property value of test element
Most of the time harnesses are able to use `getAttribute` to retrieve data from DOM elements, but there are edge-cases where it's not correct to retrieve data through the attribute. For example: Consider an `input` element which has a `value` attribute set to the initial input value. Now when the value of the input changes through interaction, the `value` attribute is not refreshed as it's a static attribute. In that case, the harness should retrieve the value by reading the DOM element property. This is needed in order to implement the `MatInputHarness`.
1 parent 8e321ae commit ae96075

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export class ProtractorElement implements TestElement {
5151
return this.element.getAttribute(name);
5252
}
5353

54+
async getProperty(name: string): Promise<any> {
55+
return browser.executeScript<any|undefined>(`return arguments[0]['${name}']`, this.element);
56+
}
57+
5458
async hasClass(name: string): Promise<boolean> {
5559
const classes = (await this.getAttribute('class')) || '';
5660
return new Set(classes.split(/\s+/).filter(c => c)).has(name);

src/cdk-experimental/testing/test-element.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export interface TestElement {
4444
*/
4545
getAttribute(name: string): Promise<string | null>;
4646

47+
/** Gets the value of the given property from the element. */
48+
getProperty(name: string): Promise<any | undefined>;
49+
4750
/** Checks whether the element has the given class. */
4851
hasClass(name: string): Promise<boolean>;
4952
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ export class UnitTestElement implements TestElement {
101101
return value;
102102
}
103103

104+
async getProperty(name: string): Promise<any|undefined> {
105+
await this._stabilize();
106+
return (this.element as any)[name];
107+
}
108+
104109
async hasClass(name: string): Promise<boolean> {
105110
await this._stabilize();
106111
return this.element.classList.contains(name);

src/cdk-experimental/testing/tests/protractor.e2e.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ describe('ProtractorHarnessEnvironment', () => {
215215
expect(await memo.getAttribute('value')).toBe(memoStr);
216216
});
217217

218+
it('should be able to getProperty', async () => {
219+
const memo = await harness.memo();
220+
await memo.sendKeys('new-value');
221+
expect(await memo.getProperty('value')).toBe('new-value');
222+
});
223+
218224
it('should be able to getCssValue', async () => {
219225
const title = await harness.title();
220226
expect(await title.getCssValue('height')).toBe('50px');

src/cdk-experimental/testing/tests/testbed.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ describe('TestbedHarnessEnvironment', () => {
234234
expect(await memo.getAttribute('value')).toBe(memoStr);
235235
});
236236

237+
it('should be able to getProperty', async () => {
238+
const memo = await harness.memo();
239+
await memo.sendKeys('new-value');
240+
expect(await memo.getProperty('value')).toBe('new-value');
241+
});
242+
237243
it('should be able to getCssValue', async () => {
238244
const title = await harness.title();
239245
expect(await title.getCssValue('height')).toBe('50px');

0 commit comments

Comments
 (0)