gpt4 book ai didi

regex - 在 TestCafe 的 .withText API 中使用字符串和正则表达式时遇到问题

转载 作者:行者123 更新时间:2023-11-28 20:25:37 27 4
gpt4 key购买 nike

我正在尝试在 .withText() 中同时使用文本和正则表达式来验证我的一项测试。这是我希望完成的:

Selector('div').withText(`Are you sure you want to delete this field? It is currently used for 1 people. This action cannot be undone.`);

在上面的代码中,我有一个变量1 people,它可以是2 people,等等。我需要一种方法让它接受整数(比如12 等)。我尝试了下面的方法和其他一些方法,但问题是 TestCafe 将该正则表达式视为文本:

Selector('div').withText(`Are you sure you want to delete this field\? It is currently used for [0-9] people. This action cannot be undone.`);

错误:

Cannot obtain information about the node because the specified selector does not match any node in the DOM tree.

> | Selector('div')
| .withText(`Are you sure you want to delete this field\? It is currently used for [0-9] people. This action cannot be undone.`)

如何编写一个既能容纳字符串又能容纳正则表达式的选择器?

最佳答案

请将您的模式用斜杠括起来以创建 RegExp然后放入TestCafe withText方法。以下代码应该适用于您的情况:

Selector('div')
.withText(/Are you sure you want to delete this field\? It is currently used for [1-9] people\. This action cannot be undone\./);

关于regex - 在 TestCafe 的 .withText API 中使用字符串和正则表达式时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59093802/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com