Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  22] [ 3]  / answers: 1 / hits: 9274  / 5 Years ago, mon, may 20, 2019, 12:00:00

I'm new to and Cypress and I need to extract a dynamical part of a text from my application.



<body>
<div>Hello World greeting number 9123</div>
</body>


In this example I need to extract 9123 from the div, to use later in my tests. Any idea how I should do that?


More From » testing

 Answers
4

First you could try to get the full text from the div using 'invoke method and the use RegExp and match in javascript to get the number from the text:



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

[#7578] Friday, May 17, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlynnb

Total Points: 402
Total Questions: 96
Total Answers: 109

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
;