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

Trouble using string and regex in .withText API in TestCafe

我正在尝试在 .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 and then put it into the 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\./);