@applitools/driver 1.11.32 → 1.11.33

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/driver.js CHANGED
@@ -39,6 +39,7 @@ class Driver {
39
39
  constructor(options) {
40
40
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
41
41
  this._driverInfo = {};
42
+ this._state = {};
42
43
  this._customConfig = {};
43
44
  this._customConfig = (_a = options.customConfig) !== null && _a !== void 0 ? _a : {};
44
45
  this._spec = options.spec;
@@ -189,8 +190,13 @@ class Driver {
189
190
  if (this.isMobile) {
190
191
  this._driverInfo.orientation =
191
192
  (_l = (await this.getOrientation().catch(() => undefined))) !== null && _l !== void 0 ? _l : this._driverInfo.orientation;
192
- const world = await this.getCurrentWorld();
193
- this._driverInfo.isWebView = !!(world === null || world === void 0 ? void 0 : world.isWebView);
193
+ if (this.isWeb) {
194
+ const world = await this.getCurrentWorld();
195
+ if (world) {
196
+ const [home] = (await this.getWorlds());
197
+ this._driverInfo.isWebView = world !== home;
198
+ }
199
+ }
194
200
  }
195
201
  if (this.isWeb) {
196
202
  const browserInfo = await this.currentContext.executePoll(snippets.getBrowserInfo);
@@ -335,6 +341,34 @@ class Driver {
335
341
  this._logger.log(`Returning helper for of type ${(_c = (_b = this._helper) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : null}`);
336
342
  return this._helper;
337
343
  }
344
+ async extractBrokerUrl() {
345
+ if (this._state.brokerUrl)
346
+ return this._state.brokerUrl;
347
+ if (!this.isNative)
348
+ return null;
349
+ this._logger.log('Broker url extraction is started');
350
+ const element = await this.element({ type: 'accessibility id', selector: 'Applitools_View' });
351
+ if (!element)
352
+ return null;
353
+ try {
354
+ let result;
355
+ do {
356
+ result = JSON.parse(await element.getText());
357
+ if (result.nextPath) {
358
+ this._logger.log('Broker url was extraction finished successfully with value', result.nextPath);
359
+ this._state.brokerUrl = result.nextPath;
360
+ return this._state.brokerUrl;
361
+ }
362
+ await utils.general.sleep(1000);
363
+ } while (!result.error);
364
+ this._logger.error('Broker url extraction has failed with error', result.error);
365
+ return null;
366
+ }
367
+ catch (error) {
368
+ this._logger.error('Broker url extraction has failed with error', error);
369
+ return null;
370
+ }
371
+ }
338
372
  // begin world
339
373
  //
340
374
  // About the concept of a "World":
@@ -353,69 +387,73 @@ class Driver {
353
387
  // - the native app world can be switched to (with the `goHome` option)
354
388
  async switchWorld(options) {
355
389
  var _a, _b;
356
- if ((options === null || options === void 0 ? void 0 : options.restoreState) && !this._previousWorld)
390
+ if ((options === null || options === void 0 ? void 0 : options.restoreState) && !this._state.world)
357
391
  return;
358
392
  if (!this._spec.getCurrentWorld || !this._spec.switchWorld) {
359
393
  this._logger.warn('world switching not implemented in the spec driver, skipping');
360
394
  return;
361
395
  }
362
396
  this._logger.log('switchWorld called with', options ? options : 'no options');
363
- const { id, home, next } = await this.getCurrentWorld();
364
- if (!this._previousWorld) {
365
- this._logger.log('storing current world id for future restoration', id);
366
- this._previousWorld = id;
367
- }
368
- const providedTarget = (options === null || options === void 0 ? void 0 : options.restoreState)
369
- ? this._previousWorld
370
- : (options === null || options === void 0 ? void 0 : options.goHome)
371
- ? home
372
- : (options === null || options === void 0 ? void 0 : options.id)
373
- ? options.id
374
- : next;
375
- this._logger.log('switching world with', providedTarget ? providedTarget : 'no id');
397
+ const current = (await this.getCurrentWorld());
398
+ if (!this._state.world) {
399
+ this._logger.log('storing current world id for future restoration', current);
400
+ this._state.world = current;
401
+ }
402
+ let world;
403
+ if (options === null || options === void 0 ? void 0 : options.id)
404
+ world = options.id;
405
+ else if (options === null || options === void 0 ? void 0 : options.restoreState)
406
+ world = this._state.world;
407
+ else {
408
+ const [home, next] = (await this.getWorlds());
409
+ if (options === null || options === void 0 ? void 0 : options.goHome)
410
+ world = home;
411
+ else
412
+ world = next;
413
+ }
414
+ this._logger.log('switching world with', world);
376
415
  try {
377
- await ((_b = (_a = this._spec).switchWorld) === null || _b === void 0 ? void 0 : _b.call(_a, this.target, providedTarget));
416
+ await ((_b = (_a = this._spec).switchWorld) === null || _b === void 0 ? void 0 : _b.call(_a, this.target, world));
378
417
  await this.init();
379
418
  }
380
419
  catch (error) {
381
420
  throw new Error(`Unable to switch worlds, the original error was: ${error.message}`);
382
421
  }
383
422
  }
384
- async getWorlds(attempt = 1) {
385
- var _a, _b;
423
+ async getWorlds() {
386
424
  if (!this._spec.getWorlds)
387
- return [];
388
- this._logger.log('attempting to find worlds');
389
- await utils.general.sleep(500);
390
- const worlds = await ((_b = (_a = this._spec).getWorlds) === null || _b === void 0 ? void 0 : _b.call(_a, this.target));
391
- if (!worlds[1]) {
392
- if (attempt > 5) {
393
- this._logger.warn(`just one world found - ${worlds}. done looking.`);
394
- return worlds;
425
+ return null;
426
+ this._logger.log('Extracting worlds');
427
+ try {
428
+ let worlds = [];
429
+ for (let attempt = 0; worlds.length <= 1 && attempt < 5; ++attempt) {
430
+ if (attempt > 0)
431
+ await utils.general.sleep(500);
432
+ worlds = await this._spec.getWorlds(this.target);
395
433
  }
396
- this._logger.log(`just one world found, retrying to see if there are others (attempt #${attempt})`);
397
- await this.getWorlds(attempt + 1);
434
+ this._logger.log('Worlds were extracted', worlds);
435
+ return worlds;
436
+ }
437
+ catch (error) {
438
+ this._logger.warn('Worlds were not extracted due to the error', error);
439
+ return null;
398
440
  }
399
- this._logger.log(`worlds found - ${worlds}`);
400
- return worlds;
401
441
  }
402
442
  async getCurrentWorld() {
403
443
  var _a, _b;
404
444
  if (!this._spec.getCurrentWorld)
405
- return undefined;
406
- const [origin, next] = await this.getWorlds();
407
- const currentWorld = await ((_b = (_a = this._spec).getCurrentWorld) === null || _b === void 0 ? void 0 : _b.call(_a, this.target));
408
- const result = {
409
- id: currentWorld,
410
- home: origin,
411
- next,
412
- isNative: currentWorld === origin,
413
- isWebView: currentWorld !== origin,
414
- };
415
- this._logger.log('current world', result);
416
- return result;
445
+ return null;
446
+ try {
447
+ this._logger.log('Extracting current world');
448
+ const current = await ((_b = (_a = this._spec).getCurrentWorld) === null || _b === void 0 ? void 0 : _b.call(_a, this.target));
449
+ this._logger.log('Current world was extracted', current);
450
+ return current;
451
+ }
452
+ catch (error) {
453
+ this._logger.warn('Current world was not extracted due to the error', error);
454
+ return null;
455
+ }
417
456
  }
418
- // end world
419
457
  async getSessionMetadata() {
420
458
  var _a, _b;
421
459
  if (this.isECClient)
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.visit = exports.getTitle = exports.getUrl = exports.getOrientation = exports.setWindowSize = exports.getWindowSize = exports.getDriverInfo = exports.takeScreenshot = exports.childContext = exports.parentContext = exports.mainContext = exports.findElements = exports.findElement = exports.executeScript = exports.isEqualElements = exports.isStaleElementError = exports.extractSelector = exports.untransformSelector = exports.transformSelector = exports.isSelector = exports.isElement = exports.isDriver = void 0;
26
+ exports.visit = exports.getTitle = exports.getUrl = exports.getOrientation = exports.setWindowSize = exports.getWindowSize = exports.getDriverInfo = exports.takeScreenshot = exports.childContext = exports.parentContext = exports.mainContext = exports.getElementText = exports.findElements = exports.findElement = exports.executeScript = exports.isEqualElements = exports.isStaleElementError = exports.extractSelector = exports.untransformSelector = exports.transformSelector = exports.isSelector = exports.isElement = exports.isDriver = void 0;
27
27
  const utils = __importStar(require("@applitools/utils"));
28
28
  function isDriver(driver) {
29
29
  return driver && driver.constructor.name === 'MockDriver';
@@ -84,6 +84,11 @@ async function findElements(driver, selector, parent) {
84
84
  return driver.findElements(selector, parent);
85
85
  }
86
86
  exports.findElements = findElements;
87
+ async function getElementText(_driver, element) {
88
+ var _a;
89
+ return (_a = element.attrs) === null || _a === void 0 ? void 0 : _a.text;
90
+ }
91
+ exports.getElementText = getElementText;
87
92
  async function mainContext(driver) {
88
93
  return driver.switchToFrame(null);
89
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applitools/driver",
3
- "version": "1.11.32",
3
+ "version": "1.11.33",
4
4
  "description": "Applitools universal framework wrapper",
5
5
  "keywords": [
6
6
  "applitools",
package/types/driver.d.ts CHANGED
@@ -21,10 +21,10 @@ export declare class Driver<T extends SpecType> {
21
21
  private _mainContext;
22
22
  private _currentContext;
23
23
  private _driverInfo;
24
+ private _helper?;
25
+ private _state;
24
26
  private _logger;
25
27
  private _customConfig;
26
- private _helper?;
27
- private _previousWorld?;
28
28
  protected readonly _spec: SpecDriver<T>;
29
29
  constructor(options: DriverOptions<T>);
30
30
  get target(): T['driver'];
@@ -65,19 +65,14 @@ export declare class Driver<T extends SpecType> {
65
65
  updateCurrentContext(context: Context<T>): void;
66
66
  init(): Promise<this>;
67
67
  getHelper(): Promise<HelperAndroid<T> | HelperIOS<T> | null>;
68
+ extractBrokerUrl(): Promise<string | null>;
68
69
  switchWorld(options?: {
69
70
  id?: string;
70
71
  restoreState?: boolean;
71
72
  goHome?: boolean;
72
73
  }): Promise<void>;
73
- getWorlds(attempt?: number): Promise<string[]>;
74
- getCurrentWorld(): Promise<{
75
- id: string;
76
- home: string;
77
- next?: string;
78
- isNative: boolean;
79
- isWebView: boolean;
80
- }>;
74
+ getWorlds(): Promise<string[] | null>;
75
+ getCurrentWorld(): Promise<string | null>;
81
76
  getSessionMetadata(): Promise<any>;
82
77
  refreshContexts(): Promise<Context<T>>;
83
78
  switchTo(context: Context<T>): Promise<Context<T>>;
@@ -22,8 +22,9 @@ export declare function extractSelector(element: Element): any;
22
22
  export declare function isStaleElementError(): boolean;
23
23
  export declare function isEqualElements(_driver: Driver, element1: Element, element2: Element): Promise<boolean>;
24
24
  export declare function executeScript(driver: Driver, script: ((arg: any) => any) | string, arg: any): Promise<any>;
25
- export declare function findElement(driver: Driver, selector: Selector, parent?: Element): Promise<Element>;
25
+ export declare function findElement(driver: Driver, selector: Selector, parent?: Element): Promise<Element | null>;
26
26
  export declare function findElements(driver: Driver, selector: Selector, parent?: Element): Promise<Element[]>;
27
+ export declare function getElementText(_driver: Driver, element: Element): Promise<string>;
27
28
  export declare function mainContext(driver: Driver): Promise<Driver>;
28
29
  export declare function parentContext(driver: Driver): Promise<Driver>;
29
30
  export declare function childContext(driver: Driver, element: Element): Promise<Driver>;