Untitled

 avatar
unknown
plain_text
a year ago
2.5 kB
5
Indexable
 /**
   * Swipe from coordinates (from) to the new coordinates (to). The given coordinates are
   * percentages of the screen.
   */
  static async swipeOnPercentage(from: Coordinate, to: Coordinate) {
    // Get the screen size and store it so it can be re-used.
    // This will save a lot of webdriver calls if this methods is used multiple times.
    screenSize = screenSize || (await driver.getWindowRect());
    // Get the start position on the screen for the swipe
    const startCoordinates = this.getDeviceScreenCoordinates(screenSize, from);
    // Get the move to position on the screen for the swipe
    const endCoordinates = this.getDeviceScreenCoordinates(screenSize, to);

    await this.swipe(startCoordinates, endCoordinates);
  }

  /**
   * Swipe from coordinates (from) to the new coordinates (to). The given coordinates are in pixels.
   */
  static async swipe(from: Coordinate, to: Coordinate) {
    await driver.performActions([
      {
        actions: [
          // b. Move finger into start position
          { duration: 0, type: 'pointerMove', x: from.x, y: from.y },
          // c. Finger comes down into contact with screen
          { button: 0, type: 'pointerDown' },
          // d. Pause for a little bit
          { duration: 100, type: 'pause' },
          // e. Finger moves to end position
          //    We move our finger from the center of the element to the
          //    starting position of the element.
          //    Play with the duration to make the swipe go slower / faster
          { duration: 1000, type: 'pointerMove', x: to.x, y: to.y },
          // f. Finger gets up, off the screen
          { button: 0, type: 'pointerUp' },
        ],

        id: 'finger1',

        parameters: { pointerType: 'touch' },
        // a. Create the event
        type: 'pointer',
      },
    ]);
    // Add a pause, just to make sure the swipe is done
    await driver.pause(1000);
  }

  /**
   * Get the screen coordinates based on the device screen size
   */
  private static getDeviceScreenCoordinates(
    deviceScreenSize: RectReturn,
    coordinates: Coordinate,
  ): Coordinate {
    return {
      x: Math.round(deviceScreenSize.width * (coordinates.x / 100)),
      y: Math.round(deviceScreenSize.height * (coordinates.y / 100)),
    };
  }

  /**
   * Calculate the x y coordinates based on a percentage
   */
  private static calculateXY(
    { x, y }: Coordinate,
    percentage: number,
  ): Coordinate {
    return {
      x: x * percentage,
      y: y * percentage,
    };
  }
Editor is loading...
Leave a Comment