Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress test Material UI autocomplete [duplicate]

How can I get a value from the dropdown in cypress for the material ui autocomplete.

Currently it gets to the selection of the autocomplete but I cant seem to get to the dropdown or type anything. There will always be a test user called "ntest_user" so I thought maybe I can autocomplete it instead of clicking dropdown but no dice

  <Autocomplete
    id="combo-box"
    data-testid="tagAutocomplete"
    options={userz}
    getOptionLabel={(option) => option}
    value={userId}
    onChange={viewUserz}
    renderInput={(params) => <TextField {...params}
      label="User Id"
      variant="outlined" />}
  />
</FormControl>


cy.get('.tagAutocomplete li[data-option-index="0"]').click();
like image 611
OnePiece Avatar asked Mar 06 '26 06:03

OnePiece


1 Answers

You can do something like this:

cy.get("#combo-box").click();
cy.get("li[data-option-index="0"]").contains("ntest_user").then((option) => {
   option[0].click();
})

OP was able to solve by this:

cy.get("#combo-box").click();
cy.contains("ntest_user").then((option) => {
   option[0].click();
})
like image 183
Alapan Das Avatar answered Mar 09 '26 13:03

Alapan Das



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!