gpt4 book ai didi

php - 如何使用 Behat/Mink 从下拉列表中验证所选选项

转载 作者:可可西里 更新时间:2023-10-31 23:55:47 25 4
gpt4 key购买 nike

页面上有几个过滤器,我想验证默认过滤器值。我无法让选择器工作。有人可以帮忙吗。

这是代码片段:

<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>

正如您在上面看到的,过滤器默认为当前日期/月份,我需要能够验证这些值。但是下面使用的 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);

最佳答案

我有同样的问题,我想使用选项值和 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()));
}
}

关于php - 如何使用 Behat/Mink 从下拉列表中验证所选选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22839001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com