TestCafe - 如何在不通过测试的情况下检查 Web 元素是否存在?

TestCafe - How to check if a web element exists or does not exist without failing the test?(TestCafe - 如何在不通过测试的情况下检查 Web 元素是否存在?)
本文介绍了TestCafe - 如何在不通过测试的情况下检查 Web 元素是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本需要根据 CSS 选择器找到的特定浏览器对象是否存在来调整其工作流行为.

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.

我不想使用 document.getElementByID 方法,因为这在技术上不是 CSS 选择器,而且我们的整个企业都在 CSS 选择器上进行了标准化,所以除了 CSS 选择器之外的任何遍历 DOM 的东西都不会通过我们的无论如何都要进行代码审查.

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.

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

thingSelector 在哪里:

Where thingSelector is:

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

OpenThing 在哪里:

Where OpenThing is:

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.

在所有情况下,我仍然需要继续工作流程.

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())

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

如果上述其中一项在一种情况下未失败,则在另一种情况下将失败,而在第一个未失败的情况下,另一项将失败.我需要一些能给我逻辑的东西,但永远不会使脚本执行失败,并且仍然允许我根据对象是否存在返回 True 或 False.

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.

提前感谢您帮助我解决了这个问题,也感谢您学习和提高我的 Javascript 技能!

Thank you in advance for helping me to solve this problem, and also to learn and grow in my Javascript skills!

推荐答案

您可以通过以下方式检查 if 条件中的 async exists 属性:

You can check the async exists property in the if condition in the following way:

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

这篇关于TestCafe - 如何在不通过测试的情况下检查 Web 元素是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)