[6a3a178] | 1 | import { __awaiter } from 'tslib';
|
---|
| 2 | import { TestKey, _getTextWithExcludedElements, HarnessEnvironment } from '@angular/cdk/testing';
|
---|
| 3 | import { Key, browser, Button, by, element } from 'protractor';
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * @license
|
---|
| 7 | * Copyright Google LLC All Rights Reserved.
|
---|
| 8 | *
|
---|
| 9 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 10 | * found in the LICENSE file at https://angular.io/license
|
---|
| 11 | */
|
---|
| 12 | /** Maps the `TestKey` constants to Protractor's `Key` constants. */
|
---|
| 13 | const keyMap = {
|
---|
| 14 | [TestKey.BACKSPACE]: Key.BACK_SPACE,
|
---|
| 15 | [TestKey.TAB]: Key.TAB,
|
---|
| 16 | [TestKey.ENTER]: Key.ENTER,
|
---|
| 17 | [TestKey.SHIFT]: Key.SHIFT,
|
---|
| 18 | [TestKey.CONTROL]: Key.CONTROL,
|
---|
| 19 | [TestKey.ALT]: Key.ALT,
|
---|
| 20 | [TestKey.ESCAPE]: Key.ESCAPE,
|
---|
| 21 | [TestKey.PAGE_UP]: Key.PAGE_UP,
|
---|
| 22 | [TestKey.PAGE_DOWN]: Key.PAGE_DOWN,
|
---|
| 23 | [TestKey.END]: Key.END,
|
---|
| 24 | [TestKey.HOME]: Key.HOME,
|
---|
| 25 | [TestKey.LEFT_ARROW]: Key.ARROW_LEFT,
|
---|
| 26 | [TestKey.UP_ARROW]: Key.ARROW_UP,
|
---|
| 27 | [TestKey.RIGHT_ARROW]: Key.ARROW_RIGHT,
|
---|
| 28 | [TestKey.DOWN_ARROW]: Key.ARROW_DOWN,
|
---|
| 29 | [TestKey.INSERT]: Key.INSERT,
|
---|
| 30 | [TestKey.DELETE]: Key.DELETE,
|
---|
| 31 | [TestKey.F1]: Key.F1,
|
---|
| 32 | [TestKey.F2]: Key.F2,
|
---|
| 33 | [TestKey.F3]: Key.F3,
|
---|
| 34 | [TestKey.F4]: Key.F4,
|
---|
| 35 | [TestKey.F5]: Key.F5,
|
---|
| 36 | [TestKey.F6]: Key.F6,
|
---|
| 37 | [TestKey.F7]: Key.F7,
|
---|
| 38 | [TestKey.F8]: Key.F8,
|
---|
| 39 | [TestKey.F9]: Key.F9,
|
---|
| 40 | [TestKey.F10]: Key.F10,
|
---|
| 41 | [TestKey.F11]: Key.F11,
|
---|
| 42 | [TestKey.F12]: Key.F12,
|
---|
| 43 | [TestKey.META]: Key.META
|
---|
| 44 | };
|
---|
| 45 | /** Converts a `ModifierKeys` object to a list of Protractor `Key`s. */
|
---|
| 46 | function toProtractorModifierKeys(modifiers) {
|
---|
| 47 | const result = [];
|
---|
| 48 | if (modifiers.control) {
|
---|
| 49 | result.push(Key.CONTROL);
|
---|
| 50 | }
|
---|
| 51 | if (modifiers.alt) {
|
---|
| 52 | result.push(Key.ALT);
|
---|
| 53 | }
|
---|
| 54 | if (modifiers.shift) {
|
---|
| 55 | result.push(Key.SHIFT);
|
---|
| 56 | }
|
---|
| 57 | if (modifiers.meta) {
|
---|
| 58 | result.push(Key.META);
|
---|
| 59 | }
|
---|
| 60 | return result;
|
---|
| 61 | }
|
---|
| 62 | /**
|
---|
| 63 | * A `TestElement` implementation for Protractor.
|
---|
| 64 | * @deprecated
|
---|
| 65 | * @breaking-change 13.0.0
|
---|
| 66 | */
|
---|
| 67 | class ProtractorElement {
|
---|
| 68 | constructor(element) {
|
---|
| 69 | this.element = element;
|
---|
| 70 | }
|
---|
| 71 | /** Blur the element. */
|
---|
| 72 | blur() {
|
---|
| 73 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 74 | return browser.executeScript('arguments[0].blur()', this.element);
|
---|
| 75 | });
|
---|
| 76 | }
|
---|
| 77 | /** Clear the element's input (for input and textarea elements only). */
|
---|
| 78 | clear() {
|
---|
| 79 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 80 | return this.element.clear();
|
---|
| 81 | });
|
---|
| 82 | }
|
---|
| 83 | click(...args) {
|
---|
| 84 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 85 | yield this._dispatchClickEventSequence(args, Button.LEFT);
|
---|
| 86 | });
|
---|
| 87 | }
|
---|
| 88 | rightClick(...args) {
|
---|
| 89 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 90 | yield this._dispatchClickEventSequence(args, Button.RIGHT);
|
---|
| 91 | });
|
---|
| 92 | }
|
---|
| 93 | /** Focus the element. */
|
---|
| 94 | focus() {
|
---|
| 95 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 96 | return browser.executeScript('arguments[0].focus()', this.element);
|
---|
| 97 | });
|
---|
| 98 | }
|
---|
| 99 | /** Get the computed value of the given CSS property for the element. */
|
---|
| 100 | getCssValue(property) {
|
---|
| 101 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 102 | return this.element.getCssValue(property);
|
---|
| 103 | });
|
---|
| 104 | }
|
---|
| 105 | /** Hovers the mouse over the element. */
|
---|
| 106 | hover() {
|
---|
| 107 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 108 | return browser.actions()
|
---|
| 109 | .mouseMove(yield this.element.getWebElement())
|
---|
| 110 | .perform();
|
---|
| 111 | });
|
---|
| 112 | }
|
---|
| 113 | /** Moves the mouse away from the element. */
|
---|
| 114 | mouseAway() {
|
---|
| 115 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 116 | return browser.actions()
|
---|
| 117 | .mouseMove(yield this.element.getWebElement(), { x: -1, y: -1 })
|
---|
| 118 | .perform();
|
---|
| 119 | });
|
---|
| 120 | }
|
---|
| 121 | sendKeys(...modifiersAndKeys) {
|
---|
| 122 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 123 | const first = modifiersAndKeys[0];
|
---|
| 124 | let modifiers;
|
---|
| 125 | let rest;
|
---|
| 126 | if (typeof first !== 'string' && typeof first !== 'number') {
|
---|
| 127 | modifiers = first;
|
---|
| 128 | rest = modifiersAndKeys.slice(1);
|
---|
| 129 | }
|
---|
| 130 | else {
|
---|
| 131 | modifiers = {};
|
---|
| 132 | rest = modifiersAndKeys;
|
---|
| 133 | }
|
---|
| 134 | const modifierKeys = toProtractorModifierKeys(modifiers);
|
---|
| 135 | const keys = rest.map(k => typeof k === 'string' ? k.split('') : [keyMap[k]])
|
---|
| 136 | .reduce((arr, k) => arr.concat(k), [])
|
---|
| 137 | // Key.chord doesn't work well with geckodriver (mozilla/geckodriver#1502),
|
---|
| 138 | // so avoid it if no modifier keys are required.
|
---|
| 139 | .map(k => modifierKeys.length > 0 ? Key.chord(...modifierKeys, k) : k);
|
---|
| 140 | return this.element.sendKeys(...keys);
|
---|
| 141 | });
|
---|
| 142 | }
|
---|
| 143 | /**
|
---|
| 144 | * Gets the text from the element.
|
---|
| 145 | * @param options Options that affect what text is included.
|
---|
| 146 | */
|
---|
| 147 | text(options) {
|
---|
| 148 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 149 | if (options === null || options === void 0 ? void 0 : options.exclude) {
|
---|
| 150 | return browser.executeScript(_getTextWithExcludedElements, this.element, options.exclude);
|
---|
| 151 | }
|
---|
| 152 | // We don't go through Protractor's `getText`, because it excludes text from hidden elements.
|
---|
| 153 | return browser.executeScript(`return (arguments[0].textContent || '').trim()`, this.element);
|
---|
| 154 | });
|
---|
| 155 | }
|
---|
| 156 | /** Gets the value for the given attribute from the element. */
|
---|
| 157 | getAttribute(name) {
|
---|
| 158 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 159 | return browser.executeScript(`return arguments[0].getAttribute(arguments[1])`, this.element, name);
|
---|
| 160 | });
|
---|
| 161 | }
|
---|
| 162 | /** Checks whether the element has the given class. */
|
---|
| 163 | hasClass(name) {
|
---|
| 164 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 165 | const classes = (yield this.getAttribute('class')) || '';
|
---|
| 166 | return new Set(classes.split(/\s+/).filter(c => c)).has(name);
|
---|
| 167 | });
|
---|
| 168 | }
|
---|
| 169 | /** Gets the dimensions of the element. */
|
---|
| 170 | getDimensions() {
|
---|
| 171 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 172 | const { width, height } = yield this.element.getSize();
|
---|
| 173 | const { x: left, y: top } = yield this.element.getLocation();
|
---|
| 174 | return { width, height, left, top };
|
---|
| 175 | });
|
---|
| 176 | }
|
---|
| 177 | /** Gets the value of a property of an element. */
|
---|
| 178 | getProperty(name) {
|
---|
| 179 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 180 | return browser.executeScript(`return arguments[0][arguments[1]]`, this.element, name);
|
---|
| 181 | });
|
---|
| 182 | }
|
---|
| 183 | /** Sets the value of a property of an input. */
|
---|
| 184 | setInputValue(value) {
|
---|
| 185 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 186 | return browser.executeScript(`arguments[0].value = arguments[1]`, this.element, value);
|
---|
| 187 | });
|
---|
| 188 | }
|
---|
| 189 | /** Selects the options at the specified indexes inside of a native `select` element. */
|
---|
| 190 | selectOptions(...optionIndexes) {
|
---|
| 191 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 192 | const options = yield this.element.all(by.css('option'));
|
---|
| 193 | const indexes = new Set(optionIndexes); // Convert to a set to remove duplicates.
|
---|
| 194 | if (options.length && indexes.size) {
|
---|
| 195 | // Reset the value so all the selected states are cleared. We can
|
---|
| 196 | // reuse the input-specific method since the logic is the same.
|
---|
| 197 | yield this.setInputValue('');
|
---|
| 198 | for (let i = 0; i < options.length; i++) {
|
---|
| 199 | if (indexes.has(i)) {
|
---|
| 200 | // We have to hold the control key while clicking on options so that multiple can be
|
---|
| 201 | // selected in multi-selection mode. The key doesn't do anything for single selection.
|
---|
| 202 | yield browser.actions().keyDown(Key.CONTROL).perform();
|
---|
| 203 | yield options[i].click();
|
---|
| 204 | yield browser.actions().keyUp(Key.CONTROL).perform();
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | });
|
---|
| 209 | }
|
---|
| 210 | /** Checks whether this element matches the given selector. */
|
---|
| 211 | matchesSelector(selector) {
|
---|
| 212 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 213 | return browser.executeScript(`
|
---|
| 214 | return (Element.prototype.matches ||
|
---|
| 215 | Element.prototype.msMatchesSelector).call(arguments[0], arguments[1])
|
---|
| 216 | `, this.element, selector);
|
---|
| 217 | });
|
---|
| 218 | }
|
---|
| 219 | /** Checks whether the element is focused. */
|
---|
| 220 | isFocused() {
|
---|
| 221 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 222 | return this.element.equals(browser.driver.switchTo().activeElement());
|
---|
| 223 | });
|
---|
| 224 | }
|
---|
| 225 | /**
|
---|
| 226 | * Dispatches an event with a particular name.
|
---|
| 227 | * @param name Name of the event to be dispatched.
|
---|
| 228 | */
|
---|
| 229 | dispatchEvent(name, data) {
|
---|
| 230 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 231 | return browser.executeScript(_dispatchEvent, name, this.element, data);
|
---|
| 232 | });
|
---|
| 233 | }
|
---|
| 234 | /** Dispatches all the events that are part of a click event sequence. */
|
---|
| 235 | _dispatchClickEventSequence(args, button) {
|
---|
| 236 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 237 | let modifiers = {};
|
---|
| 238 | if (args.length && typeof args[args.length - 1] === 'object') {
|
---|
| 239 | modifiers = args.pop();
|
---|
| 240 | }
|
---|
| 241 | const modifierKeys = toProtractorModifierKeys(modifiers);
|
---|
| 242 | // Omitting the offset argument to mouseMove results in clicking the center.
|
---|
| 243 | // This is the default behavior we want, so we use an empty array of offsetArgs if
|
---|
| 244 | // no args remain after popping the modifiers from the args passed to this function.
|
---|
| 245 | const offsetArgs = (args.length === 2 ?
|
---|
| 246 | [{ x: args[0], y: args[1] }] : []);
|
---|
| 247 | let actions = browser.actions()
|
---|
| 248 | .mouseMove(yield this.element.getWebElement(), ...offsetArgs);
|
---|
| 249 | for (const modifierKey of modifierKeys) {
|
---|
| 250 | actions = actions.keyDown(modifierKey);
|
---|
| 251 | }
|
---|
| 252 | actions = actions.click(button);
|
---|
| 253 | for (const modifierKey of modifierKeys) {
|
---|
| 254 | actions = actions.keyUp(modifierKey);
|
---|
| 255 | }
|
---|
| 256 | yield actions.perform();
|
---|
| 257 | });
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | /**
|
---|
| 261 | * Dispatches an event with a particular name and data to an element.
|
---|
| 262 | * Note that this needs to be a pure function, because it gets stringified by
|
---|
| 263 | * Protractor and is executed inside the browser.
|
---|
| 264 | */
|
---|
| 265 | function _dispatchEvent(name, element, data) {
|
---|
| 266 | const event = document.createEvent('Event');
|
---|
| 267 | event.initEvent(name);
|
---|
| 268 | if (data) {
|
---|
| 269 | // tslint:disable-next-line:ban Have to use `Object.assign` to preserve the original object.
|
---|
| 270 | Object.assign(event, data);
|
---|
| 271 | }
|
---|
| 272 | // This type has a string index signature, so we cannot access it using a dotted property access.
|
---|
| 273 | element['dispatchEvent'](event);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | /**
|
---|
| 277 | * @license
|
---|
| 278 | * Copyright Google LLC All Rights Reserved.
|
---|
| 279 | *
|
---|
| 280 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 281 | * found in the LICENSE file at https://angular.io/license
|
---|
| 282 | */
|
---|
| 283 | /** The default environment options. */
|
---|
| 284 | const defaultEnvironmentOptions = {
|
---|
| 285 | queryFn: (selector, root) => root.all(by.css(selector))
|
---|
| 286 | };
|
---|
| 287 | /**
|
---|
| 288 | * A `HarnessEnvironment` implementation for Protractor.
|
---|
| 289 | * @deprecated
|
---|
| 290 | * @breaking-change 13.0.0
|
---|
| 291 | */
|
---|
| 292 | class ProtractorHarnessEnvironment extends HarnessEnvironment {
|
---|
| 293 | constructor(rawRootElement, options) {
|
---|
| 294 | super(rawRootElement);
|
---|
| 295 | this._options = Object.assign(Object.assign({}, defaultEnvironmentOptions), options);
|
---|
| 296 | }
|
---|
| 297 | /** Creates a `HarnessLoader` rooted at the document root. */
|
---|
| 298 | static loader(options) {
|
---|
| 299 | return new ProtractorHarnessEnvironment(element(by.css('body')), options);
|
---|
| 300 | }
|
---|
| 301 | /** Gets the ElementFinder corresponding to the given TestElement. */
|
---|
| 302 | static getNativeElement(el) {
|
---|
| 303 | if (el instanceof ProtractorElement) {
|
---|
| 304 | return el.element;
|
---|
| 305 | }
|
---|
| 306 | throw Error('This TestElement was not created by the ProtractorHarnessEnvironment');
|
---|
| 307 | }
|
---|
| 308 | /**
|
---|
| 309 | * Flushes change detection and async tasks captured in the Angular zone.
|
---|
| 310 | * In most cases it should not be necessary to call this manually. However, there may be some edge
|
---|
| 311 | * cases where it is needed to fully flush animation events.
|
---|
| 312 | */
|
---|
| 313 | forceStabilize() {
|
---|
| 314 | return __awaiter(this, void 0, void 0, function* () { });
|
---|
| 315 | }
|
---|
| 316 | /** @docs-private */
|
---|
| 317 | waitForTasksOutsideAngular() {
|
---|
| 318 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 319 | // TODO: figure out how we can do this for the protractor environment.
|
---|
| 320 | // https://github.com/angular/components/issues/17412
|
---|
| 321 | });
|
---|
| 322 | }
|
---|
| 323 | /** Gets the root element for the document. */
|
---|
| 324 | getDocumentRoot() {
|
---|
| 325 | return element(by.css('body'));
|
---|
| 326 | }
|
---|
| 327 | /** Creates a `TestElement` from a raw element. */
|
---|
| 328 | createTestElement(element) {
|
---|
| 329 | return new ProtractorElement(element);
|
---|
| 330 | }
|
---|
| 331 | /** Creates a `HarnessLoader` rooted at the given raw element. */
|
---|
| 332 | createEnvironment(element) {
|
---|
| 333 | return new ProtractorHarnessEnvironment(element, this._options);
|
---|
| 334 | }
|
---|
| 335 | /**
|
---|
| 336 | * Gets a list of all elements matching the given selector under this environment's root element.
|
---|
| 337 | */
|
---|
| 338 | getAllRawElements(selector) {
|
---|
| 339 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 340 | const elementArrayFinder = this._options.queryFn(selector, this.rawRootElement);
|
---|
| 341 | const length = yield elementArrayFinder.count();
|
---|
| 342 | const elements = [];
|
---|
| 343 | for (let i = 0; i < length; i++) {
|
---|
| 344 | elements.push(elementArrayFinder.get(i));
|
---|
| 345 | }
|
---|
| 346 | return elements;
|
---|
| 347 | });
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /**
|
---|
| 352 | * @license
|
---|
| 353 | * Copyright Google LLC All Rights Reserved.
|
---|
| 354 | *
|
---|
| 355 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 356 | * found in the LICENSE file at https://angular.io/license
|
---|
| 357 | */
|
---|
| 358 |
|
---|
| 359 | /**
|
---|
| 360 | * @license
|
---|
| 361 | * Copyright Google LLC All Rights Reserved.
|
---|
| 362 | *
|
---|
| 363 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 364 | * found in the LICENSE file at https://angular.io/license
|
---|
| 365 | */
|
---|
| 366 |
|
---|
| 367 | export { ProtractorElement, ProtractorHarnessEnvironment };
|
---|
| 368 | //# sourceMappingURL=protractor.js.map
|
---|