vij*_*jar 3 php bdd css-selectors behat mink
页面上有几个过滤器,我想验证默认过滤器值.我无法让选择器工作.有人可以帮忙吗
这是代码片段:
<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>
Run Code Online (Sandbox Code Playgroud)
如您所见,过滤器默认为当前日/月,我需要能够验证这些值.但是下面使用的css选择器失败了.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);
Run Code Online (Sandbox Code Playgroud)
Dal*_*las 10
我遇到了同样的问题,我想使用选项值和select css选择器确定所选选项的值.这就是我最终做的事情:
/**
* @Then /^the option "([^"]*)" from select "([^"]*)" is selected$/
*/
public function theOptionFromSelectIsSelected($optionValue, $select)
{
$selectField = $this->getSession()->getPage()->find('css',$select);
if (null === $selectField) {
throw new \Exception(sprintf('The select "%s" was not found in the page %s', $select, $this->getSession()->getCurrentUrl()));
}
$optionField = $selectField->find('xpath', "//option[@selected='selected']");
if (null === $optionField) {
throw new \Exception(sprintf('No option is selected in the %s select in the page %s', $select, $this->getSession()->getCurrentUrl()));
}
if ($optionField->getValue() != $optionValue) {
throw new \Exception(sprintf('The option "%s" was not selected in the page %s, %s was selected', $optionValue, $this->getSession()->getCurrentUrl(), $optionField->getValue()));
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您不想在功能上下文中添加自定义步骤,可以尝试直接在Gherkin中使用CSS选择器:
Then the "select[name='day'] option[selected='selected']" element contains "Thursday"
And the "select[name='month'] option[selected='selected']" element contains "April"
vij*_*jar -1
以下选择器对我有用:
$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);
Run Code Online (Sandbox Code Playgroud)