Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const select = async (
elementHandle: ElementHandle,
value: string | null,
timeoutMs?: number
): Promise => {
logger.verbose("actions.select");
const findTimeoutMs = (isNil(timeoutMs) ? CONFIG.findTimeoutMs : timeoutMs)!;
// ensure option with desired value is loaded before selecting
await elementHandle.evaluate(
(element: HTMLSelectElement, value: string | null, timeoutMs: number) => {
const qawolf: QAWolfWeb = (window as any).qawolf;
return qawolf.select.waitForOption(element, value, timeoutMs);
},
value,
findTimeoutMs
);
await elementHandle.select(value || "");
};
this.global.waitUntil = (
booleanFn: () => boolean,
timeoutMs?: number,
sleepMs?: number
) =>
waitUntil(
booleanFn,
// default timeoutMs to CONFIG.findTimeoutMs
(isNil(timeoutMs) ? CONFIG.findTimeoutMs : timeoutMs)!,
sleepMs
);
private async beforeAction(
selectorOrStep: Selector | Step | null,
action: Action
) {
logger.verbose("Runner: beforeAction");
let element = selectorOrStep
? await this._browser.find(selectorOrStep, {
action,
timeoutMs: CONFIG.findTimeoutMs
})
: null;
if (CONFIG.sleepMs) {
logger.verbose(`Runner: beforeAction sleep ${CONFIG.sleepMs} ms`);
await sleep(CONFIG.sleepMs);
// reload the element in case it changed since the sleep
if (selectorOrStep) {
logger.verbose("Runner: beforeAction reload element after sleep");
try {
// try to find it immediately
element = await this._browser.find(selectorOrStep, {
action,
timeoutMs: 0,
private async beforeAction(
selectorOrStep: Selector | Step | null,
action: Action
) {
logger.verbose("Runner: beforeAction");
let element = selectorOrStep
? await this._browser.find(selectorOrStep, {
action,
timeoutMs: CONFIG.findTimeoutMs
})
: null;
if (CONFIG.sleepMs) {
logger.verbose(`Runner: beforeAction sleep ${CONFIG.sleepMs} ms`);
await sleep(CONFIG.sleepMs);
// reload the element in case it changed since the sleep
if (selectorOrStep) {
logger.verbose("Runner: beforeAction reload element after sleep");
try {
// try to find it immediately
element = await this._browser.find(selectorOrStep, {
action,
timeoutMs: 0,
waitForRequests: false
});
} catch (e) {
// if it cannot be found immediately wait longer
element = await this._browser.find(selectorOrStep, {
private async beforeAction(
selectorOrStep: Selector | Step | null,
action: Action
) {
logger.verbose("Runner: beforeAction");
let element = selectorOrStep
? await this._browser.find(selectorOrStep, {
action,
timeoutMs: CONFIG.findTimeoutMs
})
: null;
if (CONFIG.sleepMs) {
logger.verbose(`Runner: beforeAction sleep ${CONFIG.sleepMs} ms`);
await sleep(CONFIG.sleepMs);
// reload the element in case it changed since the sleep
if (selectorOrStep) {
logger.verbose("Runner: beforeAction reload element after sleep");
try {
// try to find it immediately
element = await this._browser.find(selectorOrStep, {
action,
timeoutMs: 0,
waitForRequests: false
});
} catch (e) {
// if it cannot be found immediately wait longer
browser.page = qawolf.page.bind(qawolf);
browser.scroll = qawolf.scroll.bind(qawolf);
browser.select = qawolf.select.bind(qawolf);
browser.type = qawolf.type.bind(qawolf);
browser.qawolf = qawolf;
await managePages(browser);
if (options.url) await qawolf.goto(options.url);
const videoPath = options.videoPath || CONFIG.videoPath;
if (videoPath) {
// start capture after goto
qawolf._screenCapture = await ScreenCapture.start({
offset: {
x: CONFIG.chromeOffsetX,
y: CONFIG.chromeOffsetY
},
savePath: `${CONFIG.videoPath}/${basename(require.main!.filename)}`,
size: {
height: device.viewport.height,
width: device.viewport.width
}
});
}
return browser;
};
// replace the url w/ env variable if it exists
workflow.url = getUrl(workflow);
const options: BrowserCreateOptions = { size: workflow.size };
if (CONFIG.domPath) options.domPath = `${CONFIG.domPath}/${workflow.name}`;
self._browser = await Browser.create(options);
self._envValues = getEnvValues(workflow);
self._workflow = workflow;
if (CONFIG.videoPath) {
const device = self._browser.device;
self._screenCapture = await ScreenCapture.start({
offset: {
x: CONFIG.chromeOffsetX,
y: CONFIG.chromeOffsetY
},
savePath: `${CONFIG.videoPath}/${workflow.name}`,
size: {
height: device.viewport.height,
width: device.viewport.width
}
});
}
try {
await self._browser.goto(workflow.url);
} catch (error) {
logger.error(`Runner: could not goto ${workflow.url}: ${error}`);
// finish the video
workflow.url = getUrl(workflow);
const options: BrowserCreateOptions = { size: workflow.size };
if (CONFIG.domPath) options.domPath = `${CONFIG.domPath}/${workflow.name}`;
self._browser = await Browser.create(options);
self._envValues = getEnvValues(workflow);
self._workflow = workflow;
if (CONFIG.videoPath) {
const device = self._browser.device;
self._screenCapture = await ScreenCapture.start({
offset: {
x: CONFIG.chromeOffsetX,
y: CONFIG.chromeOffsetY
},
savePath: `${CONFIG.videoPath}/${workflow.name}`,
size: {
height: device.viewport.height,
width: device.viewport.width
}
});
}
try {
await self._browser.goto(workflow.url);
} catch (error) {
logger.error(`Runner: could not goto ${workflow.url}: ${error}`);
// finish the video
await self.close();
browser.scroll = qawolf.scroll.bind(qawolf);
browser.select = qawolf.select.bind(qawolf);
browser.type = qawolf.type.bind(qawolf);
browser.qawolf = qawolf;
await managePages(browser);
if (options.url) await qawolf.goto(options.url);
const videoPath = options.videoPath || CONFIG.videoPath;
if (videoPath) {
// start capture after goto
qawolf._screenCapture = await ScreenCapture.start({
offset: {
x: CONFIG.chromeOffsetX,
y: CONFIG.chromeOffsetY
},
savePath: `${CONFIG.videoPath}/${basename(require.main!.filename)}`,
size: {
height: device.viewport.height,
width: device.viewport.width
}
});
}
return browser;
};
const promises = [super.teardown()];
// if there is a sleep ms, wait after the last step
await new Promise(resolve => setTimeout(resolve, CONFIG.sleepMs));
if (this._runner) {
promises.push(this._runner.close());
}
await Promise.all(promises);
// give logs time to output
await new Promise(resolve => setTimeout(resolve, 100));
if (CONFIG.debug) {
// stay open
await new Promise(resolve => setTimeout(resolve, 24 * 60 * 1000));
}
}