Javascript Cypress-如何使用正则表达式实现负外观?

Javascript Cypress-如何使用正则表达式实现负外观?,javascript,node.js,regex,filter,cypress,Javascript,Node.js,Regex,Filter,Cypress,我试图在Cypress中测试一个过滤器,一旦过滤器被删除,搜索结果应该再次包含超出过滤值的值 我正在尝试这样做: cy.get('.outputTableArea').within(() => { cy.get("td").then(($td) => { expect($td).to.contain(/^(?!regex)/); }); }); 不幸的是,这里的否定前瞻似乎不适用于(?!…)。如何测试列表的长度,以确保在删除筛选器时返回所有结果?例如

我试图在Cypress中测试一个过滤器,一旦过滤器被删除,搜索结果应该再次包含超出过滤值的值

我正在尝试这样做:

cy.get('.outputTableArea').within(() => {
    cy.get("td").then(($td) => {
        expect($td).to.contain(/^(?!regex)/);
    });
});

不幸的是,这里的否定前瞻似乎不适用于
(?!…)

如何测试列表的长度,以确保在删除筛选器时返回所有结果?例如,其中“10”是完整列表中的项目数:

cy.get('.outputTableArea td').should('have.length', 10);
如果需要检查表内容中是否缺少特定值,可以尝试:

cy.get('.outputTableArea').contains(/^regex/).should(not.exist);
或者可以将其缩小为带有以下内容的标签:

cy.get('.outputTableArea td').contains(/^regex/).should(not.exist);

如何测试列表的长度,以确保在删除筛选器时返回所有结果?例如,其中“10”是完整列表中的项目数:

cy.get('.outputTableArea td').should('have.length', 10);
如果需要检查表内容中是否缺少特定值,可以尝试:

cy.get('.outputTableArea').contains(/^regex/).should(not.exist);
或者可以将其缩小为带有以下内容的标签:

cy.get('.outputTableArea td').contains(/^regex/).should(not.exist);
您在DOM元素上使用了chai的matcher(别名为
include
),但API需要一个字符串,而不是一个
RegExp
指针,而是另一个字符串(与DOM的
string.prototype.include
)相同)

您可以使用:

expect($td.text()).to.match(/^(?!regex)/);
或者更好的是,Cypress'或
.should('match')
(在下面使用chai):

description('test',()=>{
它('test',()=>{
cy.document().然后(doc=>{
doc.body.innerHTML=`
你好,世界!
你好,史蒂夫!
`;
});
//使用cy.contains
// -------------------------------------------------------------------------
cy.get('.test:first').contains(/hello(?!world)/i);//将失败
cy.get('.test:last').contains(/hello(?!world)/i);//将成功
//在生成的文本上使用柴匹配器
// -------------------------------------------------------------------------
cy.get('.test:first').invoke('text').should('match',/hello(?!world)/i);//将失败
cy.get('.test:last').invoke('text').should('match',/hello(?!world)/i);//将成功
//在回调中使用cy.contains
// -------------------------------------------------------------------------
cy.get('.test:first')。然后($el=>{
cy.wrap($el).contains(/hello(?!world)/i);//将失败
});
cy.get('.test:last')。然后($el=>{
cy.wrap($el).contains(/hello(?!world)/i);//将成功
});
});
});
您在DOM元素上使用的是chai的matcher(别名为
include
),但API需要一个字符串,而不是一个
RegExp
指针,而是另一个字符串(与DOM的
string.prototype.includes

您可以使用:

expect($td.text()).to.match(/^(?!regex)/);
或者更好的是,Cypress'或
.should('match')
(在下面使用chai):

description('test',()=>{
它('test',()=>{
cy.document().然后(doc=>{
doc.body.innerHTML=`
你好,世界!
你好,史蒂夫!
`;
});
//使用cy.contains
// -------------------------------------------------------------------------
cy.get('.test:first').contains(/hello(?!world)/i);//将失败
cy.get('.test:last').contains(/hello(?!world)/i);//将成功
//在生成的文本上使用柴匹配器
// -------------------------------------------------------------------------
cy.get('.test:first').invoke('text').should('match',/hello(?!world)/i);//将失败
cy.get('.test:last').invoke('text').should('match',/hello(?!world)/i);//将成功
//在回调中使用cy.contains
// -------------------------------------------------------------------------
cy.get('.test:first')。然后($el=>{
cy.wrap($el).contains(/hello(?!world)/i);//将失败
});
cy.get('.test:last')。然后($el=>{
cy.wrap($el).contains(/hello(?!world)/i);//将成功
});
});
});

您计划与
^(?!regex)
匹配什么?这只是一个示例。这是一个设备列表,我计划匹配除已过滤设备之外的所有设备(以查看是否正确删除了过滤器…),您能否提供指向
contain()
API的链接?它支持正则表达式吗?@OliverHowald你有没有找到这个问题的答案?你打算用什么来匹配
^(?!regex)
?这只是一个例子。这是一个设备列表,我计划匹配除已过滤设备之外的所有设备(以查看是否正确删除了过滤器…),您能否提供指向
contain()
API的链接?它支持正则表达式吗?@OliverHowald你有没有找到这个问题的答案?