※こちらは旧サイトです(新サイトはこちら

Mink+BehatでもJenkinsのBUILD_TAGみたいなのが欲しい

2017-06-01 09:33:32

jenkinsにあるBUILD_TAGみたいな環境変数をMink+Behatのテストでも使いたい

FeatureContextで使う

とりあえずFeatureContext内でなんとかしてみる

class FeatureContext extends RawMinkContext implements Context, SnippetAcceptingContext
{
    private $BUILD_TAG;

    /**
     * Initializes context.
     *
     * Every scenario gets its own context instance.
     * You can also pass arbitrary arguments to the
     * context constructor through behat.yml.
     */
    public function __construct()
    {
        if(getenv('BUILD_TAG'))
        {
            $this->BUILD_TAG = getenv('BUILD_TAG');
        }else{
            $this->BUILD_TAG = date('Ymdhis',strtotime('now'));
        }
    }

    /**
     * @When :index 番目の :element エレメントに :text と入力する
     */
    public function inputElementText($index, $element, $text)
    {
        if(false !== strpos($text, "{BUILD_TAG}"))
        {
            $text = str_replace("{BUILD_TAG}", $this->BUILD_TAG, $text);
        }

        $nodes = $this->getSession()->getPage()->findAll('css', $element);
        $index = $index - 1;


        if (isset($nodes[$index]))
        {
            $nodes[$index]->setValue($text);
        } else {
            throw new PendingException($element." エレメントが見つかりませんでした");
        }
    }

    /**
     * @When :index 番目の :element エレメントに :text テキストが含まれていること
     */
    public function checkElementTextImplode($index, $element, $text)
    {
        if(false !== strpos($text, "{BUILD_TAG}"))
        {
            $text = str_replace("{BUILD_TAG}", $this->BUILD_TAG, $text);
        }

        $nodes = $this->getSession()->getPage()->findAll('css', $element);
        $index = $index - 1;

        if (isset($nodes[$index]))
        {
            if(false !== strpos($nodes[$index]->getHtml(),$text))
            {
            }else {
                throw new PendingException("テキストが見つかりませんでした - ".$text." - ".$nodes[$index]->getHtml());
            }
        } else {
            throw new PendingException($element." エレメントが見つかりませんでした");
        }
    }

    (中略)
}
もし 1 番目の "*[name='message']" エレメントに "{BUILD_TAG}" と入力する
    かつ 私が 1 番目の "#submitBtn" 要素をクリックする
    ならば 1 番目の "#messageList" エレメントに "{BUILD_TAG}" テキストが含まれていること

解説

  1. プライベートなメンバ変数にBUILD_TAGを持たせる
class FeatureContext extends RawMinkContext implements Context, SnippetAcceptingContext
{
    private $BUILD_TAG;     // ※追加
  1. コンストラクタで、メンバ変数に現在時刻(Ymdhis)をセットする。
    public function __construct()
    {
        if(getenv('BUILD_TAG'))
        {
            $this->BUILD_TAG = getenv('BUILD_TAG');
        }else{
            $this->BUILD_TAG = date('Ymdhis',strtotime('now'));
        }
    }

もし、behat実行時に環境変数BUILD_TAGが指定されていた場合はそっちを使うようにすることで、JenkinsのBUILD_TAGをそのまま渡したりすることができます

$ BUILD_TAG=testdaoooooo behat --profile=chrome-via-webdriver features/login.feature
  1. 普段テキストを渡すところに、{BUILD_TAG} という文字列が来たら、メンバ変数の値に差し替える
    /**
     * @When :index 番目の :element エレメントに :text と入力する
     */
    public function inputElementText($index, $element, $text)
    {
        if(false !== strpos($text, "{BUILD_TAG}"))      // ※"{BUILD_TAG}"という文字列があれば
        {
            $text = str_replace("{BUILD_TAG}", $this->BUILD_TAG, $text);        // :textを置換
        }

        $nodes = $this->getSession()->getPage()->findAll('css', $element);
        $index = $index - 1;