Skip to content

Commit a9ca95e

Browse files
authored
fix(cdk/testing): Make harnesses click on the center of the element by default (#19212)
* fix(cdk/testing): Make harnesses click on the center of the element by default This is Protractor's default behavior. Prior to this PR we had been clicking the top-left corner, but that didn't trigger the click event in Protractor. * address feedback * clarifying comment * update api goldens
1 parent 15efb67 commit a9ca95e

File tree

8 files changed

+62
-12
lines changed

8 files changed

+62
-12
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,14 @@ export class ProtractorElement implements TestElement {
7373
return this.element.clear();
7474
}
7575

76-
async click(relativeX = 0, relativeY = 0): Promise<void> {
76+
async click(...args: number[]): Promise<void> {
77+
// Omitting the offset argument to mouseMove results in clicking the center.
78+
// This is the default behavior we want, so we use an empty array of offsetArgs if no args are
79+
// passed to this method.
80+
const offsetArgs = args.length ? [{x: args[0], y: args[1]}] : [];
81+
7782
await browser.actions()
78-
.mouseMove(await this.element.getWebElement(), {x: relativeX, y: relativeY})
83+
.mouseMove(await this.element.getWebElement(), ...offsetArgs)
7984
.click()
8085
.perform();
8186
}

src/cdk/testing/test-element.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ export interface TestElement {
6767
/** Clear the element's input (for input and textarea elements only). */
6868
clear(): Promise<void>;
6969

70+
/** Click the element at the element's center. */
71+
click(): Promise<void>;
72+
7073
/**
71-
* Click the element.
74+
* Click the element at the specified coordinates relative to the top-left of the element.
7275
* @param relativeX Coordinate within the element, along the X-axis at which to click.
7376
* @param relativeY Coordinate within the element, along the Y-axis at which to click.
7477
*/
75-
click(relativeX?: number, relativeY?: number): Promise<void>;
78+
click(relativeX: number, relativeY: number): Promise<void>;
7679

7780
/** Focus the element. */
7881
focus(): Promise<void>;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ export class UnitTestElement implements TestElement {
7171
await this._stabilize();
7272
}
7373

74-
async click(relativeX = 0, relativeY = 0): Promise<void> {
75-
await this._stabilize();
76-
const {left, top} = this.element.getBoundingClientRect();
74+
async click(...args: number[]): Promise<void> {
75+
const {left, top, width, height} = await this.getDimensions();
76+
const relativeX = args.length ? args[0] : width / 2;
77+
const relativeY = args.length ? args[1] : height / 2;
78+
7779
// Round the computed click position as decimal pixels are not
7880
// supported by mouse events and could lead to unexpected results.
7981
const clientX = Math.round(left + relativeX);

src/material/button/testing/BUILD.bazel

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library")
1+
load("//src/e2e-app:test_suite.bzl", "e2e_test_suite")
2+
load("//tools:defaults.bzl", "ng_e2e_test_library", "ng_test_library", "ng_web_test_suite", "ts_library")
23

34
package(default_visibility = ["//visibility:public"])
45

@@ -38,7 +39,10 @@ ng_test_library(
3839
name = "unit_tests_lib",
3940
srcs = glob(
4041
["**/*.spec.ts"],
41-
exclude = ["shared.spec.ts"],
42+
exclude = [
43+
"**/*.e2e.spec.ts",
44+
"shared.spec.ts",
45+
],
4246
),
4347
deps = [
4448
":harness_tests_lib",
@@ -51,3 +55,21 @@ ng_web_test_suite(
5155
name = "unit_tests",
5256
deps = [":unit_tests_lib"],
5357
)
58+
59+
ng_e2e_test_library(
60+
name = "e2e_test_sources",
61+
srcs = glob(["**/*.e2e.spec.ts"]),
62+
deps = [
63+
"//src/cdk/testing/private/e2e",
64+
"//src/cdk/testing/protractor",
65+
"//src/material/button/testing",
66+
],
67+
)
68+
69+
e2e_test_suite(
70+
name = "e2e_tests",
71+
deps = [
72+
":e2e_test_sources",
73+
"//src/cdk/testing/private/e2e",
74+
],
75+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {ProtractorHarnessEnvironment} from '@angular/cdk/testing/protractor';
2+
import {MatButtonHarness} from '@angular/material/button/testing';
3+
import {browser} from 'protractor';
4+
5+
describe('button harness', () => {
6+
beforeEach(async () => await browser.get('/button'));
7+
8+
it('can click button', async () => {
9+
const loader = ProtractorHarnessEnvironment.loader();
10+
const disableToggle = await loader.getHarness(MatButtonHarness.with({text: 'Disable buttons'}));
11+
const testButton = await loader.getHarness(MatButtonHarness.with({selector: '#test-button'}));
12+
13+
expect(await testButton.isDisabled()).toBe(false);
14+
await disableToggle.click();
15+
expect(await testButton.isDisabled()).toBe(true);
16+
});
17+
});

tools/public_api_guard/cdk/testing.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ export interface ModifierKeys {
107107
export interface TestElement {
108108
blur(): Promise<void>;
109109
clear(): Promise<void>;
110-
click(relativeX?: number, relativeY?: number): Promise<void>;
110+
click(): Promise<void>;
111+
click(relativeX: number, relativeY: number): Promise<void>;
111112
focus(): Promise<void>;
112113
getAttribute(name: string): Promise<string | null>;
113114
getCssValue(property: string): Promise<string>;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export declare class ProtractorElement implements TestElement {
33
constructor(element: ElementFinder);
44
blur(): Promise<void>;
55
clear(): Promise<void>;
6-
click(relativeX?: number, relativeY?: number): Promise<void>;
6+
click(...args: number[]): Promise<void>;
77
focus(): Promise<void>;
88
getAttribute(name: string): Promise<string | null>;
99
getCssValue(property: string): Promise<string>;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export declare class UnitTestElement implements TestElement {
2020
constructor(element: Element, _stabilize: () => Promise<void>);
2121
blur(): Promise<void>;
2222
clear(): Promise<void>;
23-
click(relativeX?: number, relativeY?: number): Promise<void>;
23+
click(...args: number[]): Promise<void>;
2424
focus(): Promise<void>;
2525
getAttribute(name: string): Promise<string | null>;
2626
getCssValue(property: string): Promise<string>;

0 commit comments

Comments
 (0)