1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Eliminate redundant locator building (#19585)

Our e2e test driver was building the locator object multiple times over
in some cases. The `Locator` object is required by certain webdriver
methods, but our driver methods all accept a "raw locator".

This change avoids a few redundant calls, and makes it easier to
improve the error message for a timeout failure (which will be done in
a later PR). It also makes the code match the documentation/parameter
names.
This commit is contained in:
Mark Stacey 2023-07-10 17:09:37 -02:30 committed by GitHub
parent e7144411c7
commit e43552bd55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -161,16 +161,18 @@ class Driver {
// replacement for the implementation below. It takes an option options // replacement for the implementation below. It takes an option options
// bucket that can include the state attribute to wait for elements that // bucket that can include the state attribute to wait for elements that
// match the selector to be removed from the DOM. // match the selector to be removed from the DOM.
const selector = this.buildLocator(rawLocator);
let element; let element;
if (!['visible', 'detached'].includes(state)) { if (!['visible', 'detached'].includes(state)) {
throw new Error(`Provided state selector ${state} is not supported`); throw new Error(`Provided state selector ${state} is not supported`);
} }
if (state === 'visible') { if (state === 'visible') {
element = await this.driver.wait(until.elementLocated(selector), timeout); element = await this.driver.wait(
until.elementLocated(this.buildLocator(rawLocator)),
timeout,
);
} else if (state === 'detached') { } else if (state === 'detached') {
element = await this.driver.wait( element = await this.driver.wait(
until.stalenessOf(await this.findElement(selector)), until.stalenessOf(await this.findElement(rawLocator)),
timeout, timeout,
); );
} }
@ -205,15 +207,13 @@ class Driver {
} }
async findVisibleElement(rawLocator) { async findVisibleElement(rawLocator) {
const locator = this.buildLocator(rawLocator); const element = await this.findElement(rawLocator);
const element = await this.findElement(locator);
await this.driver.wait(until.elementIsVisible(element), this.timeout); await this.driver.wait(until.elementIsVisible(element), this.timeout);
return wrapElementWithAPI(element, this); return wrapElementWithAPI(element, this);
} }
async findClickableElement(rawLocator) { async findClickableElement(rawLocator) {
const locator = this.buildLocator(rawLocator); const element = await this.findElement(rawLocator);
const element = await this.findElement(locator);
await Promise.all([ await Promise.all([
this.driver.wait(until.elementIsVisible(element), this.timeout), this.driver.wait(until.elementIsVisible(element), this.timeout),
this.driver.wait(until.elementIsEnabled(element), this.timeout), this.driver.wait(until.elementIsEnabled(element), this.timeout),
@ -231,8 +231,7 @@ class Driver {
} }
async findClickableElements(rawLocator) { async findClickableElements(rawLocator) {
const locator = this.buildLocator(rawLocator); const elements = await this.findElements(rawLocator);
const elements = await this.findElements(locator);
await Promise.all( await Promise.all(
elements.reduce((acc, element) => { elements.reduce((acc, element) => {
acc.push( acc.push(
@ -246,14 +245,12 @@ class Driver {
} }
async clickElement(rawLocator) { async clickElement(rawLocator) {
const locator = this.buildLocator(rawLocator); const element = await this.findClickableElement(rawLocator);
const element = await this.findClickableElement(locator);
await element.click(); await element.click();
} }
async clickPoint(rawLocator, x, y) { async clickPoint(rawLocator, x, y) {
const locator = this.buildLocator(rawLocator); const element = await this.findElement(rawLocator);
const element = await this.findElement(locator);
await this.driver await this.driver
.actions() .actions()
.move({ origin: element, x, y }) .move({ origin: element, x, y })
@ -281,10 +278,9 @@ class Driver {
} }
async assertElementNotPresent(rawLocator) { async assertElementNotPresent(rawLocator) {
const locator = this.buildLocator(rawLocator);
let dataTab; let dataTab;
try { try {
dataTab = await this.findElement(locator); dataTab = await this.findElement(rawLocator);
} catch (err) { } catch (err) {
assert( assert(
err instanceof webdriverError.NoSuchElementError || err instanceof webdriverError.NoSuchElementError ||