How to verify the selected option from the dropdown using Behat/Mink

Issue

There are couple of filters on the page and I would like to verify the default filter values.
I’m not able to get the selectors working.
Can someone help.

Here is the code snippet:

<div class="filter-widget">
<form name="" method="post" action="">
<div class="pure-g-r">
    <div class="pure-u-1-4">
    <div><label for="day" class="required">Day</label><select id="day" name="day">    <option value="all">all</option><option value="Sunday">Sunday</option><option value="Monday">Monday</option><option value="Tuesday">Tuesday</option><option value="Wednesday">Wednesday</option><option value="Thursday" selected="selected">Thursday</option><option value="Friday">Friday</option><option value="Saturday">Saturday</option></select></div>
</div>
<div class="pure-u-1-4">
    <div><label for="month" class="required">Month</label><select id="month" name="month"><option value="January">January</option><option value="February">February</option><option value="March">March</option><option value="April" selected="selected">April</option><option value="May">May</option><option value="June">June</option><option value="July">July</option><option value="August">August</option><option value="September">September</option><option value="October">October</option><option value="November">November</option><option value="December">December</option></select></div>
</div>
</div>

As you can see above, the filters default to the current day/month and I need to be able to verify those values. But the css selector used below is failing. Is it supported in Behat?

$page = $this->getSession()->getPage();
$defaultFilterDay = $page->find('css', '#day select option:selected')->getText();
$defaultFilterMonth = $page->find('css', '#month select option:selected')->getText();
$currentDay = new \DateTime('now')->format('l'); //This returns current Day
$currentMonth = new \DateTime('now')->format('F'); //This returns current Month

assertEquals(currentDay, $defaultFilterDay);
assertEquals(currentMonth, $defaultFilterMonth);

Solution

The following selector worked for me:

$defaultFilterDay = $page->find("css", "#day option[selected='selected']")->getText();
    $defaultFilterMonth = $page->find("css", "#month option[selected='selected']")->getText();

    date_default_timezone_set('UTC');
    $currentTime = new \DateTime('now');
    $currentDay = $currentTime->format('l');
    $currentMonth = $currentTime->format('F');

    assertEquals($currentDay, $defaultFilterDay);
    assertEquals($currentMonth, $defaultFilterMonth);    

Answered By – vijay pujar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published