/** * {{> waitNumberOfVisibleElements }} */ async waitNumberOfVisibleElements(locator, num, sec) { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout; return this.t .expect(createSelector(locator).with({ boundTestRun: this.t }).filterVisible().count) .eql(num, `The number of elements (${locator}) is not ${num} after ${sec} sec`, { timeout: waitTimeout }) .catch(mapError); }
// TODO Add url assertions /** * {{> setCookie }} */ async setCookie(cookie) { if (Array.isArray(cookie)) { throw new Error('cookie array is not supported'); } cookie.path = cookie.path || '/'; // cookie.expires = cookie.expires || (new Date()).toUTCString(); const setCookie = ClientFunction(() => { document.cookie = `${cookie.name}=${cookie.value};path=${cookie.path};expires=${cookie.expires};`; }, { dependencies: { cookie } }).with({ boundTestRun: this.t }); return setCookie(); }
/** * {{> scrollPageToBottom }} */ scrollPageToBottom() { return ClientFunction(() => { const body = document.body; const html = document.documentElement; window.scrollTo(0, Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight, )); }).with({ boundTestRun: this.t })().catch(mapError); }
/** * {{> waitForFunction }} */ async waitForFunction(fn, argsOrSec = null, sec = null) { let args = []; if (argsOrSec) { if (Array.isArray(argsOrSec)) { args = argsOrSec; } else if (typeof argsOrSec === 'number') { sec = argsOrSec; } } const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout; const clientFn = createClientFunction(fn, args).with({ boundTestRun: this.t }); return waitForFunction(clientFn, waitTimeout); }
/** * {{> waitInUrl }} */ async waitInUrl(urlPart, sec = null) { const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout; const clientFn = createClientFunction((urlPart) => { const currUrl = decodeURIComponent(decodeURIComponent(decodeURIComponent(window.location.href))); return currUrl.indexOf(urlPart) > -1; }, [urlPart]).with({ boundTestRun: this.t }); return waitForFunction(clientFn, waitTimeout).catch(async (err) => { const currUrl = await this.grabCurrentUrl(); throw new Error(`expected url to include ${urlPart}, but found ${currUrl}`); }); }