Skip to content

Commit

Permalink
fix: move to waitFor (#3933)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Oct 21, 2023
1 parent b652bd5 commit f598059
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -2365,10 +2365,11 @@ class Playwright extends Helper {
locator = new Locator(locator, 'css');

const context = await this._getContext();
const waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'attached' });
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still not present on page after ${waitTimeout / 1000} sec\n${err.message}`);
});
try {
await context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'attached' });
} catch (e) {
throw new Error(`element (${locator.toString()}) still not present on page after ${waitTimeout / 1000} sec\n${e.message}`);
}
}

/**
Expand All @@ -2380,10 +2381,10 @@ class Playwright extends Helper {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
locator = new Locator(locator, 'css');
const context = await this._getContext();
let waiter;
let count = 0;

// we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
let waiter;
if (this.frame) {
do {
waiter = await this.frame.locator(buildLocatorString(locator)).first().isVisible();
Expand All @@ -2393,13 +2394,13 @@ class Playwright extends Helper {
} while (count <= waitTimeout);

if (!waiter) throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec.`);
return;
}

waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'visible' });
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec\n${err.message}`);
});
try {
await context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'visible' });
} catch (e) {
throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec\n${e.message}`);
}
}

/**
Expand All @@ -2425,10 +2426,11 @@ class Playwright extends Helper {
return;
}

waiter = context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'hidden' });
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still visible after ${waitTimeout / 1000} sec\n${err.message}`);
});
try {
await context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'hidden' });
} catch (e) {
throw new Error(`element (${locator.toString()}) still visible after ${waitTimeout / 1000} sec\n${e.message}`);
}
}

/**
Expand All @@ -2438,7 +2440,6 @@ class Playwright extends Helper {
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
locator = new Locator(locator, 'css');
const context = await this._getContext();

let waiter;
let count = 0;

Expand All @@ -2455,7 +2456,7 @@ class Playwright extends Helper {
return;
}

return context.waitForSelector(buildLocatorString(locator), { timeout: waitTimeout, state: 'hidden' }).catch((err) => {
return context.locator(buildLocatorString(locator)).first().waitFor({ timeout: waitTimeout, state: 'hidden' }).catch((err) => {
throw new Error(`element (${locator.toString()}) still not hidden after ${waitTimeout / 1000} sec\n${err.message}`);
});
}
Expand Down Expand Up @@ -2522,7 +2523,12 @@ class Playwright extends Helper {
if (context) {
const locator = new Locator(context, 'css');
if (!locator.isXPath()) {
waiter = contextObject.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> text=${text}`, { timeout: waitTimeout, state: 'visible' });
try {
await contextObject.locator(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> text=${text}`).first().waitFor({ timeout: waitTimeout, state: 'visible' });
} catch (e) {
console.log(e);
throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec\n${e.message}`);
}
}

if (locator.isXPath()) {
Expand All @@ -2535,23 +2541,17 @@ class Playwright extends Helper {
}
} else {
// we have this as https://github.com/microsoft/playwright/issues/26829 is not yet implemented
if (this.frame) {
let count = 0;
do {
waiter = await this.frame.locator(`:has-text('${text}')`).first().isVisible();
await this.wait(1);
count += 1000;
} while (count <= waitTimeout);

if (!waiter) throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec`);
return;
}
// eslint-disable-next-line no-lonely-if
const _contextObject = this.frame ? this.frame : contextObject;
let count = 0;
do {
waiter = await _contextObject.locator(`:has-text('${text}')`).first().isVisible();
await this.wait(1);
count += 1000;
} while (count <= waitTimeout);

waiter = contextObject.waitForFunction(text => document.body && document.body.innerText.indexOf(text) > -1, text, { timeout: waitTimeout });
if (!waiter) throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec`);
}
return waiter.catch((err) => {
throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec\n${err.message}`);
});
}

/**
Expand Down Expand Up @@ -2710,17 +2710,21 @@ class Playwright extends Helper {
let waiter;
const context = await this._getContext();
if (!locator.isXPath()) {
waiter = context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`, { timeout: waitTimeout, state: 'detached' });
try {
await context.locator(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`).first().waitFor({ timeout: waitTimeout, state: 'detached' });
} catch (e) {
throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${e.message}`);
}
} else {
const visibleFn = function ([locator, $XPath]) {
eval($XPath); // eslint-disable-line no-eval
return $XPath(null, locator).length === 0;
};
waiter = context.waitForFunction(visibleFn, [locator.value, $XPath.toString()], { timeout: waitTimeout });
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${err.message}`);
});
}
return waiter.catch((err) => {
throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${err.message}`);
});
}

async _waitForAction() {
Expand Down

0 comments on commit f598059

Please sign in to comment.