1

I have this element

<label for="prod-field">Project <span class="aui-icon">Required</span> </label> 

I want to check if Label text is equal to Project

cy.get('[for="prod-field"]').should('have.text', 'Project') 

but the result is

-'ProjectRequired' +'Project' 

so this selector take also span...

How can i select them independently and check?

2 Answers 2

1

You can do something like:

cy.get('[for="prod-field"]').should(($el) => { expect( $el .contents() // Grab all contents .first() // The text node you're looking for .text() // Get the text .trim() // And trim the white space ).to.eq('Project'); }); 

As you can see in the above, we can do this, however the selector is over complicated. I'd recommend you to tweak the HTML a bit if you can to something like:

<label for="prod-field" ><span class="label-text">Project</span>> <span class="aui-icon">Required</span> </label> 

Then, you can simply do this:

cy.get('[for="prod-field"] .label-text').should('have.text', 'Project'); 
Sign up to request clarification or add additional context in comments.

Comments

1

Your locator selects label and all within it, just change the locator to the span like this:

 cy.get('[for="prod-field"] span.label-text').should('have.text', 'Project') 

1 Comment

but how to check only label?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.