首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Behat自旋法找不到自周期选择器

Behat自旋法找不到自周期选择器
EN

Stack Overflow用户
提问于 2016-04-08 19:51:42
回答 1查看 348关注 0票数 1

假设我从Behat文档中获取了以下场景:

代码语言:javascript
复制
@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,并具有以下方法:

代码语言:javascript
复制
/**
 * @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配置设置为:

代码语言:javascript
复制
default:
  extensions:
    Behat\MinkExtension:
      base_url: http://en.wikipedia.org
      goutte: ~
      selenium2:
        browser: phantomjs

为什么我发现传递给spin的函数从来不返回true?如果传入页面第一次加载(如body )时存在的选择器,它可以正常工作,但我希望这样可以发现自动完成结果(或者更确切地说是包含结果的标记)是可用的。

我哪里出问题了吗?

来自自带服务器的日志显示了试图查找元素的选择器:

代码语言:javascript
复制
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]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-13 10:31:54

我们最终放弃了使用自旋函数,因为我们遇到了同样的问题。

我们发现有一件事是有效的,那就是含蓄的等待。

您可以将其添加到上下文中,并按如下方式使用:

代码语言:javascript
复制
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));
}

您还可以将其添加到您的特性上下文中,并为每个场景设置它:

代码语言:javascript
复制
 /**
 * @BeforeScenario
 */
public function implicitlyWait($event)
{
    // Set up implicit timeouts
    $driver = $this->getSession()->getDriver()->getWebDriverSession();
    $driver->timeouts()->implicit_wait(array("ms" => 10000));
}

注意:关闭隐式等待,等待那些期望看不到元素的步骤,您应该会很好。在它没有成功地看到元素之后,打开它。不执行此操作将使其挂起隐式等待的长度。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36508201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档