【问题标题】:Behat: use variable between the steps in a scenarioBehat:在场景中的步骤之间使用变量
【发布时间】:2015-12-22 17:59:45
【问题描述】:

如何在一个场景中的步骤之间使用变量? 我需要存储 $output 的值,然后在第二步中使用它。

假设我有以下结构:

class testContext extends DefaultContext
{
    /** @When /^I click "([^"]*)"$/ */
    public function iClick($element) {
       if ($element = 2){
           $output = 5        
       }
    }


    /** @When /^I press "([^"]*)"$/ */
    public function iPress($button) {
        if($button == $output){
        echo "ok";
        }
    }
}

【问题讨论】:

    标签: php variables behat scenarios


    【解决方案1】:

    上下文类可以是有状态的;场景的所有步骤都将使用相同的上下文实例。这意味着您可以使用常规类属性来阻止步骤之间的状态:

    class testContext extends DefaultContext
    {
        private $output = NULL;
    
        /** @When /^I click "([^"]*)"$/ */
        public function iClick($element)
        {
           if ($element = 2) {
               $this->output = 5;
           }
        }
    
    
        /** @When /^I press "([^"]*)"$/ */
        public function iPress($button)
        {
            if ($this->output === NULL) {
                throw new BadMethodCallException("output must be initialized first");
            }
    
            if ($button == $this->output) {
                echo "ok";
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多