假设我从Behat文档中获取了以下场景:
@javascript
Scenario: Searching for a page with autocompletion
Given I am on "/wiki/Main_Page"
When I fill in "search" with "Behavior Driv"
And I wait for the suggestion box to appear
Then I should see "Behavior Driven Development"我的FeatureContext类扩展了MinkContext,并具有以下方法:
/**
* @Given /^I wait for the suggestion box to appear$/
*/
public function iWaitForTheSuggestionBoxToAppear()
{
$this->spin(function (FeatureContext $context) {
return $context->assertElementOnPage('.suggestions-result');
}, 15);
return true;
}
public function spin ($lambda, $wait = 15)
{
for ($i = 0; $i < $wait; $i++)
{
try {
if ($lambda($this)) {
return true;
}
} catch (Exception $e) {
// do nothing
}
sleep(1);
}
$backtrace = debug_backtrace();
throw new Exception(
"Timeout thrown by " . $backtrace[1]['class'] . "::" . $backtrace[1]['function'] . "()\n" .
$backtrace[0]['file'] . ", line " . $backtrace[0]['line']
);
}我的Behat配置设置为:
default:
extensions:
Behat\MinkExtension:
base_url: http://en.wikipedia.org
goutte: ~
selenium2:
browser: phantomjs为什么我发现传递给spin的函数从来不返回true?如果传入页面第一次加载(如body )时存在的选择器,它可以正常工作,但我希望这样可以发现自动完成结果(或者更确切地说是包含结果的标记)是可用的。
我哪里出问题了吗?
来自自带服务器的日志显示了试图查找元素的选择器:
20:44:40.924 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:41.161 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:42.218 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:42.445 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:43.454 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:43.696 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:44.724 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:44.962 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:45.969 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:46.249 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:47.273 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:47.519 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:48.529 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:48.816 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:49.829 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:50.075 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:51.082 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:51.306 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:52.314 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:52.540 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:53.546 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:53.768 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:54.773 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:54.994 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:56.011 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:56.226 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:57.234 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:57.479 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:58.485 INFO - Executing: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]])
20:44:58.720 INFO - Done: [find elements: By.xpath: //html/descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' suggestions-result ')]]
20:44:59.837 INFO - Executing: [delete session: d95ac5c9-6969-4ecb-b8f2-aafc6f8b82a5])
[INFO - 2016-04-08T20:44:59.866Z] ShutdownReqHand - _handle - About to shutdown
20:45:00.425 INFO - Done: [delete session: d95ac5c9-6969-4ecb-b8f2-aafc6f8b82a5]发布于 2016-04-13 10:31:54
我们最终放弃了使用自旋函数,因为我们遇到了同样的问题。
我们发现有一件事是有效的,那就是含蓄的等待。
您可以将其添加到上下文中,并按如下方式使用:
public function turnOnImplicitWait()
{
// Set up implicit timeouts
$driver = $this->getSession()->getDriver()->getWebDriverSession();
$driver->timeouts()->implicit_wait(array("ms" => 10000));
}
public function turnOffImplicitWait()
{
// Set up implicit timeouts
$driver = $this->getSession()->getDriver()->getWebDriverSession();
$driver->timeouts()->implicit_wait(array("ms" => 0));
}
/**
* @Given /^I set (?:|the )implicit wait to ([\d]+)? (m|mins?|s|seconds?|ms|milliseconds)?(?:| .*)$/
*/
public
function setImplicitWait($time, $msormins)
{
$driver = $this->getSession()->getDriver()->getWebDriverSession();
if ($msormins == "min" | $msormins == "mins") {
$time = $time * 60000;
} elseif ($msormins == "s" | $msormins == "seconds") {
$time = $time * 1000;
} elseif ($msormins == "ms" | $msormins == "milliseconds") {
$time = $time * 1;
}
$driver->timeouts()->implicit_wait(array("ms" => $time));
}您还可以将其添加到您的特性上下文中,并为每个场景设置它:
/**
* @BeforeScenario
*/
public function implicitlyWait($event)
{
// Set up implicit timeouts
$driver = $this->getSession()->getDriver()->getWebDriverSession();
$driver->timeouts()->implicit_wait(array("ms" => 10000));
}注意:关闭隐式等待,等待那些期望看不到元素的步骤,您应该会很好。在它没有成功地看到元素之后,打开它。不执行此操作将使其挂起隐式等待的长度。
https://stackoverflow.com/questions/36508201
复制相似问题