Skip to content

Commit

Permalink
feat: next方法增加 page popup 对象方法调用
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuineng committed Oct 8, 2023
1 parent 54c4c3c commit 40f153b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 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
14 changes: 13 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;
popup = null;
locator = null; // 当前选中的 element
atoms = [];
pages = [];
elements = {};
browserContexts = [];
currentContextIndex = 0;

static DEFAULT_CONTEXT = DEFAULT_CONTEXT;

Expand Down Expand Up @@ -89,9 +89,21 @@ 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.popup = null;
await popup.waitForLoadState();
this.popup = 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

0 comments on commit 40f153b

Please sign in to comment.