Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress Get Attribute value and store in Variable

I want to get the Attribute value and store in a variable how we can achieve this in cypress

In my case I want to get the complete class value and store it in variable.

This code just give me the attribute class value but how I can store the fetch value in variable

cy.get('div[class*="ui-growl-item-container ui-state-highlight ui-corner-all ui-shadow ui-growl-message"]').invoke('attr', 'class')
like image 925
Mehran Shafqat Avatar asked Aug 21 '19 06:08

Mehran Shafqat


People also ask

How do you get attribute values in Cypress?

To find elements by data attribute, query using the attribute selector. cy. get() yields a jQuery object, you can get its attribute by invoking the . attr() method.

How do I get an element's text contents in Cypress?

How do I get an element's text contents? Cypress commands yield jQuery objects, so you can call methods on them. If the text contains a non-breaking space entity   then use the Unicode character \u00a0 instead of   . Tip: watch the Confirming the text with non breaking space entity video.

How do you fetch text in Cypress?

Cypress can validate the text on an element with the help of jQuery text() method. This method shall help us to fetch the text content on the selected element. We can also put assertions on the text content of the element. cy.


1 Answers

I was trying to compare the style of one element with another to make sure they were equal. Here's the code that seems to work for me.

cy.get('.searchable-group-selector-card-image')
  .eq(4)
  .invoke('attr', 'style')
  .then(($style1) => {
    const style1 = $style1
  })
like image 142
Joseph Mutidjo Avatar answered Sep 18 '22 14:09

Joseph Mutidjo