简体   繁体   English

TestCafe - 如何在测试失败的情况下检查 web 元素是否存在或不存在?

[英]TestCafe - How to check if a web element exists or does not exist without failing the test?

I'm trying to write a script that needs to adapt it's workflow behavior depending on whether a particular browser object found by CSS selector exists or does not exist.我正在尝试编写一个脚本,该脚本需要根据 CSS 选择器找到的特定浏览器 object 是否存在来调整其工作流行为。

I do not want to use a document.getElementByID method as this is not technically a CSS selector, and our entire enterprise is standardized on CSS selector so anything that walks the DOM other then a CSS selector won't make it past our code review process anyway.我不想使用 document.getElementByID 方法,因为这在技术上不是 CSS 选择器,而且我们的整个企业都在 CSS 选择器上标准化,所以除了 CSS 选择器之外的任何遍历 DOM 的东西都不会通过我们的代码审查过程反正。

var thing = await things.thingSelector(thingName);
if (await t.expect(thing.exists).notOk()) {
   await t.click(things.OpenThing(thingName));
} else {
   return false;
}
return true;

Where thingSelector is: thingSelector 在哪里:

const thingSelector = name =>
  Selector('p.thing-header-title span')
    .withText(name)
    .parent('div.thing');

Where OpenThing is: OpenThing 在哪里:

const OpenThing = name =>
    thingSelector(name)
        .find('a.thing-footer-item')
        .withText('Open');

I need to be able to continue execution if the object is not there and I'm checking that it exists, or if the object is there and I'm checking that it does not exist, and also the cases where the object is not there and it does not exist and the object is not there and I'm checking that it does not exist.如果 object 不存在并且我正在检查它是否存在,或者如果 object 存在并且我正在检查它不存在,以及 object 不存在的情况,我需要能够继续执行它不存在并且 object 不存在,我正在检查它不存在。

In all cases I still need to proceed with the workflow.在所有情况下,我仍然需要继续工作流程。

I've tried both sides of the logic coin:我已经尝试了逻辑硬币的两面:

if (!await t.expect(thing.exists).ok())

And

if (await t.expect(thing.exists).notOk())

If one of the above doesn't fail in one scenario it will fail in the other, and the other one will fail in the scenario that the first one didn't fail.如果上述其中一个在一种情况下没有失败,那么在另一种情况下也会失败,而在第一个没有失败的情况下,另一个也会失败。 I need something that will give me the logic, but not ever fail the script execution and still allow me to return either True or False depending on if the object is present or not present.我需要一些能给我逻辑的东西,但永远不会使脚本执行失败,并且仍然允许我返回 True 或 False,具体取决于 object 是否存在。

Thank you in advance for helping me to solve this problem, and also to learn and grow in my Javascript skills!在此先感谢您帮助我解决了这个问题,也让我的 Javascript 技能得到了学习和成长!

You can check the async exists property in the if condition in the following way:您可以通过以下方式检查if条件中的 async exists属性:

if(await things.thingSelector(thingName).exists) {
    // do something 
} 

You can use the following assertions to test existence and non-existence elements:您可以使用以下断言来测试存在和不存在元素:

test('Test existence and non-existence elements', async t => {
    await t
        .expect(Selector('#existing-element').exists)
        .expect(Selector('#non-existing-element').exists).notOk()
});
  • How to check if an element exists:如何检查元素是否存在:

     test('Element with id "element" exists', async t => { await t.expect(Selector('#element').exists).ok(); });
  • How to check if an element does NOT exist:如何检查元素是否不存在:

     test('Element with id "element" shouldn\'t exist', async t => { await t.expect(Selector('#element').exists).notOk(); });

Check out the official documentation .查看官方文档

this is working for me, you can give it a try这对我有用,你可以试试

async veriryCreativeIconButtonNotExists(){
await t.expect(this.exportButton.exists).ok()
await t.expect(this.columnPickerIcon.exists).ok()
await t.expect(this.filterColumnIcon.exists).ok()
if (await t.expect(this.columnPickerIcon.exists).ok()){
  await t.expect(this.creavtiveIconButton.exists).ok()
  await t.click(this.creavtiveIconButton)
  await t.expect(this.creativeImage.exists).ok()
  await t.click(this.creativeImage)
} else {
  return false;
}
return true;

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

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