简体   繁体   English

剧作家逻辑期待

[英]Playwright Logical expect

I have two headings on the page and I need to make sure that either the one or the other exists in the page.我在页面上有两个标题,我需要确保页面中存在一个或另一个。

let heading = page.getByRole('heading', {name: 'MyFirstHeading'});
let heading2 = page.getByRole('heading', {name: 'MySecondHeading'});
await expect(heading).toBeVisible();

What I tried and worked:我尝试和工作的是:

try {
    await expect(page.getByRole('heading', {name: 'MyFirstHeading'})).toBeVisible();
} catch (error) {
    await expect(page.getByRole('heading', {name: 'MySecondHeading'})).toBeVisible();
}

While clarification on what is wanted would be could, I believe the user is asking for other ways to do this...perhaps.虽然可以澄清需要什么,但我相信用户正在要求其他方式来做到这一点......也许。 I offer this - playwright retrying :我提供这个 -剧作家重试

const heading = page.getByRole('heading', { name: 'MyFirstHeading' });
const heading2 = page.getByRole('heading', { name: 'MySecondHeading' });

await expect(async () => {
  const headingIsVisible = await heading.isVisible();
  const heading2IsVisible = await heading2.isVisible();

  expect(headingIsVisible || heading2IsVisible).toBeTruthy();      }
}).toPass({
 timeout: 30000,
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM