Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress: How do I get the text of the selected option in a select drop-down list?

Tags:

cypress

I want to be able to get the text of the selected option and not the value. I would use the value but Angular is changing it and putting the value in a ng-reflect-ng-value attribute.

<option _ngcontent-c1="" value="5: 1" ng-reflect-ng-value="1">Miscellaenous</option>

This will work, but I want to check that it equals "Miscellaenous"

cy.get('#id-9999').find('.categoryList').should('have.value','Miscellaenous');
like image 740
Maccurt Avatar asked Jul 07 '18 11:07

Maccurt


People also ask

How do you get the selected text value of a dropdown?

Or to get the text of the option, use text() : $var = jQuery("#dropdownid option:selected").

How do you handle the dropdown in Cypress?

cypress will not allow a click of an option, because it think options are invisible because the parent is invisible. The work around is to use . then() to get access to the unwrapped list item, and use jquery click to select it instead of cypress click.

How do I know which option is selected in dropdown?

You can use the jQuery :selected selector in combination with the val() method to find the selected option value in a select box or dropdown list.


1 Answers

This worked for me:

cy.get('#id-9999').find('.categoryList').find(':selected').contains('Miscellaenous')
like image 200
Maccurt Avatar answered Oct 08 '22 06:10

Maccurt