test: make clickXpath wait until it can 'see' the element

This commit is contained in:
Christopher Willis-Ford 2023-09-20 10:29:48 -07:00
parent cdde117692
commit 1275c58921
2 changed files with 40 additions and 8 deletions

View file

@ -118,9 +118,7 @@ describe('www-integration project-page signed in', () => {
// expect(projectUrl).toBe(defined);
driver = await buildDriver('www-integration project-page signed in');
await driver.get(rootUrl);
await driver.sleep(1000);
await signIn(username, password);
await findByXpath('//span[contains(@class, "profile-name")]');
});
afterAll(async () => await driver.quit());
@ -184,11 +182,9 @@ describe('www-integration project-page signed in', () => {
describe('www-integration project-creation signed in', () => {
beforeAll(async () => {
// expect(projectUrl).toBe(defined);
driver = await buildDriver('www-integration project-creation signed in');
await driver.get(rootUrl);
await signIn(username, password);
await findByXpath('//span[contains(@class, "profile-name")]');
// SauceLabs doesn't have access to the sb3 used in 'load project from file' test
// https://support.saucelabs.com/hc/en-us/articles/115003685593-Uploading-Files-to-a-Sauce-Labs-Virtual-Machine-during-a-Test
@ -203,8 +199,6 @@ describe('www-integration project-creation signed in', () => {
test('make a copy of a project', async () => {
await driver.get(ownedUnsharedUrl + '/editor');
let gf = await findByXpath('//img[@class="green-flag_green-flag_1kiAo"]');
await gf.isDisplayed();
await clickXpath(FILE_MENU_XPATH);
await clickText('Save as a copy');
let successAlert = await findText('Project saved as a copy.');

View file

@ -119,8 +119,46 @@ class SeleniumHelper {
return this.driver.wait(until.stalenessOf(element));
}
clickXpath (xpath) {
return this.findByXpath(xpath).then(el => el.click());
async waitUntilClickable (xpath) {
return await this.driver.wait(async () => {
let elementAtPath = await this.findByXpath(xpath);
if (!elementAtPath) {
return;
}
const rect = await elementAtPath.getRect();
const x = rect.x + (rect.width / 2);
const y = rect.y + (rect.height / 2);
const elementAtPoint = await this.driver.executeScript(
'return document.elementFromPoint(arguments[0], arguments[1])',
x,
y
);
if (!elementAtPoint) {
return;
}
// If we ask to click on a button and Selenium finds an image on the button, or vice versa, that's OK.
// It doesn't have to be an exact match.
const match = await this.driver.executeScript(
'return arguments[0].contains(arguments[1]) || arguments[1].contains(arguments[0])',
elementAtPath,
elementAtPoint
);
if (!match) {
return;
}
if (!await elementAtPath.isDisplayed()) {
return;
}
if (!await elementAtPath.isEnabled()) {
return;
}
return elementAtPath;
});
}
async clickXpath (xpath) {
const element = await this.waitUntilClickable(xpath);
element.click();
}
clickText (text) {