Skip
Skip

Reputation: 480

Use a wraped variable in the type() function. Cypress

I'm trying to save a string with:

        cy.get('#accessInfoText').invoke('text').then($wholeText => {
            let pattern = /(?<=code: ).{10}/i;
            let number = $wholeText.match(pattern);
            cy.wrap(number).as('accessCode');
            cy.log(number);                           // AsTE5sOlJU 
        });

Later on, in the test, I need it to type it into an input field:

     cy.get('@accessCode').then(code => {
            cy.log(code);                           // [AsTE5sOlJU] 
            cy.followLabel('Access code').type(code)
        });

And my problem is that cypress converts it to an object. Which is not accepted by the .type function. enter image description here

Upvotes: 1

Views: 415

Answers (1)

Eddy Gilmour
Eddy Gilmour

Reputation: 3120

The line let number = $wholeText.match(pattern); returns an array of all matches, even if you only have one.

Try

let number = $wholeText.match(pattern)[0];  // take first match only

If there's a possibility that there's no matches, you would need to expand it to avoid a runtime error.

const matches = $wholeText.match(pattern)
const number = matches.length ? matches[0] : null

Upvotes: 1

Related Questions