@applitools/driver 1.11.26 → 1.11.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/context.js CHANGED
@@ -29,14 +29,19 @@ const utils = __importStar(require("@applitools/utils"));
29
29
  const specUtils = __importStar(require("./spec-utils"));
30
30
  const snippets = require('@applitools/snippets');
31
31
  class Context {
32
+ _isReference(reference) {
33
+ return (reference instanceof Context ||
34
+ utils.types.isInteger(reference) ||
35
+ utils.types.isString(reference) ||
36
+ reference instanceof element_1.Element ||
37
+ this._spec.isElement(reference) ||
38
+ specUtils.isSelector(this._spec, reference));
39
+ }
32
40
  constructor(options) {
33
- var _a, _b, _c;
41
+ var _a, _b, _c, _d;
34
42
  this._state = {};
35
- if (options.context instanceof Context)
36
- return options.context;
37
43
  this._spec = options.spec;
38
- if (options.logger)
39
- this._logger = options.logger;
44
+ this._logger = options.logger;
40
45
  if (options.context) {
41
46
  if ((_c = (_b = (_a = this._spec).isContext) === null || _b === void 0 ? void 0 : _b.call(_a, options.context)) !== null && _c !== void 0 ? _c : this._spec.isDriver(options.context)) {
42
47
  this._target = options.context;
@@ -54,11 +59,9 @@ class Context {
54
59
  this._reference = options.reference;
55
60
  this._parent = options.parent;
56
61
  this._scrollingElement = options.scrollingElement;
57
- this._driver = options.driver || this._parent.driver;
62
+ this._driver = options.driver || ((_d = this._parent) === null || _d === void 0 ? void 0 : _d.driver);
58
63
  }
59
64
  else if (!options.reference) {
60
- this._element = null;
61
- this._parent = null;
62
65
  this._scrollingElement = options.scrollingElement;
63
66
  this._driver = options.driver;
64
67
  }
@@ -66,14 +69,6 @@ class Context {
66
69
  throw new TypeError('Context constructor called with argument of unknown type!');
67
70
  }
68
71
  }
69
- _isReference(reference) {
70
- return (reference instanceof Context ||
71
- utils.types.isInteger(reference) ||
72
- utils.types.isString(reference) ||
73
- reference instanceof element_1.Element ||
74
- this._spec.isElement(reference) ||
75
- specUtils.isSelector(this._spec, reference));
76
- }
77
72
  get target() {
78
73
  return this._target;
79
74
  }
@@ -105,6 +100,7 @@ class Context {
105
100
  return !this._target;
106
101
  }
107
102
  async _findElements(selector, options = {}) {
103
+ var _a;
108
104
  await this.focus();
109
105
  const { parent, all, wait } = options;
110
106
  const transformedSelector = specUtils.transformSelector(this._spec, selector, this.driver);
@@ -123,8 +119,9 @@ class Context {
123
119
  if (element) {
124
120
  clearTimeout(timeout);
125
121
  elements = [element];
122
+ break;
126
123
  }
127
- await utils.general.sleep(wait.interval);
124
+ await utils.general.sleep((_a = wait.interval) !== null && _a !== void 0 ? _a : 0);
128
125
  }
129
126
  }
130
127
  }
@@ -163,7 +160,7 @@ class Context {
163
160
  }
164
161
  }
165
162
  if (elements.length === 0 && selector.fallback) {
166
- elements = await this._findElements(selector.fallback, parent);
163
+ elements = await this._findElements(selector.fallback, { parent });
167
164
  }
168
165
  }
169
166
  return elements;
