Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: click异常处理 #29

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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