import * as playwright from "playwright" ; import { isPageElement } from "../domain/SharedLogic" ; import { D365Selectors } from "../domain/D365Selectors" ; import { EnsureXrmGetter } from "./Global" ; import { XrmUiTest } from "./XrmUITest" ; /**
 * Define behavior on navigation, for example handling of blocking popups (e.g. "Do you really want to leave this page?"")
 */ export interface NavigationSettings { /**
     * Define whether to confirm or to cancel dialogs that occur on navigation
     */ popUpAction ? : "confirm" | "cancel" ; } /**
 * Define behaviour for opening forms
 */ export interface FormNavigationSettings extends NavigationSettings { /**
     * Define the ID of the form to open
     */ formId ? : string ; } /**
 * Module for navigating in D365
 */ export class Navigation { private _page : playwright . Page ; constructor ( private xrmUiTest ) { this . _page = xrmUiTest . page ; this . xrmUiTest = xrmUiTest ; } private async HandlePopUpOnNavigation ( navigationPromise , settings ) { const defaultSettings = { popUpAction : "cancel" } ; const safeSettings = { ... defaultSettings , ... settings } ; const popUpButton = safeSettings . popUpAction === "cancel" ? D365Selectors . PopUp . cancel : D365Selectors . PopUp . confirm ; const result = await Promise . race ( [ navigationPromise , this . _page . waitForSelector ( popUpButton , { timeout : this . xrmUiTest . settings . timeout } ) ] ) ; if ( isPageElement ( result ) ) { await Promise . all ( [ navigationPromise , result . click ( ) ] ) ; } } param entityName param settings returns openCreateForm = async ( entityName , settings ? ) => { await EnsureXrmGetter ( this . _page ) ; const navigationPromise = this . _page . evaluate ( ( [ entityName , formId ] ) => { const xrm = window . oss_FindXrm ( ) ; return xrm . Navigation . openForm ( { entityName : entityName , formId : formId ? formId : undefined } ) ; } , [ entityName , settings ?. formId ] ) ; await this . HandlePopUpOnNavigation ( navigationPromise , settings ) ; await this . xrmUiTest . waitForIdleness ( ) ; } ; param entityName param entityId param settings returns openUpdateForm = async ( entityName , entityId , settings ? ) => { await EnsureXrmGetter ( this . _page ) ; const navigationPromise = this . _page . evaluate ( ( [ entityName , entityId , formId ] ) => { const xrm = window . oss_FindXrm ( ) ; return xrm . Navigation . openForm ( { entityName : entityName , entityId : entityId , formId : formId ? formId : undefined } ) ; } , [ entityName , entityId , settings ?. formId ] ) ; await this . HandlePopUpOnNavigation ( navigationPromise , settings ) ; await this . xrmUiTest . waitForIdleness ( ) ; } ; param pageInput param navigationOptions param settings returns navigateTo = async ( pageInput , navigationOptions ? , settings ? ) => { await EnsureXrmGetter ( this . _page ) ; const navigationPromise = this . _page . evaluate ( ( [ pageInput , navigationOptions ] ) => { const xrm = window . oss_FindXrm ( ) ; return xrm . Navigation . navigateTo ( pageInput , navigationOptions ) ; } , [ pageInput , navigationOptions ] as [ Xrm . Navigation . PageInputEntityRecord | Xrm . Navigation . PageInputEntityList | Xrm . Navigation . PageInputHtmlWebResource , Xrm . Navigation . NavigationOptions ] ) ; await this . HandlePopUpOnNavigation ( navigationPromise , settings ) ; await this . xrmUiTest . waitForIdleness ( ) ; } ; param entityName returns openQuickCreate = async ( entityName ) => { await EnsureXrmGetter ( this . _page ) ; await this . _page . evaluate ( ( entityName ) => { const xrm = window . oss_FindXrm ( ) ; return xrm . Navigation . openForm ( { entityName : entityName , useQuickCreateForm : true } ) ; } , entityName ) ; await this . xrmUiTest . waitForIdleness ( ) ; } ; param appId param settings returns openAppById = async ( appId , settings ? ) => { this . xrmUiTest . AppId = appId ; const navigationUrl = this . xrmUiTest . buildUrl ( this . xrmUiTest . crmUrl , appId ) ; const navigationPromise = this . _page . goto ( navigationUrl , { waitUntil : "load" , timeout : this . xrmUiTest . settings . timeout } ) ; await this . HandlePopUpOnNavigation ( navigationPromise , settings ) ; await this . xrmUiTest . waitForIdleness ( ) ; } ; }