@@ -171,8 +168,9 @@ class Context {
171
168
  async init() {
172
169
  if (this.isInitialized)
173
170
  return this;
174
- if (!this._reference)
171
+ if (!this._reference || !this.parent) {
175
172
  throw new TypeError('Cannot initialize context without a reference to the context element');
173
+ }
176
174
  await this.parent.focus();
177
175
  this._logger.log('Context initialization');
178
176
  if (utils.types.isInteger(this._reference)) {
@@ -200,12 +198,10 @@ class Context {
200
198
  }
201
199
  else if (this._spec.isElement(this._reference) || this._reference instanceof element_1.Element) {
202
200
  this._logger.log('Initialize context from reference element', this._reference);
203
- this._element = new element_1.Element({
204
- spec: this._spec,
205
- context: this.parent,
206
- element: this._reference,
207
- logger: this._logger,
208
- });
201
+ this._element =
202
+ this._reference instanceof element_1.Element
203
+ ? this._reference
204
+ : new element_1.Element({ spec: this._spec, context: this.parent, element: this._reference, logger: this._logger });
209
205
  }
210
206
  else {
211
207
  throw new TypeError('Reference type does not supported');
@@ -241,7 +237,7 @@ class Context {
241
237
  return true;
242
238
  if (!this._element)
243
239
  return false;
244
- return this._element.equals(context instanceof Context ? await context.getContextElement() : context);
240
+ return this._element.equals(context instanceof Context ? (await context.getContextElement()) : context);
245
241
  }
246
242
  async context(reference) {
247
243
  if (reference instanceof Context) {
@@ -264,6 +260,9 @@ class Context {
264
260
  logger: this._logger,
265
261
  });
266
262
  }
263
+ else {
264
+ throw new Error('Cannot get context using reference of unknown type!');
265
+ }
267
266
  }
268
267
  async element(elementOrSelector) {
269
268
  if (this._spec.isElement(elementOrSelector)) {
@@ -307,6 +306,7 @@ class Context {
307
306
  }
308
307
  }
309
308
  async waitFor(selector, options) {
309
+ this._logger.log('Waiting for element by selector: ', selector, 'and options', options);
310
310
  const [element] = await this._findElements(selector, {
311
311
  wait: { state: 'exist', timeout: 10000, interval: 500, ...options },
312
312
  });
@@ -384,10 +384,11 @@ class Context {
384
384
  }
385
385
  }
386
386
  async getContextElement() {
387
+ var _a;
387
388
  if (this.isMain)
388
389
  return null;
389
390
  await this.init();
390
- return this._element;
391
+ return (_a = this._element) !== null && _a !== void 0 ? _a : null;
391
392
  }
392
393
  async getScrollingElement() {
393
394
  if (!(this._scrollingElement instanceof element_1.Element)) {
@@ -444,7 +445,7 @@ class Context {
444
445
  }
445
446
  }
446
447
  async getRegion() {
447
- var _a;
448
+ var _a, _b;
448
449
  if (this.isMain && this.isCurrent) {
449
450
  const viewportRegion = utils.geometry.region({ x: 0, y: 0 }, await this.driver.getViewportSize());
450
451
  this._state.region = this._scrollingElement
@@ -453,12 +454,12 @@ class Context {
453
454
  }
454
455
  else if ((_a = this.parent) === null || _a === void 0 ? void 0 : _a.isCurrent) {
455
456
  await this.init();
456
- this._state.region = await this._element.getRegion();
457
+ this._state.region = await ((_b = this._element) === null || _b === void 0 ? void 0 : _b.getRegion());
457
458
  }
458
459
  return this._state.region;
459
460
  }
460
461
  async getClientRegion() {
461
- var _a;
462
+ var _a, _b;
462
463
  if (this.isMain && this.isCurrent) {
463
464
  const viewportRegion = utils.geometry.region({ x: 0, y: 0 }, await this.driver.getViewportSize());
464
465
  this._state.clientRegion = this._scrollingElement
@@ -467,14 +468,14 @@ class Context {
467
468
  }
468
469
  else if ((_a = this.parent) === null || _a === void 0 ? void 0 : _a.isCurrent) {
469
470
  await this.init();
470
- this._state.clientRegion = await this._element.getClientRegion();
471
+ this._state.clientRegion = await ((_b = this._element) === null || _b === void 0 ? void 0 : _b.getClientRegion());
471
472
  }
472
473
  return this._state.clientRegion;
473
474
  }
474
475
  async getScrollingRegion() {
475
476
  if (this.isCurrent) {
476
477
  const scrollingElement = await this.getScrollingElement();
477
- this._state.scrollingRegion = await scrollingElement.getClientRegion();
478
+ this._state.scrollingRegion = await (scrollingElement === null || scrollingElement === void 0 ? void 0 : scrollingElement.getClientRegion());
478
479
  }
479
480
  return this._state.scrollingRegion;
480
481
  }
@@ -511,12 +512,12 @@ class Context {
511
512
  }
512
513
  async getRegionInViewport(region) {
513
514
  var _a, _b;
514
- let currentContext = this;
515
515
  this._logger.log('Converting context region to viewport region', region);
516
516
  if (region)
517
- region = utils.geometry.offsetNegative(region, await currentContext.getInnerOffset());
517
+ region = utils.geometry.offsetNegative(region, await this.getInnerOffset());
518
518
  else
519
519
  region = { x: 0, y: 0, width: Infinity, height: Infinity };
520
+ let currentContext = this;
520
521
  while (currentContext) {
521
522
  const contextRegion = await currentContext.getClientRegion();
522
523
  // const contextScrollingRegion = await currentContext.getScrollingRegion()
@@ -95,8 +95,8 @@ async function checkSpecDriver(options) {
95
95
  },
96
96
  'main context': async () => {
97
97
  const mainDocument1 = await spec.findElement(context, transformSelector('html'));
98
- const childContext1 = await spec.childContext(context, await spec.findElement(context, transformSelector('[name="frame1"]')));
99
- const childContext2 = await spec.childContext(childContext1, await spec.findElement(childContext1, transformSelector('[name="frame1-1"]')));
98
+ const childContext1 = await spec.childContext(context, (await spec.findElement(context, transformSelector('[name="frame1"]'))));
99
+ const childContext2 = await spec.childContext(childContext1, (await spec.findElement(childContext1, transformSelector('[name="frame1-1"]'))));
100
100
  const frameDocument = await spec.findElement(childContext2, transformSelector('html'));
101
101
  assert_1.strict.ok(!(await isEqualElements(childContext2, mainDocument1, frameDocument)));
102
102
  const mainContext = await spec.mainContext(childContext2);
@@ -104,9 +104,9 @@ async function checkSpecDriver(options) {
104
104
  assert_1.strict.ok(await isEqualElements(mainContext, mainDocument2, mainDocument1));
105
105
  },
106
106
  'parent context': async () => {
107
- const parentContext1 = await spec.childContext(context, await spec.findElement(context, transformSelector('[name="frame1"]')));
107
+ const parentContext1 = await spec.childContext(context, (await spec.findElement(context, transformSelector('[name="frame1"]'))));
108
108
  const parentDocument1 = await spec.findElement(parentContext1, transformSelector('html'));
109
- const frameContext = await spec.childContext(parentContext1, await spec.findElement(parentContext1, transformSelector('[name="frame1-1"]')));
109
+ const frameContext = await spec.childContext(parentContext1, (await spec.findElement(parentContext1, transformSelector('[name="frame1-1"]'))));
110
110
  const frameDocument = await spec.findElement(frameContext, transformSelector('html'));
111
111
  assert_1.strict.ok(!(await isEqualElements(frameContext, parentDocument1, frameDocument)));
112
112
  const parentContext2 = await spec.parentContext(frameContext);