When writing functional tests, how can I set the value of a select box if I only have the label of the option I want, but not the actual value?
I'm not sure I got exactly what you need, I suppose you have a form like this
<form action="..." method="post">
...
<select id="my_form_category" name="my_form[category]">
<option value="1">category1</option>
<option value="2">category2</option>
<option value="3">category3</option>
</select>
...
<button type="submit">Edit</button>
</form>
and you want to select category2
. Even if you don't know the option value you can use the crawler to extract it
$client = static::createClient();
// go to form
$crawler = $client->request('GET', '...');
$value = $crawler->filter('#my_form_category option:contains("category2")')->attr('value');
$form = $crawler->selectButton('Edit')->form();
$form['my_form[category]']->select($value);
// ... set other values
$client->submit($form);
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