2018-10-25 14:44:03 +00:00
/* eslint no-invalid-this: "off" */
2019-12-31 11:00:16 +00:00
import { url as defaultURL } from './config' ;
2019-10-28 16:31:33 +00:00
let actions = {
2019-12-31 11:00:16 +00:00
clickIfExists : async function ( selector ) {
let exists ;
try {
exists = await this . waitForSelector ( selector , { timeout : 500 } ) ;
} catch ( error ) {
exists = false ;
}
if ( exists ) await this . waitToClick ( selector ) ;
return exists ;
} ,
2019-10-18 19:36:30 +00:00
parsedUrl : async function ( ) {
return new URL ( await this . url ( ) ) ;
} ,
2019-10-28 16:31:33 +00:00
waitUntilNotPresent : async function ( selector ) {
await this . wait ( selector => {
return document . querySelector ( selector ) == null ;
} , selector ) ;
} ,
2019-10-15 14:19:35 +00:00
changeLanguageToEnglish : async function ( ) {
let langSelector = '.user-popover vn-autocomplete[ng-model="$ctrl.lang"]' ;
2019-12-12 07:37:35 +00:00
await this . waitToClick ( '#user' ) ;
await this . wait ( langSelector ) ;
let lang = await this . waitToGetProperty ( ` ${ langSelector } input ` , 'value' ) ;
2019-10-15 14:19:35 +00:00
if ( lang !== 'English' )
await this . autocompleteSearch ( langSelector , 'English' ) ;
2019-12-31 11:00:16 +00:00
await this . keyboard . press ( 'Escape' ) ;
2019-12-12 07:37:35 +00:00
await this . waitForSelector ( langSelector , { hidden : true } ) ;
} ,
2019-11-25 08:13:20 +00:00
2019-12-12 07:37:35 +00:00
doLogin : async function ( userName , password = 'nightmare' ) {
2019-12-31 11:00:16 +00:00
await this . wait ( ` vn-login [ng-model=" $ ctrl.user"] ` ) ;
await this . clearInput ( ` vn-login [ng-model=" $ ctrl.user"] ` ) ;
2020-01-02 10:37:25 +00:00
await this . write ( ` vn-login [ng-model=" $ ctrl.user"] ` , userName ) ;
2019-12-31 11:00:16 +00:00
await this . clearInput ( ` vn-login [ng-model=" $ ctrl.password"] ` ) ;
await this . write ( ` vn-login [ng-model=" $ ctrl.password"] ` , password ) ;
2019-12-12 07:37:35 +00:00
await this . click ( 'vn-login button[type=submit]' ) ;
2019-10-18 19:36:30 +00:00
} ,
2019-10-15 14:19:35 +00:00
login : async function ( userName ) {
2020-01-14 08:20:14 +00:00
try {
await this . waitForURL ( '#!/login' ) ;
} catch ( e ) {
await this . goto ( ` ${ defaultURL } /#!/login ` ) ;
2019-12-31 11:00:16 +00:00
}
2020-01-14 08:20:14 +00:00
await this . doLogin ( userName ) ;
await this . wait ( ( ) => {
return document . location . hash === '#!/' ;
} , { } ) ;
await this . changeLanguageToEnglish ( ) ;
2019-10-15 14:19:35 +00:00
} ,
selectModule : async function ( moduleName ) {
let snakeName = moduleName . replace ( /[\w]([A-Z])/g , m => {
return m [ 0 ] + '-' + m [ 1 ] ;
} ) . toLowerCase ( ) ;
2019-12-12 07:37:35 +00:00
let selector = ` vn-home a[ui-sref=" ${ moduleName } .index"] ` ;
await this . waitToClick ( selector ) ;
await this . waitForURL ( snakeName ) ;
2019-10-15 14:19:35 +00:00
} ,
loginAndModule : async function ( userName , moduleName ) {
2019-12-12 07:37:35 +00:00
await this . login ( userName ) ;
await this . selectModule ( moduleName ) ;
2019-10-15 14:19:35 +00:00
} ,
datePicker : async function ( selector , changeMonth , day ) {
let date = new Date ( ) ;
if ( changeMonth ) date . setMonth ( date . getMonth ( ) + changeMonth ) ;
date . setDate ( day ? day : 16 ) ;
date = date . toISOString ( ) . substr ( 0 , 10 ) ;
2019-12-12 07:37:35 +00:00
await this . wait ( selector ) ;
await this . evaluate ( ( selector , date ) => {
let input = document . querySelector ( selector ) . $ctrl . input ;
input . value = date ;
input . dispatchEvent ( new Event ( 'change' ) ) ;
} , selector , date ) ;
2019-10-15 14:19:35 +00:00
} ,
pickTime : async function ( selector , time ) {
2019-12-12 07:37:35 +00:00
await this . wait ( selector ) ;
await this . evaluate ( ( selector , time ) => {
let input = document . querySelector ( selector ) . $ctrl . input ;
input . value = time ;
input . dispatchEvent ( new Event ( 'change' ) ) ;
} , selector , time ) ;
} ,
clearTextarea : async function ( selector ) {
await this . wait ( selector ) ;
await this . input . evaluate ( inputSelector => {
return document . querySelector ( inputSelector ) . value = '' ;
} , selector ) ;
2019-05-03 15:49:38 +00:00
} ,
2018-10-31 10:58:10 +00:00
2019-12-12 07:37:35 +00:00
clearInput : async function ( selector ) {
await this . wait ( selector ) ;
2019-12-31 11:00:16 +00:00
let field = await this . evaluate ( selector => {
2020-01-02 10:37:25 +00:00
return document . querySelector ( ` ${ selector } input ` ) . closest ( '.vn-field' ) . $ctrl . field ;
2019-12-12 07:37:35 +00:00
} , selector ) ;
2020-01-14 08:20:14 +00:00
if ( ( field != null && field != '' ) || field == '0' ) {
2019-12-31 11:00:16 +00:00
let coords = await this . evaluate ( selector => {
let rect = document . querySelector ( selector ) . getBoundingClientRect ( ) ;
2020-01-09 12:07:16 +00:00
return { x : rect . x + ( rect . width / 2 ) , y : rect . y + ( rect . height / 2 ) , width : rect . width } ;
2019-12-31 11:00:16 +00:00
} , selector ) ;
await this . mouse . move ( coords . x , coords . y ) ;
2020-01-16 12:40:51 +00:00
await this . waitForSelector ( ` ${ selector } [icon="clear"] ` , { visible : true } ) ;
2019-12-31 11:00:16 +00:00
await this . waitToClick ( ` ${ selector } [icon="clear"] ` ) ;
}
2020-01-16 12:40:51 +00:00
await this . evaluate ( selector => {
return document . querySelector ( ` ${ selector } input ` ) . closest ( '.vn-field' ) . $ctrl . field == '' ;
} , selector ) ;
2019-05-03 15:49:38 +00:00
} ,
2017-09-15 10:24:37 +00:00
2019-12-12 07:37:35 +00:00
getProperty : async function ( selector , property ) {
return await this . evaluate ( ( selector , property ) => {
2018-11-22 11:34:20 +00:00
return document . querySelector ( selector ) [ property ] . replace ( /\s+/g , ' ' ) . trim ( ) ;
2019-10-28 18:52:54 +00:00
} , selector , property ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitPropertyLength : async function ( selector , property , minLength ) {
await this . wait ( ( selector , property , minLength ) => {
2018-11-22 11:34:20 +00:00
const element = document . querySelector ( selector ) ;
return element && element [ property ] != null && element [ property ] !== '' && element [ property ] . length >= minLength ;
2019-12-12 07:37:35 +00:00
} , { } , selector , property , minLength ) ;
return await this . getProperty ( selector , property ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitPropertyValue : async function ( selector , property , status ) {
await this . wait ( selector ) ;
return await this . wait ( ( selector , property , status ) => {
const element = document . querySelector ( selector ) ;
return element [ property ] === status ;
} , { } , selector , property , status ) ;
2019-02-07 13:33:52 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitToGetProperty : async function ( selector , property ) {
await this . wait ( ( selector , property ) => {
2018-11-22 11:34:20 +00:00
const element = document . querySelector ( selector ) ;
2019-07-01 11:41:38 +00:00
2018-11-22 11:34:20 +00:00
return element && element [ property ] != null && element [ property ] !== '' ;
2019-12-12 07:37:35 +00:00
} , { } , selector , property ) ;
return await this . getProperty ( selector , property ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
write : async function ( selector , text ) {
2020-01-16 12:40:51 +00:00
await this . waitForSelector ( selector , { } ) ;
2019-12-31 11:00:16 +00:00
await this . type ( ` ${ selector } input ` , text ) ;
2020-01-16 12:40:51 +00:00
await this . waitForTextInInput ( selector , text ) ;
2018-11-22 11:34:20 +00:00
} ,
2017-09-15 10:24:37 +00:00
2019-12-12 07:37:35 +00:00
waitToClick : async function ( selector ) {
await this . wait ( selector ) ;
await this . click ( selector ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
focusElement : async function ( selector ) {
await this . wait ( selector ) ;
return await this . evaluate ( selector => {
let element = document . querySelector ( selector ) ;
element . focus ( ) ;
} , selector ) ;
2019-02-26 16:32:32 +00:00
} ,
2019-12-12 07:37:35 +00:00
isVisible : async function ( selector ) {
await this . wait ( selector ) ;
return await this . evaluate ( elementSelector => {
let selectorMatches = document . querySelectorAll ( elementSelector ) ;
let element = selectorMatches [ 0 ] ;
if ( selectorMatches . length > 1 )
throw new Error ( ` Multiple matches of ${ elementSelector } found ` ) ;
let isVisible = false ;
if ( element ) {
let eventHandler = event => {
event . preventDefault ( ) ;
isVisible = true ;
} ;
element . addEventListener ( 'mouseover' , eventHandler ) ;
let rect = element . getBoundingClientRect ( ) ;
let x = rect . left + rect . width / 2 ;
let y = rect . top + rect . height / 2 ;
let elementInCenter = document . elementFromPoint ( x , y ) ;
let elementInTopLeft = document . elementFromPoint ( rect . left , rect . top ) ;
let elementInBottomRight = document . elementFromPoint ( rect . right , rect . bottom ) ;
let e = new MouseEvent ( 'mouseover' , {
view : window ,
bubbles : true ,
cancelable : true ,
} ) ;
if ( elementInCenter )
elementInCenter . dispatchEvent ( e ) ;
if ( elementInTopLeft )
elementInTopLeft . dispatchEvent ( e ) ;
if ( elementInBottomRight )
elementInBottomRight . dispatchEvent ( e ) ;
element . removeEventListener ( 'mouseover' , eventHandler ) ;
}
return isVisible ;
} , selector ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitImgLoad : async function ( selector ) {
await this . wait ( selector ) ;
return await this . wait ( selector => {
const imageReady = document . querySelector ( selector ) . complete ;
return imageReady ;
2020-01-09 12:07:16 +00:00
} , { } , selector ) ;
2019-02-07 08:07:00 +00:00
} ,
2019-12-12 07:37:35 +00:00
clickIfVisible : async function ( selector ) {
await this . wait ( selector ) ;
let isVisible = await this . isVisible ( selector ) ;
if ( isVisible )
return await this . click ( selector ) ;
2019-02-04 13:47:55 +00:00
2019-12-12 07:37:35 +00:00
throw new Error ( ` invisible selector: ${ selector } ` ) ;
2019-02-04 13:47:55 +00:00
} ,
2019-12-12 07:37:35 +00:00
countElement : async function ( selector ) {
return await this . evaluate ( selector => {
2018-11-22 11:34:20 +00:00
return document . querySelectorAll ( selector ) . length ;
2019-10-28 18:52:54 +00:00
} , selector ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForNumberOfElements : async function ( selector , count ) {
return await this . wait ( ( selector , count ) => {
2018-11-22 11:34:20 +00:00
return document . querySelectorAll ( selector ) . length === count ;
2019-12-12 07:37:35 +00:00
} , { } , selector , count ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForClassNotPresent : async function ( selector , className ) {
await this . wait ( selector ) ;
return await this . wait ( ( selector , className ) => {
if ( ! document . querySelector ( selector ) . classList . contains ( className ) )
return true ;
} , { } , selector , className ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForClassPresent : async function ( selector , className ) {
await this . wait ( selector ) ;
return await this . wait ( ( elementSelector , targetClass ) => {
if ( document . querySelector ( elementSelector ) . classList . contains ( targetClass ) )
return true ;
} , { } , selector , className ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForTextInElement : async function ( selector , text ) {
await this . wait ( selector ) ;
return await this . wait ( ( selector , text ) => {
return document . querySelector ( selector ) . innerText . toLowerCase ( ) . includes ( text . toLowerCase ( ) ) ;
} , { } , selector , text ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForTextInInput : async function ( selector , text ) {
await this . wait ( selector ) ;
return await this . wait ( ( selector , text ) => {
2020-01-02 10:37:25 +00:00
return document . querySelector ( ` ${ selector } input ` ) . value . toLowerCase ( ) . includes ( text . toLowerCase ( ) ) ;
2019-12-12 07:37:35 +00:00
} , { } , selector , text ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForInnerText : async function ( selector ) {
await this . wait ( selector ) ;
await this . wait ( selector => {
const innerText = document . querySelector ( selector ) . innerText ;
return innerText != null && innerText != '' ;
} , selector ) ;
return await this . evaluate ( selector => {
return document . querySelector ( selector ) . innerText ;
} , selector ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForEmptyInnerText : async function ( selector ) {
return await this . wait ( selector => {
2018-11-22 11:34:20 +00:00
return document . querySelector ( selector ) . innerText == '' ;
2019-10-28 16:31:33 +00:00
} , selector ) ;
2018-11-22 11:34:20 +00:00
} ,
2018-09-10 08:23:26 +00:00
2019-12-12 07:37:35 +00:00
waitForURL : async function ( hashURL ) {
2020-01-16 12:40:51 +00:00
await this . waitForFunction ( expectedHash => {
2019-12-12 07:37:35 +00:00
return document . location . hash . includes ( expectedHash ) ;
} , { } , hashURL ) ;
2018-11-22 11:34:20 +00:00
} ,
2019-12-31 11:00:16 +00:00
hideSnackbar : async function ( ) {
await this . waitToClick ( '#shapes .shown button' ) ;
2018-12-02 23:45:34 +00:00
} ,
2018-11-23 07:05:57 +00:00
2019-12-12 07:37:35 +00:00
waitForLastShape : async function ( selector ) {
await this . wait ( selector ) ;
2019-12-31 11:00:16 +00:00
let snackBarText = await this . evaluate ( selector => {
2019-12-12 07:37:35 +00:00
const shape = document . querySelector ( selector ) ;
2019-01-07 08:33:07 +00:00
2019-12-12 07:37:35 +00:00
return shape . innerText ;
} , selector ) ;
2019-12-31 11:00:16 +00:00
await this . hideSnackbar ( ) ;
return snackBarText ;
2019-01-16 07:52:56 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForLastSnackbar : async function ( ) {
2019-12-31 11:00:16 +00:00
await this . wait ( 2000 ) ; // this needs a refactor to be somehow dynamic ie: page.waitForResponse(urlOrPredicate[, options]) or something to fire waitForLastShape once the request is completed
2019-12-12 07:37:35 +00:00
await this . waitForSpinnerLoad ( ) ;
2019-12-31 11:00:16 +00:00
return await this . waitForLastShape ( 'vn-snackbar .shown .text' ) ;
2019-12-12 07:37:35 +00:00
} ,
accessToSearchResult : async function ( searchValue ) {
2019-12-31 11:00:16 +00:00
await this . clearInput ( 'vn-searchbar' ) ;
await this . write ( 'vn-searchbar' , searchValue ) ;
await this . waitToClick ( 'vn-searchbar vn-icon[icon="search"]' ) ;
2019-12-12 07:37:35 +00:00
await this . waitForNumberOfElements ( '.search-result' , 1 ) ;
return await this . waitToClick ( 'ui-view vn-card a' ) ;
} ,
accessToSection : async function ( sectionRoute ) {
await this . wait ( ` vn-left-menu ` ) ;
2019-12-31 11:00:16 +00:00
let nested = await this . evaluate ( sectionRoute => {
2019-12-12 07:37:35 +00:00
return document . querySelector ( ` vn-left-menu li li > a[ui-sref=" ${ sectionRoute } "] ` ) != null ;
} , sectionRoute ) ;
2019-12-31 11:00:16 +00:00
2019-12-12 07:37:35 +00:00
if ( nested ) {
await this . waitToClick ( 'vn-left-menu vn-item-section > vn-icon[icon=keyboard_arrow_down]' ) ;
await this . wait ( 'vn-left-menu .expanded' ) ;
}
2019-12-31 11:00:16 +00:00
await this . evaluate ( sectionRoute => {
let navButton = document . querySelector ( ` vn-left-menu li > a[ui-sref=" ${ sectionRoute } "] ` ) ;
navButton . scrollIntoViewIfNeeded ( ) ;
return navButton . click ( ) ;
} , sectionRoute ) ;
2020-01-16 12:40:51 +00:00
await this . waitForNavigation ( { waitUntil : [ 'networkidle0' ] } ) ;
2019-12-12 07:37:35 +00:00
} ,
2020-01-16 12:40:51 +00:00
autocompleteSearch : async function ( selector , searchValue ) {
try {
await this . waitToClick ( ` ${ selector } input ` ) ,
await this . waitForSelector ( selector => {
document
. querySelector ( ` ${ selector } vn-drop-down ` ) . $ctrl . content
. querySelectorAll ( 'li' ) ;
} , selector ) ;
await this . write ( ` .vn-drop-down.shown ` , searchValue ) ,
await this . waitForFunction ( selector => {
return document
. querySelector ( ` ${ selector } vn-drop-down ` ) . $ctrl . content
. querySelector ( 'li.active' ) ;
} , { } , selector ) ;
await this . keyboard . press ( 'Enter' ) ;
await this . waitForFunction ( ( selector , searchValue ) => {
return document . querySelector ( ` ${ selector } input ` ) . value . toLowerCase ( )
. includes ( searchValue . toLowerCase ( ) ) ;
} , { } , selector , searchValue ) ;
} catch ( error ) {
throw new Error ( ` ${ selector } failed to autocomplete ${ searchValue } ! ${ error } ` ) ;
}
await this . waitForMutation ( ` .vn-drop-down ` , 'childList' ) ;
2019-12-12 07:37:35 +00:00
} ,
reloadSection : async function ( sectionRoute ) {
2019-12-31 11:00:16 +00:00
await this . evaluate ( ( ) => {
let summayButton = document . querySelector ( 'vn-icon[icon="desktop_windows"]' ) ;
summayButton . scrollIntoViewIfNeeded ( ) ;
return summayButton . click ( ) ;
} ) ;
2019-12-12 07:37:35 +00:00
await this . wait ( 'vn-card.summary' ) ;
await this . waitToClick ( ` vn-left-menu li > a[ui-sref=" ${ sectionRoute } "] ` ) ;
} ,
forceReloadSection : async function ( sectionRoute ) {
await this . waitToClick ( 'vn-icon[icon="desktop_windows"]' ) ;
await this . waitToClick ( 'button[response="accept"]' ) ;
await this . wait ( 'vn-card.summary' ) ;
await this . waitToClick ( ` vn-left-menu li > a[ui-sref=" ${ sectionRoute } "] ` ) ;
} ,
checkboxState : async function ( selector ) {
await this . wait ( selector ) ;
return await this . evaluate ( selector => {
let checkbox = document . querySelector ( selector ) ;
switch ( checkbox . $ctrl . field ) {
case null :
return 'intermediate' ;
case true :
return 'checked' ;
default :
return 'unchecked' ;
}
} , selector ) ;
2019-01-30 10:09:53 +00:00
} ,
2019-12-12 07:37:35 +00:00
isDisabled : async function ( selector ) {
await this . wait ( selector ) ;
return await this . evaluate ( selector => {
let element = document . querySelector ( selector ) ;
return element . $ctrl . disabled ;
} , selector ) ;
2019-02-14 19:17:22 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForStylePresent : async function ( selector , property , value ) {
return await this . wait ( ( selector , property , value ) => {
2019-11-12 07:51:50 +00:00
const element = document . querySelector ( selector ) ;
return element . style [ property ] == value ;
2019-12-12 07:37:35 +00:00
} , { } , selector , property , value ) ;
2019-06-27 13:33:15 +00:00
} ,
2019-11-12 07:51:50 +00:00
2019-12-12 07:37:35 +00:00
waitForSpinnerLoad : async function ( ) {
await this . waitUntilNotPresent ( 'vn-topbar vn-spinner' ) ;
2019-11-19 13:33:25 +00:00
} ,
2019-12-12 07:37:35 +00:00
waitForWatcherData : async function ( selector ) {
await this . wait ( selector ) ;
await this . wait ( selector => {
2019-12-31 11:00:16 +00:00
let watcher = document . querySelector ( selector ) ;
2019-12-12 07:37:35 +00:00
let orgData = watcher . $ctrl . orgData ;
return ! angular . equals ( { } , orgData ) && orgData != null ;
2019-12-31 11:00:16 +00:00
} , { } , selector ) ;
2019-12-12 07:37:35 +00:00
await this . waitForSpinnerLoad ( ) ;
2020-01-09 12:07:16 +00:00
} ,
2020-01-16 12:40:51 +00:00
waitForMutation : async function ( selector , type ) {
try {
await this . evaluate ( ( selector , type ) => {
return new Promise ( resolve => {
const config = { attributes : true , childList : true , subtree : true } ;
const target = document . querySelector ( selector ) ;
const onEnd = function ( mutationsList , observer ) {
resolve ( ) ;
observer . disconnect ( ) ;
} ;
const observer = new MutationObserver ( onEnd ) ;
observer . expectedType = type ;
observer . observe ( target , config ) ;
} ) ;
} , selector , type ) ;
} catch ( error ) {
throw new Error ( ` failed to wait for mutation type: ${ type } ` ) ;
}
} ,
2020-01-09 12:07:16 +00:00
waitForTransitionEnd : async function ( selector ) {
await this . evaluate ( selector => {
return new Promise ( resolve => {
const transition = document . querySelector ( selector ) ;
const onEnd = function ( ) {
transition . removeEventListener ( 'transitionend' , onEnd ) ;
resolve ( ) ;
} ;
transition . addEventListener ( 'transitionend' , onEnd ) ;
} ) ;
} , selector ) ;
2020-01-14 08:20:14 +00:00
} ,
waitForContentLoaded : async function ( ) {
await this . evaluate ( ( ) => {
return new Promise ( resolve => {
const $rootScope = angular . element ( document . body ) . injector ( ) . get ( '$rootScope' ) ;
$rootScope . $on ( '$viewContentLoaded' , resolve ( ) ) ;
} ) ;
} ) ;
2019-11-12 07:51:50 +00:00
}
2018-11-22 11:34:20 +00:00
} ;
2018-09-10 08:23:26 +00:00
2019-12-12 07:37:35 +00:00
export function extendPage ( page ) {
for ( let name in actions ) {
page [ name ] = async ( ... args ) => {
return await actions [ name ] . call ( page , ... args ) ;
} ;
}
page . wait = page . waitFor ;
return page ;
}
2019-11-25 08:13:20 +00:00
export default actions ;