Skip to content

Commit

Permalink
feat: next 支持page popup对象方法调用 (#33)
Browse files Browse the repository at this point in the history
* feat: next方法增加 page popup 对象方法调用

---------

Co-authored-by: xudafeng <[email protected]>
  • Loading branch information
yihuineng and xudafeng authored Oct 9, 2023
1 parent 54c4c3c commit 349cebc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
10 changes: 4 additions & 6 deletions lib/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string>}
* @return {Promise.<boolean>}
*/
controllers.deleteCookie = async function(cookie) {
await this.browserContext.clearCookies(cookie);
Expand All @@ -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.<string>}
* @return {Promise.<boolean>}
*/
controllers.deleteAllCookies = async function() {
await this.browserContext.clearCookies();
Expand All @@ -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;
};
Expand Down
13 changes: 12 additions & 1 deletion lib/macaca-playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions lib/next-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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.1",
"version": "1.11.2",
"description": "Macaca Playwright driver",
"keywords": [
"playwright",
Expand Down

0 comments on commit 349cebc

Please sign in to comment.