Skip to content

Commit

Permalink
feat: click异常处理 (#29)
Browse files Browse the repository at this point in the history
* feat: click异常处理
  • Loading branch information
yihuineng authored Sep 18, 2023
1 parent 50326b1 commit 97d597e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
20 changes: 16 additions & 4 deletions lib/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ const findElementOrElements = async function(strategy, selector, ctx, many) {
this.locator = this.page.locator(selector);
}
/**
* `css selector` and `xpath` is default
*/
* `css selector` and `xpath` is default
*/
if (strategy === 'name') {
selector = `text=${selector}`;
} else if (strategy === 'id') {
Expand Down Expand Up @@ -181,17 +181,29 @@ controllers.setFrame = async function(frame) {
controllers.click = async function(elementId, options = {}) {
const element = this.elements[elementId];
if (!element) {
logger.error('click Element is not found');
logger.error('click element is not found');
return null;
}
if (!element.isVisible()) {
logger.error('click Element is not visible');
logger.error('click element is not visible');
return null;
}
await element.click({
timeout: 2E3,
delay: 200,
...options,
}).catch(async e => {
// 处理一些需要自动重试的异常
if (e.message.includes('Element is not attached')) {
await _.sleep(2E3);
await element.click({
timeout: 2E3,
delay: 200,
...options,
});
} else {
throw e;
}
});
return null;
};
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.10.0",
"version": "1.10.1",
"description": "Macaca Playwright driver",
"keywords": [
"playwright",
Expand Down
2 changes: 1 addition & 1 deletion test/macaca-playwright.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('test/macaca-playwright.test.js', function() {
});

it('getAllCookies', async () => {
await driver.get('https://www.baidu.com');
await driver.get('https://www.google.com.hk');
res = await driver.getAllCookies();
assert(Array.isArray(res));
});
Expand Down

0 comments on commit 97d597e

Please sign in to comment.