2

I know that this is somewhat, very basic question, but.. I have to create a cypress element, for which I have to concatenate a value between variables.

Like I have this value

const value = "100 - 299"

and I have to print a value within a string, i.e.

cy.get('[data-value="100 - 299"]')

I am trying everything but unable to do so. Can someone help me on this?

1
  • Try backticks. Within them you can [data-value="${value}"] Commented Nov 5, 2021 at 17:30

2 Answers 2

4

I would consider template literals - also called backticks

cy.get(`[data-value="${value}"]`)
Sign up to request clarification or add additional context in comments.

2 Comments

I just want to add that template literals work in most browsers, but there are older versions of browsers that it will not work in. And if you run a development company like me, you'll know that clients do not care that their customers use Internet Explorer from the early 2000s, they WILL blame the devs! caniuse.com/template-literals
@Nerdi.org If you need that, you can use Babel
1

You could consider concatenation.

const value = "100 - 299"

cy.get('[data-value="' + value + '"]')

Or template literals ( check compatability: https://caniuse.com/template-literals )

cy.get(`[data-value="${value}"]`)

Or pre-concatenation:

const value = "100 - 299"
const fullstring = '[data-value="' + value + '"]'
cy.get(fullstring)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.