import { RectReturn } from '@wdio/protocols/build/types' ; import { ChainablePromiseElement } from 'webdriverio' ; type XY = { x : number ; y : number ; } ; const SWIPE_DIRECTION = { down : { start : { x : 50 , y : 15 } , end : { x : 50 , y : 85 } } , left : { start : { x : 95 , y : 50 } , end : { x : 5 , y : 50 } } , right : { start : { x : 5 , y : 50 } , end : { x : 95 , y : 50 } } , up : { start : { x : 50 , y : 85 } , end : { x : 50 , y : 15 } } } ; const SWIPE_PERCENTAGE = 0.85 ; const WAIT_FOR_SWIPE_FINISHED = 1000 ; class Gestures { static SCREEN_SIZE : RectReturn | null ; static async checkIfDisplayedWithSwipeUp ( element , maxScrolls , amount = 0 ) { if ( ! ( await element . isDisplayed ( ) ) && amount <= maxScrolls ) { await this . swipeUp ( SWIPE_PERCENTAGE ) ; await this . checkIfDisplayedWithSwipeUp ( element , maxScrolls , amount + 1 ) ; } else if ( amount > maxScrolls ) { throw new Error ( `The element '${ element }' could not be found or is not visible.` ) ; } } static async swipeDown ( percentage = 1 ) { await this . swipeOnPercentage ( this . calculateXY ( SWIPE_DIRECTION . down . start , percentage ) , this . calculateXY ( SWIPE_DIRECTION . down . end , percentage ) ) ; } static async swipeUp ( percentage = 1 ) { await this . swipeOnPercentage ( this . calculateXY ( SWIPE_DIRECTION . up . start , percentage ) , this . calculateXY ( SWIPE_DIRECTION . up . end , percentage ) ) ; } static async swipeLeft ( percentage = 1 ) { await this . swipeOnPercentage ( this . calculateXY ( SWIPE_DIRECTION . left . start , percentage ) , this . calculateXY ( SWIPE_DIRECTION . left . end , percentage ) ) ; } /**
     * Swipe right based on a percentage
     */ static async swipeRight ( percentage = 1 ) { await this . swipeOnPercentage ( this . calculateXY ( SWIPE_DIRECTION . right . start , percentage ) , this . calculateXY ( SWIPE_DIRECTION . right . end , percentage ) ) ; } static async swipeOnPercentage ( from , to ) { if ( ! Gestures . SCREEN_SIZE ) { Gestures . SCREEN_SIZE = await driver . getWindowRect ( ) ; } const pressOptions = this . getDeviceScreenCoordinates ( Gestures . SCREEN_SIZE , from ) ; const moveToScreenCoordinates = this . getDeviceScreenCoordinates ( Gestures . SCREEN_SIZE , to ) ; await this . swipe ( pressOptions , moveToScreenCoordinates ) ; } static async swipe ( from , to ) { await driver . performActions ( [ { type : 'pointer' , id : 'finger1' , parameters : { pointerType : 'touch' } , actions : [ { type : 'pointerMove' , duration : 0 , x : from . x , y : from . y } , { type : 'pointerDown' , button : 0 } , { type : 'pause' , duration : 100 } , { type : 'pointerMove' , duration : 1000 , x : to . x , y : to . y } , { type : 'pointerUp' , button : 0 } ] } ] ) ; await driver . pause ( WAIT_FOR_SWIPE_FINISHED ) ; } private static getDeviceScreenCoordinates ( screenSize , coordinates ) { const HUNDRED_PERCENT = 100 ; return { x : Math . round ( screenSize . width * ( coordinates . x / HUNDRED_PERCENT ) ) , y : Math . round ( screenSize . height * ( coordinates . y / HUNDRED_PERCENT ) ) } ; } private static calculateXY ( { x , y } , percentage ) { return { x : x * percentage , y : y * percentage } ; } } export default Gestures ;