Skip to content

Commit

Permalink
feat: next locator
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuineng committed Nov 24, 2023
1 parent 7829f26 commit e1633c3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/macaca-playwright.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 = {};
Expand Down
65 changes: 65 additions & 0 deletions lib/next-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "macaca-playwright",
"version": "1.11.17",
"version": "1.11.18",
"description": "Macaca Playwright driver",
"keywords": [
"playwright",
Expand Down

0 comments on commit e1633c3

Please sign in to comment.