Skip to content

refactor(cdk/testing): rename WebDriverHarnessEnvironment to SeleniumWebDriverHarnessEnvironment #22588

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 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/cdk/config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CDK_ENTRYPOINTS = [
"testing",
"testing/protractor",
"testing/testbed",
"testing/webdriver",
"testing/selenium-webdriver",
]

# List of all entry-point targets of the Angular Material package.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ load("//tools:defaults.bzl", "ts_library")
package(default_visibility = ["//visibility:public"])

ts_library(
name = "webdriver",
name = "selenium-webdriver",
srcs = glob(
["**/*.ts"],
exclude = ["**/*.spec.ts"],
),
module_name = "@angular/cdk/testing/webdriver",
module_name = "@angular/cdk/testing/selenium-webdriver",
deps = [
"//src/cdk/testing",
"@npm//@types/selenium-webdriver",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
* found in the LICENSE file at https://angular.io/license
*/

export * from './webdriver-element';
export * from './webdriver-harness-environment';
export * from './selenium-web-driver-element';
export * from './selenium-web-driver-harness-environment';
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
TextOptions
} from '@angular/cdk/testing';
import * as webdriver from 'selenium-webdriver';
import {getWebDriverModifierKeys, webDriverKeyMap} from './webdriver-keys';
import {getSeleniumWebDriverModifierKeys, seleniumWebDriverKeyMap} from './selenium-webdriver-keys';

/** A `TestElement` implementation for WebDriver. */
export class WebDriverElement implements TestElement {
export class SeleniumWebDriverElement implements TestElement {
constructor(
readonly element: () => webdriver.WebElement,
private _stabilize: () => Promise<void>) {}
Expand Down Expand Up @@ -116,8 +116,8 @@ export class WebDriverElement implements TestElement {
rest = modifiersAndKeys;
}

const modifierKeys = getWebDriverModifierKeys(modifiers);
const keys = rest.map(k => typeof k === 'string' ? k.split('') : [webDriverKeyMap[k]])
const modifierKeys = getSeleniumWebDriverModifierKeys(modifiers);
const keys = rest.map(k => typeof k === 'string' ? k.split('') : [seleniumWebDriverKeyMap[k]])
.reduce((arr, k) => arr.concat(k), [])
// webdriver.Key.chord doesn't work well with geckodriver (mozilla/geckodriver#1502),
// so avoid it if no modifier keys are required.
Expand Down Expand Up @@ -246,7 +246,7 @@ export class WebDriverElement implements TestElement {
if (args.length && typeof args[args.length - 1] === 'object') {
modifiers = args.pop() as ModifierKeys;
}
const modifierKeys = getWebDriverModifierKeys(modifiers);
const modifierKeys = getSeleniumWebDriverModifierKeys(modifiers);

// Omitting the offset argument to mouseMove results in clicking the center.
// This is the default behavior we want, so we use an empty array of offsetArgs if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {HarnessEnvironment, HarnessLoader, TestElement} from '@angular/cdk/testing';
import * as webdriver from 'selenium-webdriver';
import {WebDriverElement} from './webdriver-element';
import {SeleniumWebDriverElement} from './selenium-web-driver-element';

/**
* An Angular framework stabilizer function that takes a callback and calls it when the application
Expand Down Expand Up @@ -67,7 +67,8 @@ export async function waitForAngularReady(wd: webdriver.WebDriver) {
}

/** A `HarnessEnvironment` implementation for WebDriver. */
export class WebDriverHarnessEnvironment extends HarnessEnvironment<() => webdriver.WebElement> {
export class SeleniumWebDriverHarnessEnvironment extends
HarnessEnvironment<() => webdriver.WebElement> {
/** The options for this environment. */
private _options: WebDriverHarnessEnvironmentOptions;

Expand All @@ -79,7 +80,7 @@ export class WebDriverHarnessEnvironment extends HarnessEnvironment<() => webdri

/** Gets the ElementFinder corresponding to the given TestElement. */
static getNativeElement(el: TestElement): webdriver.WebElement {
if (el instanceof WebDriverElement) {
if (el instanceof SeleniumWebDriverElement) {
return el.element();
}
throw Error('This TestElement was not created by the WebDriverHarnessEnvironment');
Expand All @@ -88,7 +89,7 @@ export class WebDriverHarnessEnvironment extends HarnessEnvironment<() => webdri
/** Creates a `HarnessLoader` rooted at the document root. */
static loader(driver: webdriver.WebDriver, options?: WebDriverHarnessEnvironmentOptions):
HarnessLoader {
return new WebDriverHarnessEnvironment(
return new SeleniumWebDriverHarnessEnvironment(
() => driver.findElement(webdriver.By.css('body')), options);
}

Expand All @@ -114,13 +115,13 @@ export class WebDriverHarnessEnvironment extends HarnessEnvironment<() => webdri

/** Creates a `TestElement` from a raw element. */
protected createTestElement(element: () => webdriver.WebElement): TestElement {
return new WebDriverElement(element, () => this.forceStabilize());
return new SeleniumWebDriverElement(element, () => this.forceStabilize());
}

/** Creates a `HarnessLoader` rooted at the given raw element. */
protected createEnvironment(element: () => webdriver.WebElement):
HarnessEnvironment<() => webdriver.WebElement> {
return new WebDriverHarnessEnvironment(element, this._options);
return new SeleniumWebDriverHarnessEnvironment(element, this._options);
}

// Note: This seems to be working, though we may need to re-evaluate if we encounter issues with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as webdriver from 'selenium-webdriver';
* Maps the `TestKey` constants to WebDriver's `webdriver.Key` constants.
* See https://github.com/SeleniumHQ/selenium/blob/trunk/javascript/webdriver/key.js#L29
*/
export const webDriverKeyMap = {
export const seleniumWebDriverKeyMap = {
[TestKey.BACKSPACE]: webdriver.Key.BACK_SPACE,
[TestKey.TAB]: webdriver.Key.TAB,
[TestKey.ENTER]: webdriver.Key.ENTER,
Expand Down Expand Up @@ -47,7 +47,7 @@ export const webDriverKeyMap = {
};

/** Gets a list of WebDriver `Key`s for the given `ModifierKeys`. */
export function getWebDriverModifierKeys(modifiers: ModifierKeys): string[] {
export function getSeleniumWebDriverModifierKeys(modifiers: ModifierKeys): string[] {
const result: string[] = [];
if (modifiers.control) {
result.push(webdriver.Key.CONTROL);
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/testing/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ ts_library(
":cross_environment_specs",
":test_harnesses",
"//src/cdk/testing",
"//src/cdk/testing/webdriver",
"//src/cdk/testing/selenium-webdriver",
"@npm//@bazel/runfiles",
"@npm//@types/jasmine",
"@npm//@types/node",
Expand Down
30 changes: 18 additions & 12 deletions src/cdk/testing/tests/webdriver.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {runfiles} from '@bazel/runfiles';
import {HarnessLoader, parallel} from '@angular/cdk/testing';
import {waitForAngularReady, WebDriverHarnessEnvironment} from '@angular/cdk/testing/webdriver';
import {
SeleniumWebDriverHarnessEnvironment,
waitForAngularReady
} from '@angular/cdk/testing/selenium-webdriver';
import {runfiles} from '@bazel/runfiles';
import * as webdriver from 'selenium-webdriver';
import {crossEnvironmentSpecs} from './cross-environment.spec';
import {MainComponentHarness} from './harnesses/main-component-harness';
Expand Down Expand Up @@ -70,7 +73,7 @@ describe('WebDriverHarnessEnvironment', () => {
let loader: HarnessLoader;

beforeEach(() => {
loader = WebDriverHarnessEnvironment.loader(wd);
loader = SeleniumWebDriverHarnessEnvironment.loader(wd);
});

it('should create HarnessLoader from WebDriverHarnessEnvironment', () => {
Expand All @@ -82,7 +85,8 @@ describe('WebDriverHarnessEnvironment', () => {
let harness: MainComponentHarness;

beforeEach(async () => {
harness = await WebDriverHarnessEnvironment.loader(wd).getHarness(MainComponentHarness);
harness = await SeleniumWebDriverHarnessEnvironment.loader(wd)
.getHarness(MainComponentHarness);
});

it('can get elements outside of host', async () => {
Expand All @@ -99,38 +103,40 @@ describe('WebDriverHarnessEnvironment', () => {
});

it('should be able to retrieve the WebElement from a WebDriverElement', async () => {
const element = WebDriverHarnessEnvironment.getNativeElement(await harness.host());
const element = SeleniumWebDriverHarnessEnvironment.getNativeElement(await harness.host());
expect(await element.getTagName()).toBe('test-main');
});
});

describe('shadow DOM interaction', () => {
it('should not pierce shadow boundary by default', async () => {
const harness = await WebDriverHarnessEnvironment.loader(wd)
const harness = await SeleniumWebDriverHarnessEnvironment.loader(wd)
.getHarness(MainComponentHarness);
expect(await harness.shadows()).toEqual([]);
});

it('should pierce shadow boundary when using piercing query', async () => {
const harness = await WebDriverHarnessEnvironment.loader(wd, {queryFn: piercingQueryFn})
.getHarness(MainComponentHarness);
const harness =
await SeleniumWebDriverHarnessEnvironment.loader(wd, {queryFn: piercingQueryFn})
.getHarness(MainComponentHarness);
const shadows = await harness.shadows();
expect(await parallel(() => {
return shadows.map(el => el.text());
})).toEqual(['Shadow 1', 'Shadow 2']);
});

it('should allow querying across shadow boundary', async () => {
const harness = await WebDriverHarnessEnvironment.loader(wd, {queryFn: piercingQueryFn})
.getHarness(MainComponentHarness);
const harness =
await SeleniumWebDriverHarnessEnvironment.loader(wd, {queryFn: piercingQueryFn})
.getHarness(MainComponentHarness);
expect(await (await harness.deepShadow()).text()).toBe('Shadow 2');
});
});
});

describe('environment independent', () => crossEnvironmentSpecs(
() => WebDriverHarnessEnvironment.loader(wd),
() => WebDriverHarnessEnvironment.loader(wd).getHarness(MainComponentHarness),
() => SeleniumWebDriverHarnessEnvironment.loader(wd),
() => SeleniumWebDriverHarnessEnvironment.loader(wd).getHarness(MainComponentHarness),
async () => (await activeElement()).getAttribute('id'),
));
});
2 changes: 1 addition & 1 deletion src/cdk/tsconfig-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"exclude": [
"testing/protractor/**.ts",
"testing/private/e2e/**.ts",
"testing/webdriver/**.ts",
"testing/selenium-webdriver/**.ts",
"**/schematics/**/*.ts",
"**/*.e2e.spec.ts"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export declare function waitForAngularReady(wd: webdriver.WebDriver): Promise<void>;

export declare class WebDriverElement implements TestElement {
export declare class SeleniumWebDriverElement implements TestElement {
readonly element: () => webdriver.WebElement;
constructor(element: () => webdriver.WebElement, _stabilize: () => Promise<void>);
blur(): Promise<void>;
Expand All @@ -27,7 +25,7 @@ export declare class WebDriverElement implements TestElement {
text(options?: TextOptions): Promise<string>;
}

export declare class WebDriverHarnessEnvironment extends HarnessEnvironment<() => webdriver.WebElement> {
export declare class SeleniumWebDriverHarnessEnvironment extends HarnessEnvironment<() => webdriver.WebElement> {
protected constructor(rawRootElement: () => webdriver.WebElement, options?: WebDriverHarnessEnvironmentOptions);
protected createEnvironment(element: () => webdriver.WebElement): HarnessEnvironment<() => webdriver.WebElement>;
protected createTestElement(element: () => webdriver.WebElement): TestElement;
Expand All @@ -39,6 +37,8 @@ export declare class WebDriverHarnessEnvironment extends HarnessEnvironment<() =
static loader(driver: webdriver.WebDriver, options?: WebDriverHarnessEnvironmentOptions): HarnessLoader;
}

export declare function waitForAngularReady(wd: webdriver.WebDriver): Promise<void>;

export interface WebDriverHarnessEnvironmentOptions {
queryFn: (selector: string, root: () => webdriver.WebElement) => Promise<webdriver.WebElement[]>;
}
2 changes: 1 addition & 1 deletion tools/public_api_guard/generate-guard-tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def generate_test_targets(golden_files):
],
golden = "angular_material/tools/public_api_guard/%s" % golden_file,
use_angular_tag_rules = False,
# Required for the `youtube-player`, `google-maps`, and `cdk/testing/webdriver`
# Required for the `youtube-player`, `google-maps`, and `cdk/testing/selenium-webdriver`
# packages. "i0" is generated by ngtsc for creating directive, module definitions.
allow_module_identifiers = ["YT", "google", "i0", "webdriver"],
)