当匹配字符串有正斜杠时,应该在 cypress 中包含失败

Should contain failing in cypress when the match string has a forward slash

我正在使用 cy.get('selector').should('contain','searchString') 来验证错误消息的内容,但断言一直失败。我正在搜索的字符串是这样的:Name cannot accept special characters like < > | / : * ? " #; % $ @ ! + ^

我把它分成两部分,发现故障发生在 /。所以我尝试用双斜杠和四斜杠转义它,但它没有用。

有人可以帮我确定实现这个的正确方法吗?任何帮助都感激不尽。干杯!

示例命令: cy.get('[data-test=abc]>p') .should('contain','Name cannot accept special characters like < > | / : * ? " #; % $ @ ! + ^')

.should('contain') 应该可以正常工作。您确定您使用的是正确的定位器吗?我只是 运行 它在本地用于字符串 Name cannot accept special characters like < > | / : * ? " #; % $ @ ! + ^

我的代码:

 cy.get('selector').should('contain', 
        'Name cannot accept special characters like < > | / : * ? " #; % $ @ ! + ^')

测试运行程序屏幕截图:

如果您的选择器是正确的,但您仍然遇到错误。您应该检查邮件中是否有反斜杠 \。要转义反斜杠,您必须多写 3 次反斜杠。所以\ = \\。所以在你的字符串中,如果我们用反斜杠替换正斜杠,它将是这样的:

cy.get('body > :nth-child(3)').should('contain', 
        'Name cannot accept special characters like < > | \\ : * ? " #; % $ @ ! + ^')