import { WebElement } from 'selenium-webdriver' ; /**
 * Hide or remove elements on the page
 */ export default function hideRemoveElements ( hideRemoveElements , hideRemove ) { const visitedSelectors = { } ; hideRemoveElements . hide . forEach ( element => { if ( Array . isArray ( element ) ) { return element . forEach ( ( singleElement ) => hideRemoveEl ( singleElement , 'visibility' , hideRemove ) ) ; } hideRemoveEl ( element , 'visibility' , hideRemove , true ) ; } ) ; hideRemoveElements . remove . forEach ( element => { if ( Array . isArray ( element ) ) { return element . forEach ( ( singleElement ) => hideRemoveEl ( singleElement , 'display' , hideRemove ) ) ; } hideRemoveEl ( element , 'display' , hideRemove , true ) ; } ) ; function hideRemoveEl ( el , prop , hideRemove , singleElement = false ) { if ( el . style ) { setPropertyToElement ( el , prop , hideRemove ) ; } else { try { const selector = el . selector ; if ( visitedSelectors [ selector ] == null ) { visitedSelectors [ selector ] = true ; const elems = document . querySelectorAll ( selector ) ; elems . forEach ( singleEl => setPropertyToElement ( singleEl , prop , hideRemove ) ) ; } } catch ( e ) { return getElementsByXpath ( el . selector ) . forEach ( singleEl => setPropertyToElement ( singleEl , prop , hideRemove ) ) ; } } } function setPropertyToElement ( el , prop , hideRemove ) { const value = prop === 'visibility' ? 'hidden' : 'none' ; el . style [ prop ] = hideRemove ? value : '' ; } function getElementsByXpath ( xpathToExecute ) { const result = [ ] ; const nodesSnapshot = document . evaluate ( xpathToExecute , document , null , XPathResult . ORDERED_NODE_SNAPSHOT_TYPE , null ) ; for ( let i = 0 ; i < nodesSnapshot . snapshotLength ; i ++ ) { result . push ( nodesSnapshot . snapshotItem ( i ) ) ; } return result ; } }