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",