From e1633c31f22a8f839344a407bb1628b9e21f1c8d Mon Sep 17 00:00:00 2001 From: yihuineng <471110230@qq.com> Date: Fri, 24 Nov 2023 16:02:30 +0800 Subject: [PATCH] feat: next locator --- lib/macaca-playwright.ts | 4 +-- lib/next-actions.js | 65 ++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/lib/macaca-playwright.ts b/lib/macaca-playwright.ts index d4a426b..3a0cd0f 100644 --- a/lib/macaca-playwright.ts +++ b/lib/macaca-playwright.ts @@ -1,6 +1,6 @@ import path from 'path'; import { sync as mkdirp } from 'mkdirp'; -import playwright, { Frame } from 'playwright'; +import playwright, { ElementHandle, Frame, FrameLocator, Locator } from 'playwright'; import DriverBase from 'driver-base'; import _ from './helper'; @@ -52,7 +52,7 @@ class Playwright extends DriverBase { page = null; pagePopup = null; pageIframe: Frame = null; - locator = null; // 当前选中的 element + locator: ElementHandle | Locator | FrameLocator = null; // 当前选中的 element 或 locator atoms = []; pages = []; elements = {}; diff --git a/lib/next-actions.js b/lib/next-actions.js index 800b064..1b832be 100644 --- a/lib/next-actions.js +++ b/lib/next-actions.js @@ -36,11 +36,59 @@ nextActions.browser = async function({ func, args }) { logger.error('browser or func is not found'); }; +nextActions.locator = async function({ func, args }) { + if (this.locator && this.locator[func]) { + const result = await this.locator[func].apply(this.locator, args); + // 返回locator对象时,缓存 + if ( + result + && [ + 'and', + 'or', + 'getByRole', + 'filter', + 'first', + 'getByAltText', + 'getByLabel', + 'getByPlaceholder', + 'getByRole', + 'getByTestId', + 'getByText', + 'getByTitle', + 'last', + 'locator', + 'nth', + ].includes(func) + ) { + this.locator = result; + } + return result || ''; + } + logger.error('browser or func is not found'); +}; + // 对当前页面进行方法调用 nextActions.page = async function({ func, args }) { if (this.page && !this.page.isClosed() && this.page[func]) { await this.page.waitForLoadState(); const result = await this.page[func].apply(this.page, args); + // 返回locator相关的方法时需要给 this.locator 赋值 + if ( + result + && [ + 'locator', + 'frameLocator', + 'getByAltText', + 'getByLabel', + 'getByPlaceholder', + 'getByRole', + 'getByTestId', + 'getByText', + 'getByTitle', + 'waitForSelector', + ].includes(func)) { + this.locator = result; + } return result || ''; } logger.error('page or func is not found'); @@ -74,6 +122,23 @@ nextActions.pageIframe = async function({ index, func, args }) { if (this.pageIframe && this.pageIframe[func]) { await this.pageIframe.waitForLoadState(); const result = await this.pageIframe[func].apply(this.pageIframe, args); + if ( + result + && [ + 'locator', + 'frameLocator', + 'frameElement', + 'getByAltText', + 'getByLabel', + 'getByPlaceholder', + 'getByRole', + 'getByTestId', + 'getByText', + 'getByTitle', + 'waitForSelector', + ].includes(func)) { + this.locator = result; + } return result || ''; } logger.error('pageIframe or func is not found'); diff --git a/package.json b/package.json index 8fc21c6..87c3410 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "macaca-playwright", - "version": "1.11.17", + "version": "1.11.18", "description": "Macaca Playwright driver", "keywords": [ "playwright",