-3

I need to retrieve a selector from HTML with a space on it.

    <mat-radio-button
        *ngFor="let element of data"
        [attr.data-test]="'radio-buttons-' + element.value">
    </mat-radio-button>

element.value got the value of "My pictures"

Cypress cannot find the selector "radio-buttons-my pictures" and do the click.

cy.getBySelector('radio-buttons-My pictures').click();
Hugo Seleiro
  • 2,467
  • 5
  • 26
  • 39

1 Answers1

0

As mentionned by Misha Mashina in the comments, you can replace your spaces by other element, like a dash.

On your template, you iterate over an array of objects, so once you get your data, you can just add an attribute to your objects like this :

data = data.map(elem => {
  return {...elem,'data-test-value':elem.value.replace(/\s/g,'-')}
})

By doing this, you will have your value element with the original value, and the formatted one for your dataset attribute.

Alexis
  • 1,685
  • 1
  • 12
  • 30
  • Hello @Alexis, thanks for the help. im getting this error , Property 'replace' does not exist on type 'unknown'. can you help me ? – Hugo Seleiro Apr 10 '23 at 19:25
  • @HugoSeleiro, if you are sure there is only "string" in your `value` attribute, you can cast it before using `replace`. Like this `(elem.value).replace(/\s/g,'-')` – Alexis Apr 11 '23 at 06:20