import * as playwright from "playwright" ; import { RethrownError } from "../utils/RethrownError" ; import { EnsureXrmGetter } from "./Global" ; import { XrmUiTest } from "./XrmUITest" ; /**
 * Module for interacting with D365 Subgrids
 */ export class SubGrid { private _page : playwright . Page ; constructor ( private xrmUiTest ) { this . _page = xrmUiTest . page ; this . xrmUiTest = xrmUiTest ; } param { String } subgridName returns { Promise < number > } getRecordCount = async ( subgridName ) => { try { await EnsureXrmGetter ( this . _page ) ; return this . _page . evaluate ( ( name ) => { const xrm = window . oss_FindXrm ( ) ; const control = xrm . Page . getControl < Xrm . Controls . GridControl > ( name ) ; if ( ! control ) { return undefined ; } return control . getGrid ( ) . getTotalRecordCount ( ) ; } , subgridName ) ; } catch ( e ) { throw new RethrownError ( `Error when getting record count of subgrid '${ subgridName }'` , e ) ; } } ; param { String } subgridName param { Number } recordNumber returns { Promise < void > } openNthRecord = async ( subgridName , recordNumber ) => { try { await EnsureXrmGetter ( this . _page ) ; const recordReference = await this . _page . evaluate ( ( [ name , position ] ) => { const xrm = window . oss_FindXrm ( ) ; const control = xrm . Page . getControl < Xrm . Controls . GridControl > ( name ) ; if ( ! control ) { return undefined ; } const grid = control . getGrid ( ) ; const record = grid . getRows ( ) . get ( position ) . getData ( ) ; return record . getEntity ( ) . getEntityReference ( ) ; } , [ subgridName , recordNumber ] ) ; return this . xrmUiTest . Navigation . openUpdateForm ( recordReference . entityType , recordReference . id ) ; } catch ( e ) { throw new RethrownError ( `Error when setting opening record ${ recordNumber } of subgrid '${ subgridName }'` , e ) ; } } ; param { String } subgridName createNewRecord = async ( subgridName ) => { try { await EnsureXrmGetter ( this . _page ) ; const parentTab = await this . _page . evaluate ( ( [ name ] ) => { const xrm = window . oss_FindXrm ( ) ; const control = xrm . Page . getControl < Xrm . Controls . GridControl > ( name ) ; if ( ! control ) { return undefined ; } return control . getParent ( ) . getParent ( ) . getName ( ) ; } , [ subgridName ] ) ; await this . xrmUiTest . Tab . open ( parentTab ) ; const subgridEntity = await this . _page . evaluate ( ( [ name ] ) => { const xrm = window . oss_FindXrm ( ) ; const control = xrm . Page . getControl < Xrm . Controls . GridControl > ( name ) ; if ( ! control ) { return undefined ; } return control . getEntityName ( ) ; } , [ subgridName ] ) ; await this . xrmUiTest . waitForIdleness ( ) ; const addNewButton = await this . _page . $ ( `div[data-control-name='${ subgridName }'] button[data-id*='Mscrm.SubGrid.${ subgridEntity }.AddNewStandard']` ) ; if ( addNewButton ) { await addNewButton . click ( ) ; } else { const overflowButton = await this . _page . $ ( `div[data-control-name='${ subgridName }'] button[data-id*='OverflowButton']` ) ; if ( ! overflowButton ) { throw new Error ( "Failed to find the add new button on your subgrid as well as the overflow button. Please check user permissions, button hide rules and whether you use custom create buttons, as we search for the default create button" ) ; } await overflowButton . click ( ) ; await this . _page . click ( `ul[data-id='OverflowFlyout'] button[data-id*='Mscrm.SubGrid.${ subgridEntity }.AddNewStandard']` ) ; } return this . xrmUiTest . waitForIdleness ( ) ; } catch ( e ) { throw new RethrownError ( `Error when trying to create new record in subgrid '${ subgridName }'` , e ) ; } } ; param { String } subgridName returns { Promise < void > } refresh = async ( subgridName ) => { try { await EnsureXrmGetter ( this . _page ) ; return this . _page . evaluate ( ( name ) => { const xrm = window . oss_FindXrm ( ) ; const control = xrm . Page . getControl < Xrm . Controls . GridControl > ( name ) ; if ( ! control ) { return ; } return control . refresh ( ) ; } , subgridName ) ; } catch ( e ) { throw new RethrownError ( `Error when refreshing subgrid '${ subgridName }'` , e ) ; } } ; }