I have Mat-select dropdown as follows
<mat-form-field>
<mat-label>Gender</mat-label>
<mat-select id="gender" required formControlName="gender">
<mat-option id="Male" value="male"> Male </mat-option>
<mat-option value="female"> Female </mat-option>
</mat-select>
</mat-form-field>
I'm trying to use Cypress to select male or female from the field.
cy.get('mat-select').click().select('Male')
With the above code I get the following error:
CypressError: cy.select() can only be called on a <select>. Your subject is a: <mat-select>
I need some help in fixing this, thank you.
The code that worked for me.
cy.get('#gender').click().then(() => {
cy.get('#male').click()
})
To add elements to Select option, we need to use <mat-option> element and to bind value with <mat-option> , use value property of it. To set and get a value for <mat-select> , use value , ngModel , formControl and formControlName property.
Opens up the mat-option dialog for the mat-select & selects the field that contains "Apple Inc."
cy.get('mat-select[formControlName=companyName]').click().get('mat-option').contains('Apple Inc.').click();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With