Can't select an option in a drop-down with dynamic values, with codeceptjs and javascript

1.8k views Asked by At

This is my firts question and I don't speak English very well, but hope to can explain my problem. I spent some days looking for an answer in a similar question, but I didn't find it.

I'm automatiziting test with javascript. I use codeceptjs.

I have to make a test with a select. This select has ID, no problem, but the options has value, witch it's dynamically generated. I want to select the second option, but can't select by text, value or similar.

The code I need to test is this:

<select id="time_slots_list" name="selectedTimeSlot" class="form-control">
       <option value="" selected>Choose delivery</option>
       <option value="1601967600000" >Tuesday 06 - Betwwen 09 and 12h</option>
       <option value="1601967600000" >Tuesday 06 - Betwwen 12 and 14h</option>
       <option value="1601967600000" >Tuesday 06 - Betwwen 14 and 16h</option>
       <option value="1601967600000" >Tuesday 06 - Betwwen 16 and 18h</option>
       <option value="1601967600000" >Tuesday 06 - Betwwen 18 and 21h</option>
</select>

Each value changes every week.

My code is this:

const opcionEnvio = await I.grabTextFrom('select[@id="time_slots_list"]/option:nth-child(2)');
I.selectOption('select[@id="time_slots_list"]',opcionEnvio);

I tried several variants, but I can't achieve it.

Thanks

1

There are 1 answers

0
Chilina On

I fix it!

I can't use selectOption. I needed to write:

    I.click('#time_slots_list');
    let delivery = locate ('//select[@id="time_slots_list"]/option').at(2);
    I.click (delivery);

Hope can help someone.