1 | import { TestKey, getNoKeysSpecifiedError, _getTextWithExcludedElements, HarnessEnvironment } from '@angular/cdk/testing';
|
2 | import * as webdriver from 'selenium-webdriver';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const seleniumWebDriverKeyMap = {
|
9 | [TestKey.BACKSPACE]: webdriver.Key.BACK_SPACE,
|
10 | [TestKey.TAB]: webdriver.Key.TAB,
|
11 | [TestKey.ENTER]: webdriver.Key.ENTER,
|
12 | [TestKey.SHIFT]: webdriver.Key.SHIFT,
|
13 | [TestKey.CONTROL]: webdriver.Key.CONTROL,
|
14 | [TestKey.ALT]: webdriver.Key.ALT,
|
15 | [TestKey.ESCAPE]: webdriver.Key.ESCAPE,
|
16 | [TestKey.PAGE_UP]: webdriver.Key.PAGE_UP,
|
17 | [TestKey.PAGE_DOWN]: webdriver.Key.PAGE_DOWN,
|
18 | [TestKey.END]: webdriver.Key.END,
|
19 | [TestKey.HOME]: webdriver.Key.HOME,
|
20 | [TestKey.LEFT_ARROW]: webdriver.Key.ARROW_LEFT,
|
21 | [TestKey.UP_ARROW]: webdriver.Key.ARROW_UP,
|
22 | [TestKey.RIGHT_ARROW]: webdriver.Key.ARROW_RIGHT,
|
23 | [TestKey.DOWN_ARROW]: webdriver.Key.ARROW_DOWN,
|
24 | [TestKey.INSERT]: webdriver.Key.INSERT,
|
25 | [TestKey.DELETE]: webdriver.Key.DELETE,
|
26 | [TestKey.F1]: webdriver.Key.F1,
|
27 | [TestKey.F2]: webdriver.Key.F2,
|
28 | [TestKey.F3]: webdriver.Key.F3,
|
29 | [TestKey.F4]: webdriver.Key.F4,
|
30 | [TestKey.F5]: webdriver.Key.F5,
|
31 | [TestKey.F6]: webdriver.Key.F6,
|
32 | [TestKey.F7]: webdriver.Key.F7,
|
33 | [TestKey.F8]: webdriver.Key.F8,
|
34 | [TestKey.F9]: webdriver.Key.F9,
|
35 | [TestKey.F10]: webdriver.Key.F10,
|
36 | [TestKey.F11]: webdriver.Key.F11,
|
37 | [TestKey.F12]: webdriver.Key.F12,
|
38 | [TestKey.META]: webdriver.Key.META,
|
39 | };
|
40 |
|
41 | function getSeleniumWebDriverModifierKeys(modifiers) {
|
42 | const result = [];
|
43 | if (modifiers.control) {
|
44 | result.push(webdriver.Key.CONTROL);
|
45 | }
|
46 | if (modifiers.alt) {
|
47 | result.push(webdriver.Key.ALT);
|
48 | }
|
49 | if (modifiers.shift) {
|
50 | result.push(webdriver.Key.SHIFT);
|
51 | }
|
52 | if (modifiers.meta) {
|
53 | result.push(webdriver.Key.META);
|
54 | }
|
55 | return result;
|
56 | }
|
57 |
|
58 |
|
59 | class SeleniumWebDriverElement {
|
60 | constructor(element, _stabilize) {
|
61 | this.element = element;
|
62 | this._stabilize = _stabilize;
|
63 | }
|
64 |
|
65 | async blur() {
|
66 | await this._executeScript((element) => element.blur(), this.element());
|
67 | await this._stabilize();
|
68 | }
|
69 |
|
70 | async clear() {
|
71 | await this.element().clear();
|
72 | await this._stabilize();
|
73 | }
|
74 | async click(...args) {
|
75 | await this._dispatchClickEventSequence(args, webdriver.Button.LEFT);
|
76 | await this._stabilize();
|
77 | }
|
78 | async rightClick(...args) {
|
79 | await this._dispatchClickEventSequence(args, webdriver.Button.RIGHT);
|
80 | await this._stabilize();
|
81 | }
|
82 |
|
83 | async focus() {
|
84 | await this._executeScript((element) => element.focus(), this.element());
|
85 | await this._stabilize();
|
86 | }
|
87 |
|
88 | async getCssValue(property) {
|
89 | await this._stabilize();
|
90 | return this.element().getCssValue(property);
|
91 | }
|
92 |
|
93 | async hover() {
|
94 | await this._actions().mouseMove(this.element()).perform();
|
95 | await this._stabilize();
|
96 | }
|
97 |
|
98 | async mouseAway() {
|
99 | await this._actions().mouseMove(this.element(), { x: -1, y: -1 }).perform();
|
100 | await this._stabilize();
|
101 | }
|
102 | async sendKeys(...modifiersAndKeys) {
|
103 | const first = modifiersAndKeys[0];
|
104 | let modifiers;
|
105 | let rest;
|
106 | if (first !== undefined && typeof first !== 'string' && typeof first !== 'number') {
|
107 | modifiers = first;
|
108 | rest = modifiersAndKeys.slice(1);
|
109 | }
|
110 | else {
|
111 | modifiers = {};
|
112 | rest = modifiersAndKeys;
|
113 | }
|
114 | const modifierKeys = getSeleniumWebDriverModifierKeys(modifiers);
|
115 | const keys = rest
|
116 | .map(k => (typeof k === 'string' ? k.split('') : [seleniumWebDriverKeyMap[k]]))
|
117 | .reduce((arr, k) => arr.concat(k), [])
|
118 |
|
119 |
|
120 | .map(k => (modifierKeys.length > 0 ? webdriver.Key.chord(...modifierKeys, k) : k));
|
121 |
|
122 |
|
123 | if (keys.length === 0) {
|
124 | throw getNoKeysSpecifiedError();
|
125 | }
|
126 | await this.element().sendKeys(...keys);
|
127 | await this._stabilize();
|
128 | }
|
129 | |
130 |
|
131 |
|
132 |
|
133 | async text(options) {
|
134 | await this._stabilize();
|
135 | if (options?.exclude) {
|
136 | return this._executeScript(_getTextWithExcludedElements, this.element(), options.exclude);
|
137 | }
|
138 |
|
139 | return this._executeScript((element) => (element.textContent || '').trim(), this.element());
|
140 | }
|
141 | |
142 |
|
143 |
|
144 |
|
145 | async setContenteditableValue(value) {
|
146 | const contenteditableAttr = await this.getAttribute('contenteditable');
|
147 | if (contenteditableAttr !== '' && contenteditableAttr !== 'true') {
|
148 | throw new Error('setContenteditableValue can only be called on a `contenteditable` element.');
|
149 | }
|
150 | await this._stabilize();
|
151 | return this._executeScript((element, valueToSet) => (element.textContent = valueToSet), this.element(), value);
|
152 | }
|
153 |
|
154 | async getAttribute(name) {
|
155 | await this._stabilize();
|
156 | return this._executeScript((element, attribute) => element.getAttribute(attribute), this.element(), name);
|
157 | }
|
158 |
|
159 | async hasClass(name) {
|
160 | await this._stabilize();
|
161 | const classes = (await this.getAttribute('class')) || '';
|
162 | return new Set(classes.split(/\s+/).filter(c => c)).has(name);
|
163 | }
|
164 |
|
165 | async getDimensions() {
|
166 | await this._stabilize();
|
167 | const { width, height } = await this.element().getSize();
|
168 | const { x: left, y: top } = await this.element().getLocation();
|
169 | return { width, height, left, top };
|
170 | }
|
171 |
|
172 | async getProperty(name) {
|
173 | await this._stabilize();
|
174 | return this._executeScript((element, property) => element[property], this.element(), name);
|
175 | }
|
176 |
|
177 | async setInputValue(newValue) {
|
178 | await this._executeScript((element, value) => (element.value = value), this.element(), newValue);
|
179 | await this._stabilize();
|
180 | }
|
181 |
|
182 | async selectOptions(...optionIndexes) {
|
183 | await this._stabilize();
|
184 | const options = await this.element().findElements(webdriver.By.css('option'));
|
185 | const indexes = new Set(optionIndexes);
|
186 | if (options.length && indexes.size) {
|
187 |
|
188 |
|
189 | await this.setInputValue('');
|
190 | for (let i = 0; i < options.length; i++) {
|
191 | if (indexes.has(i)) {
|
192 |
|
193 |
|
194 | await this._actions().keyDown(webdriver.Key.CONTROL).perform();
|
195 | await options[i].click();
|
196 | await this._actions().keyUp(webdriver.Key.CONTROL).perform();
|
197 | }
|
198 | }
|
199 | await this._stabilize();
|
200 | }
|
201 | }
|
202 |
|
203 | async matchesSelector(selector) {
|
204 | await this._stabilize();
|
205 | return this._executeScript((element, s) => (Element.prototype.matches || Element.prototype.msMatchesSelector).call(element, s), this.element(), selector);
|
206 | }
|
207 |
|
208 | async isFocused() {
|
209 | await this._stabilize();
|
210 | return webdriver.WebElement.equals(this.element(), this.element().getDriver().switchTo().activeElement());
|
211 | }
|
212 | |
213 |
|
214 |
|
215 |
|
216 | async dispatchEvent(name, data) {
|
217 | await this._executeScript(dispatchEvent, name, this.element(), data);
|
218 | await this._stabilize();
|
219 | }
|
220 |
|
221 | _actions() {
|
222 | return this.element().getDriver().actions();
|
223 | }
|
224 |
|
225 | async _executeScript(script, ...var_args) {
|
226 | return this.element()
|
227 | .getDriver()
|
228 | .executeScript(script, ...var_args);
|
229 | }
|
230 |
|
231 | async _dispatchClickEventSequence(args, button) {
|
232 | let modifiers = {};
|
233 | if (args.length && typeof args[args.length - 1] === 'object') {
|
234 | modifiers = args.pop();
|
235 | }
|
236 | const modifierKeys = getSeleniumWebDriverModifierKeys(modifiers);
|
237 |
|
238 |
|
239 |
|
240 | const offsetArgs = (args.length === 2 ? [{ x: args[0], y: args[1] }] : []);
|
241 | let actions = this._actions().mouseMove(this.element(), ...offsetArgs);
|
242 | for (const modifierKey of modifierKeys) {
|
243 | actions = actions.keyDown(modifierKey);
|
244 | }
|
245 | actions = actions.click(button);
|
246 | for (const modifierKey of modifierKeys) {
|
247 | actions = actions.keyUp(modifierKey);
|
248 | }
|
249 | await actions.perform();
|
250 | }
|
251 | }
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | function dispatchEvent(name, element, data) {
|
257 | const event = document.createEvent('Event');
|
258 | event.initEvent(name);
|
259 |
|
260 | Object.assign(event, data || {});
|
261 | element.dispatchEvent(event);
|
262 | }
|
263 |
|
264 |
|
265 | const defaultEnvironmentOptions = {
|
266 | queryFn: async (selector, root) => root().findElements(webdriver.By.css(selector)),
|
267 | };
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | function whenStable(callback) {
|
273 | Promise.all(window.frameworkStabilizers.map(stabilizer => new Promise(stabilizer))).then(callback);
|
274 | }
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | function isBootstrapped() {
|
280 | return !!window.frameworkStabilizers;
|
281 | }
|
282 |
|
283 | async function waitForAngularReady(wd) {
|
284 | await wd.wait(() => wd.executeScript(isBootstrapped));
|
285 | await wd.executeAsyncScript(whenStable);
|
286 | }
|
287 |
|
288 | class SeleniumWebDriverHarnessEnvironment extends HarnessEnvironment {
|
289 | constructor(rawRootElement, options) {
|
290 | super(rawRootElement);
|
291 | this._options = { ...defaultEnvironmentOptions, ...options };
|
292 | this._stabilizeCallback = () => this.forceStabilize();
|
293 | }
|
294 |
|
295 | static getNativeElement(el) {
|
296 | if (el instanceof SeleniumWebDriverElement) {
|
297 | return el.element();
|
298 | }
|
299 | throw Error('This TestElement was not created by the WebDriverHarnessEnvironment');
|
300 | }
|
301 |
|
302 | static loader(driver, options) {
|
303 | return new SeleniumWebDriverHarnessEnvironment(() => driver.findElement(webdriver.By.css('body')), options);
|
304 | }
|
305 | |
306 |
|
307 |
|
308 |
|
309 |
|
310 | async forceStabilize() {
|
311 | await this.rawRootElement().getDriver().executeAsyncScript(whenStable);
|
312 | }
|
313 |
|
314 | async waitForTasksOutsideAngular() {
|
315 |
|
316 |
|
317 | }
|
318 |
|
319 | getDocumentRoot() {
|
320 | return () => this.rawRootElement().getDriver().findElement(webdriver.By.css('body'));
|
321 | }
|
322 |
|
323 | createTestElement(element) {
|
324 | return new SeleniumWebDriverElement(element, this._stabilizeCallback);
|
325 | }
|
326 |
|
327 | createEnvironment(element) {
|
328 | return new SeleniumWebDriverHarnessEnvironment(element, this._options);
|
329 | }
|
330 |
|
331 |
|
332 |
|
333 | |
334 |
|
335 |
|
336 | async getAllRawElements(selector) {
|
337 | const els = await this._options.queryFn(selector, this.rawRootElement);
|
338 | return els.map((x) => () => x);
|
339 | }
|
340 | }
|
341 |
|
342 | export { SeleniumWebDriverElement, SeleniumWebDriverHarnessEnvironment, waitForAngularReady };
|
343 |
|