From 349cebc29fdc8de6663bf6e572503cb6077a3284 Mon Sep 17 00:00:00 2001 From: SmartBear <471110230@qq.com> Date: Sun, 8 Oct 2023 22:53:54 -0500 Subject: [PATCH] =?UTF-8?q?feat:=20next=20=E6=94=AF=E6=8C=81page=20popup?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=96=B9=E6=B3=95=E8=B0=83=E7=94=A8=20(#33)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: next方法增加 page popup 对象方法调用 --------- Co-authored-by: xudafeng --- lib/controllers.js | 10 ++++------ lib/macaca-playwright.ts | 13 ++++++++++++- lib/next-actions.js | 14 ++++++++++++++ package.json | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/controllers.js b/lib/controllers.js index 98f28ee..850a122 100644 --- a/lib/controllers.js +++ b/lib/controllers.js @@ -555,7 +555,7 @@ controllers.addCookie = async function(cookie) { * Delete either a single cookie by parameter name, or all the cookies associated with the active document’s address if name is undefined. * * @module deleteCookie - * @return {Promise.} + * @return {Promise.} */ controllers.deleteCookie = async function(cookie) { await this.browserContext.clearCookies(cookie); @@ -566,7 +566,7 @@ controllers.deleteCookie = async function(cookie) { * Delete All Cookies command allows deletion of all cookies associated with the active document’s address. * * @module deleteAllCookies - * @return {Promise.} + * @return {Promise.} */ controllers.deleteAllCookies = async function() { await this.browserContext.clearCookies(); @@ -584,11 +584,9 @@ controllers.getContext = async function() { controllers.setContext = async function(name) { const index = this.browserContexts.findIndex(item => item.name === name); if (index !== -1) { - this.currentContextIndex = index; - this.browserContext = this.browserContexts[index]; - this.page = this.pages[index]; + this._setContext(index); } else { - this.currentContextIndex = await this._createContext(name); + await this._createContext(name); } return true; }; diff --git a/lib/macaca-playwright.ts b/lib/macaca-playwright.ts index dc9a649..df432ea 100644 --- a/lib/macaca-playwright.ts +++ b/lib/macaca-playwright.ts @@ -25,12 +25,12 @@ class Playwright extends DriverBase { newContextOptions = {}; frame = null; page = null; + pagePopup = null; locator = null; // 当前选中的 element atoms = []; pages = []; elements = {}; browserContexts = []; - currentContextIndex = 0; static DEFAULT_CONTEXT = DEFAULT_CONTEXT; @@ -89,9 +89,20 @@ class Playwright extends DriverBase { this.browserContext = this.browserContexts[index]; this.pages.push(await this.browserContext.newPage()); this.page = this.pages[index]; + // Get all popups when they open + this.page.on('popup', async (popup) => { + this.pagePopup = null; + await popup.waitForLoadState(); + this.pagePopup = popup; + }); return index; } + _setContext(index: number) { + this.browserContext = this.browserContexts[index]; + this.page = this.pages[index]; + } + async stopDevice() { await this.browserContext.close(); await this.browser.close(); diff --git a/lib/next-actions.js b/lib/next-actions.js index da7fa2d..ef67f23 100644 --- a/lib/next-actions.js +++ b/lib/next-actions.js @@ -17,6 +17,20 @@ nextActions.mouse = async function({ type, args }) { return true; }; +// 对当前页面进行方法调用 +nextActions.page = async function({ func, args }) { + if (this.page) { + return this.page[func].apply(this.page, args); + } +}; + +// 对弹出页面进行方法调用 +nextActions.popup = async function({ func, args }) { + if (this.popup) { + return this.popup[func].apply(this.popup, args); + } +}; + nextActions.elementStatus = async function(elementId) { const element = this.elements[elementId]; if (!element) { diff --git a/package.json b/package.json index 8be341b..d2dfd97 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "macaca-playwright", - "version": "1.11.1", + "version": "1.11.2", "description": "Macaca Playwright driver", "keywords": [ "playwright",