import { protractor , browser , by , element , ElementFinder , ElementArrayFinder , Locator , ExpectedConditions as until } from 'protractor' ; export async function getTitle ( ) { await browser . sleep ( 10 ) ; return await browser . getTitle ( ) ; } export async function getUrl ( ) { await browser . sleep ( 10 ) ; return await browser . getCurrentUrl ( ) ; } export async function getPath ( ) { await browser . sleep ( 10 ) ; return ( < string > await browser . getCurrentUrl ( ) ) . replace ( /^[^#]*?:\/\/.*?(\/.*)$/ , '$1' ) ; } export async function getNewTabUrlAndClose ( ) { await browser . sleep ( 10 ) ; const handles = await browser . getAllWindowHandles ( ) ; browser . driver . switchTo ( ) . window ( handles [ 1 ] ) ; const url = await browser . getCurrentUrl ( ) ; browser . driver . close ( ) ; browser . driver . switchTo ( ) . window ( handles [ 0 ] ) ; return url ; } export async function sendEscKey ( ) { return await ( await element ( by . css ( 'body' ) ) ) . sendKeys ( protractor . Key . ESCAPE ) ; } export async function isPresent ( elementFinder ) { return < boolean > await browser . isElementPresent ( elementFinder ) ; } export async function isDisplayed ( elementFinder ) { if ( ! await isPresent ( elementFinder ) ) { return false ; } return < boolean > await elementFinder . isDisplayed ( ) ; } export async function waitUntil ( elementFinder , timeout = 5000 ) { await browser . wait ( until . presenceOf ( elementFinder ) , timeout , `Element '${ elementFinder . locator ( ) }' not found in DOM` ) ; return < ElementFinder > elementFinder ; } export async function waitUntilCss ( selector , timeout = 5000 ) { return await waitUntil ( element ( by . css ( selector ) ) , timeout ) ; } type elementAccessor < T > = ( ( ) => Promise < T > ) & { waitUntil : ( delay ? : number , skipUntil ? : boolean ) => Promise < T > ; } ; function generateAccessor < T > ( elementGetter , locator , waitDelay = 0 ) { const accessor = < any > ( async ( ) => await elementGetter ( ) ) ; accessor . waitUntil = async ( delay = 0 , skipUntil = false ) => { if ( ! skipUntil ) { await waitUntilElement ( locator ) ( ) ; } await browser . sleep ( Math . max ( waitDelay , delay ) ) ; return await elementGetter ( ) ; } ; return accessor ; } export function getElement ( locator , waitDelay ? , ofElement ? ) { const getter = async ( ) => await ( ofElement ? ofElement . element ( locator ) : element ( locator ) ) ; return generateAccessor < ElementFinder > ( getter , locator , waitDelay ) ; } export function getElementAll ( locator , waitDelay ? , ofElement ? ) { const getter = async ( ) => < any > await ( ofElement ? ofElement . all ( locator ) : element . all ( locator ) ) ; return generateAccessor < ElementArrayFinder > ( getter , locator , waitDelay ) ; } export function waitUntilElement ( locator , timeout = 5000 , delay = 0 ) { return async ( ) : Promise < ElementFinder > => { const e = await waitUntil ( element ( locator ) , timeout ) ; await browser . sleep ( delay ) ; return e ; } ; } export function getElementByDataQa ( dataQa , waitDelay = 0 ) { return getElement ( by . css ( `[data-qa="${ dataQa }"]` ) , waitDelay ) ; } export function getElementAllByDataQa ( dataQa , waitDelay = 0 ) { return getElementAll ( by . css ( `[data-qa="${ dataQa }"]` ) , waitDelay ) ; } export function waitUntilElementByDataQa ( dataQa , timeout = 5000 , delay = 0 ) { return waitUntilElement ( by . css ( `[data-qa="${ dataQa }"]` ) , timeout , delay ) ; }