import * as playwright from "playwright" ; import { D365Selectors } from "../domain/D365Selectors" ; import { EnsureXrmGetter } from "./Global" ; import { XrmUiTest } from "./XrmUITest" ; import { RethrownError } from "../utils/RethrownError" ; /**
 * Module for interacting with D365 entity records
 */ export class Entity { private _page : playwright . Page ; constructor ( private xrmUiTest ) { this . _page = xrmUiTest . page ; this . xrmUiTest = xrmUiTest ; } returns deprecated noSubmit = async ( ) => { await EnsureXrmGetter ( this . _page ) ; return this . _page . evaluate ( ( ) => { const xrm = window . oss_FindXrm ( ) ; const attributes = xrm . Page . getAttribute ( ) ; attributes . forEach ( a => a . setSubmitMode ( "never" ) ) ; } ) ; } ; param saveData refresh = async ( saveData , ignoreDuplicateCheck = false ) => { await EnsureXrmGetter ( this . _page ) ; const refreshPromise = this . _page . evaluate ( ( [ save ] ) => { const xrm = window . oss_FindXrm ( ) ; return xrm . Page . data . refresh ( save ) ; } , [ saveData ] ) ; const promises = [ refreshPromise , this . xrmUiTest . waitForIdleness ( ) ] ; return this . handleDuplicateCheck ( promises , ignoreDuplicateCheck ) ; } ; handleDuplicateCheck = async ( promises , ignoreDuplicateCheck = false ) => { await Promise . race ( [ ... promises , this . _page . waitForSelector ( D365Selectors . DuplicateDetection . ignore , { timeout : this . xrmUiTest . settings . timeout } ) ] ) ; const duplicateCheckButton = await this . _page . $ ( D365Selectors . DuplicateDetection . ignore ) ; if ( duplicateCheckButton ) { if ( ignoreDuplicateCheck ) { await Promise . all ( [ duplicateCheckButton . click ( ) , Promise . race ( promises ) ] ) ; } else { await this . _page . click ( D365Selectors . DuplicateDetection . abort ) ; throw new Error ( "Duplicate records found. Pass true for save parameter 'ignoreDuplicateCheck' for ignore and saving" ) ; } } } ; param ignoreDuplicateCheck returns save = async ( ignoreDuplicateCheck = false ) => { try { await EnsureXrmGetter ( this . _page ) ; const waitSelectors = [ this . _page . waitForSelector ( "div[id^=quickcreate_]" , { timeout : this . xrmUiTest . settings . timeout } ) ] ; const saveResult = this . _page . evaluate ( ( ) => { const xrm = window . oss_FindXrm ( ) ; return new Promise ( ( resolve , reject ) => { xrm . Page . data . save ( ) . then ( resolve , reject ) ; } ) ; } ) ; const promises = [ ... waitSelectors , saveResult , this . xrmUiTest . waitForIdleness ( ) ] ; await this . handleDuplicateCheck ( promises , ignoreDuplicateCheck ) ; const quickCreate = await this . _page . $ ( "div[id^=quickcreate_]" ) ; if ( quickCreate ) { const handle = await quickCreate . getProperty ( "id" ) ; const id = await handle . jsonValue ( ) ; return id . substr ( 12 ) ; } return this . getId ( ) ; } catch ( e ) { throw new RethrownError ( `Error while saving, message: ${ ( e as any ) . title } - ${ ( e as any ) . message }` , e ) ; } } ; returns getId = async ( ) => { await EnsureXrmGetter ( this . _page ) ; return this . _page . evaluate ( ( ) => { const xrm = window . oss_FindXrm ( ) ; return xrm . Page . data . entity . getId ( ) ; } ) ; } ; returns getEntityName = async ( ) => { await EnsureXrmGetter ( this . _page ) ; return this . _page . evaluate ( ( ) => { const xrm = window . oss_FindXrm ( ) ; return xrm . Page . data . entity . getEntityName ( ) ; } ) ; } ; returns getEntityReference = async ( ) => { await EnsureXrmGetter ( this . _page ) ; return this . _page . evaluate ( ( ) => { const xrm = window . oss_FindXrm ( ) ; return xrm . Page . data . entity . getEntityReference ( ) ; } ) ; } ; returns remarks delete = async ( ) => { await this . xrmUiTest . Button . click ( { custom : "li[id*='DeletePrimaryRecord']" } ) ; await Promise . all ( [ this . _page . waitForNavigation ( { waitUntil : "load" , timeout : this . xrmUiTest . settings . timeout } ) , this . _page . click ( D365Selectors . PopUp . confirm , { timeout : this . xrmUiTest . settings . timeout } ) ] ) ; await this . xrmUiTest . waitForIdleness ( ) ; } ; returns remarks deactivate = async ( ) => { await this . xrmUiTest . Button . click ( { custom : "li[id*='Mscrm.Form.Deactivate']" } ) ; await this . _page . click ( "button[data-id='ok_id']" , { timeout : this . xrmUiTest . settings . timeout } ) ; await this . xrmUiTest . waitForIdleness ( ) ; } ; returns remarks activate = async ( ) => { await this . xrmUiTest . Button . click ( { custom : "li[id*='Mscrm.Form.Activate']" } ) ; await this . _page . click ( "button[data-id='ok_id']" , { timeout : this . xrmUiTest . settings . timeout } ) ; await this . xrmUiTest . waitForIdleness ( ) ; } ; }