diff --git a/lib/next-actions.js b/lib/next-actions.js index 1b832be..1e0291a 100644 --- a/lib/next-actions.js +++ b/lib/next-actions.js @@ -2,6 +2,17 @@ const logger = require('./logger'); const _ = require('./helper'); const nextActions = {}; +const locatorReturnLocatorCommonFuncs = [ + 'locator', + 'getByRole', + 'getByAltText', + 'getByLabel', + 'getByPlaceholder', + 'getByTestId', + 'getByText', + 'getByTitle', +]; + nextActions.fileChooser = async function(filePath) { const fileChooser = await this.page.waitForEvent('filechooser'); await fileChooser.setFiles(filePath); @@ -42,23 +53,14 @@ nextActions.locator = async function({ func, args }) { // 返回locator对象时,缓存 if ( result - && [ - 'and', - 'or', - 'getByRole', - 'filter', - 'first', - 'getByAltText', - 'getByLabel', - 'getByPlaceholder', - 'getByRole', - 'getByTestId', - 'getByText', - 'getByTitle', - 'last', - 'locator', - 'nth', - ].includes(func) + && locatorReturnLocatorCommonFuncs.concat([ + 'and', + 'or', + 'filter', + 'first', + 'last', + 'nth', + ]).includes(func) ) { this.locator = result; } @@ -75,18 +77,10 @@ nextActions.page = async function({ func, args }) { // 返回locator相关的方法时需要给 this.locator 赋值 if ( result - && [ - 'locator', - 'frameLocator', - 'getByAltText', - 'getByLabel', - 'getByPlaceholder', - 'getByRole', - 'getByTestId', - 'getByText', - 'getByTitle', - 'waitForSelector', - ].includes(func)) { + && locatorReturnLocatorCommonFuncs.concat([ + 'frameLocator', + 'waitForSelector', + ]).includes(func)) { this.locator = result; } return result || ''; @@ -124,19 +118,11 @@ nextActions.pageIframe = async function({ index, func, args }) { const result = await this.pageIframe[func].apply(this.pageIframe, args); if ( result - && [ - 'locator', + && locatorReturnLocatorCommonFuncs.concat([ 'frameLocator', 'frameElement', - 'getByAltText', - 'getByLabel', - 'getByPlaceholder', - 'getByRole', - 'getByTestId', - 'getByText', - 'getByTitle', 'waitForSelector', - ].includes(func)) { + ]).includes(func)) { this.locator = result; } return result || '';