在 Cypress 中提取部分文本

Ami*_*mio 2 javascript testing cypress

我是 Cypress 的新手,我需要从我的应用程序中提取文本的动态部分。

<body>
<div>Hello World greeting number 9123</div>
</body>
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我需要从 div 中提取 9123,以便稍后在我的测试中使用。知道我应该怎么做吗?

soc*_*way 5

首先,您可以尝试full textdivusing 'invoke" 方法中获取 ,RegExpmatch在 javascript 中使用and获取文本中的数字:

cy.get('div').invoke('text')
.then((text)=>{ 
    var fullText = text;
    var pattern = /[0-9]+/g;
    var number = fullText.match(pattern);
    console.log(number);
   })
Run Code Online (Sandbox Code Playgroud)