import { browser , element , by , ExpectedConditions } from 'protractor' ; import { protractor } from 'protractor/built/ptor' ; import * as path from 'path' ; import * as fs from 'fs' ; let LForms = ( global as any ) . LForms ; const TestUtil = { param elemLoc elementFactory : function ( elemLoc ) { if ( typeof elemLoc === 'string' ) elemLoc = by . id ( elemLoc ) ; return function ( ) { return element ( elemLoc ) ; } ; } , param field param value return waitForValue : function ( field , value ) { return browser . wait ( function ( ) { return field . getAttribute ( 'value' ) . then ( function ( val ) { return val === value ; } ) ; } , 5000 ) ; } , param field returns getFieldValue : function ( field ) { return field . getWebElement ( ) . then ( ( element ) => browser . executeScript ( ( el ) => { return el . value ; } , element ) ) ; } , param field param attrName returns getAttribute : function ( field , attrName ) { return field . getWebElement ( ) . then ( ( element ) => browser . executeScript ( ( el , attr ) => { return el [ attr ] ; } , element , attrName ) ) ; } , sendKeys ( field , value ) { field . sendKeys ( value ) ; } , param field param str return _sendKeys : function ( field , str ) { const self = this ; str = '' + str ; return field . getAttribute ( 'value' ) . then ( function ( oldVal ) { var allButLastChar = oldVal + str . slice ( 0 , - 1 ) ; browser . executeScript ( 'arguments[0].value = "' + allButLastChar + '"' , field . getWebElement ( ) ) . then ( function success ( ) { self . waitForValue ( field , allButLastChar ) ; field . sendKeys ( str . slice ( - 1 ) ) ; return self . waitForValue ( field , oldVal + str ) ; } , function rejected ( ) { return field . sendKeys ( str ) ; } ) ; } ) ; } , param field return clearField : function ( field ) { field . clear ( ) ; return this . waitForValue ( field , '' ) ; } , param field clear : function ( field ) { field . click ( ) ; field . sendKeys ( protractor . Key . chord ( protractor . Key . CONTROL , "a" ) ) ; field . sendKeys ( protractor . Key . BACK_SPACE ) ; field . clear ( ) ; } , param field return eraseField : function ( field ) { field . click ( ) ; field . sendKeys ( protractor . Key . CONTROL , 'a' ) ; field . sendKeys ( protractor . Key . BACK_SPACE ) ; return this . waitForValue ( field , '' ) ; } , /**
     *  Returns a promise that resolves when scrolling has stopped.
     */ waitForScrollStop : function ( ) { return browser . executeScript ( "return window.scrollY" ) . then ( originalScrollY => { return browser . sleep ( 100 ) . then ( ( ) => browser . executeScript ( "return window.scrollY" ) . then ( newScrollY => { console . log ( "originalScrollY=" + originalScrollY + "; newScrollY=" + newScrollY ) ; return ( originalScrollY == newScrollY ) ? true : this . waitForScrollStop ( ) ; } ) ) ; } ) ; } , param { ElementFinder } elementFinder return { Promise } scrollIntoView : function ( elementFinder ) { return elementFinder . getWebElement ( ) . then ( ( element ) => { return browser . executeScript ( function ( element ) { if ( element . scrollIntoViewIfNeeded ) { element . scrollIntoViewIfNeeded ( true ) ; } else { element . scrollIntoView ( { block : 'center' } ) ; } } , element ) . then ( ( ) => this . waitForScrollStop ( ) ) ; } ) ; } , param { ElementFinder } elementFinder return { Promise } safeClick : function ( elementFinder ) { return browser . wait ( ExpectedConditions . elementToBeClickable ( elementFinder ) , 5000 ) . then ( ( ) => this . scrollIntoView ( elementFinder ) . then ( ( ) => { console . log ( "Clicking " + elementFinder . locator ( ) . value ) ; return elementFinder . click ( ) ; } ) ) ; } , param field fieldListLength : function ( field ) { return browser . executeScript ( function ( htmlField ) { let size = 0 ; let autocomp = htmlField . autocomp ; if ( autocomp ) { let rawList = autocomp . rawList_ ; if ( rawList ) size = rawList . length ; } return size ; } , field . getWebElement ( ) ) ; } , param field fieldHasList : function ( field ) { return this . fieldListLength ( field ) . then ( ( length ) => { return length > 0 ; } ) ; } , /**
    *  Clicks the given add/remove repeating item button, and sleeps a bit to let the page stop moving.
    */ clickAddRemoveButton : function ( button ) { browser . executeScript ( 'arguments[0].scrollIntoView({behavior: "instant"})' , button . getWebElement ( ) ) ; browser . sleep ( 300 ) ; button . click ( ) ; browser . sleep ( 300 ) ; } , waitForElementVisible : function ( elem ) { browser . wait ( function ( ) { return ExpectedConditions . visibilityOf ( elem ) ; } , 3000 ) ; } , /**
     *  Waits for the given element to be present on the page.
     */ waitForElementPresent : function ( elem ) { browser . wait ( function ( ) { return browser . isElementPresent ( elem ) ; } , 30000 ) ; } , /**
     *  Waits for the given element to not be present on the page.
     */ waitForElementNotPresent : function ( elem ) { browser . wait ( function ( ) { return ExpectedConditions . not ( ExpectedConditions . presenceOf ( elem ) ) ; } , 3000 ) ; } , /**
     *  Waits for the given element to be displayed on the page.
     */ waitForElementDisplayed : function ( elem ) { this . waitForElementPresent ( elem ) ; browser . wait ( function ( ) { return elem . isDisplayed ( ) ; } , 3000 ) ; } , /**
     *  Waits for the given element to be not displayed on the page.
     */ waitForElementNotDisplayed : function ( elem ) { browser . wait ( function ( ) { return elem . isPresent ( ) . then ( function ( result ) { if ( ! result ) return true ; else return elem . isDisplayed ( ) . then ( function ( result ) { return ! result ; } ) ; } ) ; } , 3000 ) ; } , /**
     *  Disables Autocompleter scrolling (by making the move immediate).
     */ disableAutocompleterScroll : function ( ) { let origStart = LForms . Def . Effect . Base . prototype . start ; LForms . Def . Effect . Scroll . prototype . start = function ( options ) { options . duration = 0 ; origStart . call ( this , options ) ; } ; } , /**
     *  Disables CSS animation.
     */ disableCssAnimate : function ( ) { var style = document . createElement ( 'style' ) ; style . type = 'text/css' ; style . innerHTML = '* {' + '-webkit-transition: none !important;' + '-moz-transition: none !important;' + '-o-transition: none !important;' + '-ms-transition: none !important;' + 'transition: none !important;' ; '}' ; document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( style ) ; } , param offsetMS return getCurrentDTMString : function ( offsetMS ) { offsetMS = offsetMS || 0 ; var date = new Date ( new Date ( ) . getTime ( ) + offsetMS ) ; return [ ( 101 + date . getMonth ( ) ) . toString ( ) . substr ( 1 ) , ( 100 + date . getDate ( ) ) . toString ( ) . substr ( 1 ) , ( 10000 + date . getFullYear ( ) ) . toString ( ) . substr ( 1 ) ] . join ( '/' ) + ' ' + [ ( 100 + date . getHours ( ) ) . toString ( ) . substr ( 1 ) , ( 100 + date . getMinutes ( ) ) . toString ( ) . substr ( 1 ) , "00" ] . join ( ':' ) ; } , param q param qr param elemID return showQQR : function ( q , qr , elemID ) { return browser . executeScript ( ( ) => { var q2 = arguments [ 0 ] ; var qr2 = arguments [ 1 ] ; var elemID = arguments [ 2 ] ; var lfd = LForms . Util . convertFHIRQuestionnaireToLForms ( q2 , 'R4' ) ; var merged = LForms . Util . mergeFHIRDataIntoLForms ( 'QuestionnaireResponse' , qr2 , lfd , 'R4' ) ; var formElem = document . getElementById ( elemID ) ; merged = new LForms . LFormsData ( merged ) ; LForms . Util . addFormToPage ( merged , elemID ) ; } , q , qr , elemID ) ; } , param fileName param content return createTempFile : function ( fileName , content ) { const tmp = require ( 'tmp' ) ; tmp . setGracefulCleanup ( ) ; var dirObj = tmp . dirSync ( { prefix : 'lformsTest2' , unsafeCleanup : true } ) ; var dirPath = dirObj . name ; var pathName = path . join ( dirPath , fileName ) ; fs . writeFileSync ( pathName , content ) ; return pathName ; } , param element param noFormDefData param noEmptyValue param noDisabledItem returns getUserData : function ( element = null , noFormDefData = false , noEmptyValue = false , noDisabledItem = false ) { return browser . driver . executeAsyncScript ( function ( ) { var callback = arguments [ arguments . length - 1 ] ; var lfData ; switch ( arguments . length ) { case 5 : lfData = LForms . Util . getUserData ( arguments [ 0 ] , arguments [ 1 ] , arguments [ 2 ] , arguments [ 3 ] ) ; break ; case 4 : lfData = LForms . Util . getUserData ( arguments [ 0 ] , arguments [ 1 ] , arguments [ 2 ] ) ; break ; case 3 : lfData = LForms . Util . getUserData ( arguments [ 0 ] , arguments [ 1 ] ) ; break ; case 2 : lfData = LForms . Util . getUserData ( arguments [ 0 ] ) ; break ; case 1 : lfData = LForms . Util . getUserData ( ) ; break ; default : lfData = LForms . Util . getUserData ( ) ; } callback ( lfData ) ; } ) ; } , /**
     * Wait for the externally linked FHIR libraries to be loaded.
     */ waitForFHIRLibsLoaded ( ) { browser . wait ( function ( ) { return browser . driver . executeScript ( 'return typeof(LForms.FHIR) === "object" && typeof(LForms.FHIR.R4) === "object" && typeof(LForms.FHIR.STU3) === "object"' ) ; } , 5000 ) ; } } ; export default TestUtil ;