import logger from '@wdio/logger' ; import { DEFAULT_TIMEOUT } from './constants' ; import { assertIdDefined , Platform , UNSUPPORTED_PLATFORM_ERROR , } from './internal/utils' ; import { acceptAlert } from './alert' ; import { APP_RUNNING_STATE , isAppState , openSafari } from './appUtils' ; import { mobile$ } from './select/select' ; import { Type } from './select/type' ; import { Selector } from './select/selector' ; import { IOS_PREDICATE_ATTRIBUTES , IOS_PREDICATE_COMPARATOR , IosSelector , } from './select/iosSelector' ; const log = logger ( 'Deeplink' ) ; internal function openDeeplinkAlert ( timeout ) { try { acceptAlert ( ) ; } catch ( e ) { log . info ( 'Appium version is below 1.17.0, deeplink only works on English iOS devices! Support for Appium <1.17.0 will be dropped in the future.' ) ; const openButton = $ ( '~Open' ) ; openButton . waitForDisplayed ( { timeout : timeout } ) ; openButton . click ( ) ; } } internal function openDeeplinkIos ( deeplink , bundleId , timeout ) { log . info ( 'Opening Deeplink on iOS' ) ; log . trace ( 'Launching Safari' ) ; openSafari ( ) ; browser . execute ( 'mobile: terminateApp' , { bundleId : bundleId } ) ; isAppState ( APP_RUNNING_STATE . NOT_RUNNING , true , undefined , bundleId , timeout ) ; const nameContainsUrl = Selector . custom ( null , IosSelector . of ( IOS_PREDICATE_ATTRIBUTES . NAME , IOS_PREDICATE_COMPARATOR . EQUALS , 'URL' ) ) ; const urlButton = mobile$ ( Selector . and ( Selector . or ( Selector . type ( Type . BUTTON ) , Selector . type ( Type . TEXT_FIELD ) ) , nameContainsUrl ) ) ; urlButton . waitForDisplayed ( { timeout : timeout } ) ; urlButton . click ( ) ; const urlField = mobile$ ( Selector . and ( Selector . type ( Type . TEXT_FIELD ) , nameContainsUrl ) ) ; urlField . setValue ( deeplink + '\uE007' ) ; openDeeplinkAlert ( timeout ) ; log . trace ( 'Open button was clicked on Alert' ) ; if ( isAppState ( APP_RUNNING_STATE . FOREGROUND , true , undefined , bundleId , timeout ) ) { log . info ( 'App was opened successfully via deeplink and is running in the foreground' ) ; } else { const message = 'Could not find an open button for deeplink: ' + deeplink ; log . error ( message ) ; throw new Error ( message ) ; } } internal function openDeeplinkAndroid ( path , appId ) { log . info ( 'Opening Deeplink on Android' ) ; browser . closeApp ( ) ; browser . execute ( 'mobile: deepLink' , { url : path , package : appId , } ) ; } note param { string } path param { string } appId param { string } bundleId param { number } timeout category export function openDeeplink ( path , appId ? , bundleId ? , timeout = DEFAULT_TIMEOUT ) { log . trace ( 'openDeeplink: ' + path ) ; if ( browser . isIOS ) { assertIdDefined ( bundleId , Platform . IOS ) ; openDeeplinkIos ( path , bundleId as string , timeout ) ; } else if ( browser . isAndroid ) { assertIdDefined ( appId , Platform . ANDROID ) ; openDeeplinkAndroid ( path , appId as string ) ; } else { throw new Error ( UNSUPPORTED_PLATFORM_ERROR ) ; } }