testing - 获取赛普拉斯数组中不包含值的第一个元素
问题描述
我有一个卡片列表,我需要以编程方式选择列表中不包含数组中五个值之一的第一个。我试过这个:
cy.get('.parent-element').not().contains('string1', 'string2', 'etc')
和这个:
cy.get('.parent-element').not('string1', 'string2', 'etc')
嗯。对此有任何想法。的任何子元素.parent-element
都是公平游戏,除了这几个例外。
解决方案
const selector = ".parent-element li";
const array = ["string1", "string2", "string3"];
cy.get(selector)
.then($els => {
return $els.filter((i, el) => {
// Cypress.$ is jQuery
const textContent = Cypress.$(el).text();
const result = array.filter(str => textContent.includes(str));
// if noone string is been found... we have found the desired element
return !result.length;
});
})
.then($el => {
// $el is your searched element
// some assertions about it
expect($el.text()).not.includes("string1");
expect($el.text()).not.includes("string2");
expect($el.text()).not.includes("string3");
});
- 指定选择器以查找子项(我附加
li
到选择器) - 您使用的
.not().contains
是断言,而不是过滤器 - 该
cy.get
函数返回一些 jquery 元素 - 使用
Cypress.$
(即jQuery)手动过滤掉各种孩子
让我知道是否足够清楚
推荐阅读
- api - PUT 或 PATCH,哪个更快,为什么?
- sql - 根据第一个字段的值使用不同字段进行 SQL 排序
- python - 键错误:'客户'
- reactjs - 使用 msal.js 时 AcquireTokenSilent 失败
- python - 是否可以在后台运行 PyAutoGUI?
- python - 是否可以将预训练的 lda 模型加载到 gensim 中?
- glob - 全局匹配在 Storybook 配置中不起作用?
- go - golang json.Unmarshal 到 struct []byte
- binary - 如何使用 C 语言从原始图像文件中获取秘密位?
- html - 对齐页脚引导卡片组的顶部