※こちらは旧サイトです(新サイトはこちら)
jenkinsにあるBUILD_TAG
みたいな環境変数をMink+Behatのテストでも使いたい
とりあえず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}" テキストが含まれていること
class FeatureContext extends RawMinkContext implements Context, SnippetAcceptingContext
{
private $BUILD_TAG; // ※追加
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
{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;