unit-testing - 如何用柏树检查体内是否存在元素?
问题描述
我想检查 nextjs-portal 标记是否出现在我的页面正文中,如果是,则单击它。
我怎么能用柏树做到这一点?
这有效,但我现在需要 if 块:
cy.get('nextjs-portal').click();
就像是
if (cy.get('nextjs-portal')) {
cy.get('nextjs-portal').click();
}
解决方案
如果在这里,你不需要任何东西。如果元素不存在,cy.get('nextjs-portal')
将超时并且不会发生点击。
如果你真的需要条件测试,可以这样做,但它通常是一种反模式:
cy
.get('body')
.then($body => {
if ($body.find('.nextjs-portal').length) {
// now you know the element was found in the DOM
} else {
// your element was not found in the DOM
}
});
推荐阅读
- reactjs - 如何测试包含子组件并使用上下文的样式化组件?
- android - 从 Android 中的外部扫描仪获取条码值
- go - Go FAQ对方法集的解释
- javascript - JS/Ajax/Jquery通过链表连续请求
- karate - 空手道 jsonObject[*].label 和 $jsonObject[*].label 抛出错误
- javascript - 如何构造一个 .find() 函数,该函数接受一个值数组并返回一个键对象:值
- javascript - 如何用函数包装导出工厂的组件?
- react-native - React Native - 如何向 Pressable 组件添加不透明度反馈?
- python - 带有pickle通道的python3子进程
- java - 使用axistools-maven-plugin时未检测到全局绑定