Skip to content

Instantly share code, notes, and snippets.

@nobiki
Created June 15, 2017 08:43
Show Gist options
  • Save nobiki/e25af43b4bdc7fcb221b671a687e3888 to your computer and use it in GitHub Desktop.
Save nobiki/e25af43b4bdc7fcb221b671a687e3888 to your computer and use it in GitHub Desktop.
<?php
use Behat\Behat\Context\Context;
use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Mink\WebAssert;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use WebDriver\Session;
use Behat\Behat\Tester\Exception\PendingException;
/**
* Defines application features from the specific context.
*/
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'));
}
}
/**
* @Then :time 秒待つ
*
* @param string $time
*/
public function wait($time)
{
sleep($time);
}
/**
* @When ブラウザを :xx x :yy にリサイズ
*
* @param int $xx
* @param int $yy
*/
public function browserResize($xx, $yy)
{
$this->getSession()->resizeWindow($xx, $yy);
}
/**
* @Then URLが :url と等しいこと
*/
public function checkAssertUrlEqual($url)
{
if($url === $this->getSession()->getCurrentUrl())
{
}else{
throw new PendingException("URL ".$url." と ".$this->getSession()->getCurrentUrl()." は一致しません");
}
}
/**
* @When 現在時刻が :start から :end の間であること
*/
public function checkNowTime($start, $end)
{
$now = date("H:i:s", time());
if($start < $now && $now < $end)
{
}else{
throw new PendingException("現在時刻は ".$now." です ");
}
}
/**
* @Then アラートのOKをクリックする
*
* @param int $wait
*/
public function clickAlertOK()
{
$this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
}
/**
* @Then :wait 秒後にアラートのOKをクリックする
*
* @param int $wait
*/
public function clickAlertOKWithWait($wait)
{
sleep($wait);
$this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
}
/**
* @Then スクリプト :script を実行する
*
* @param string $element
* @param string $text
*/
public function execScriptJs($script)
{
$this->getSession()->evaluateScript($script);
}
/**
* @Then :element 要素に .val(:text) を実行する
*
* @param string $element
* @param string $text
*/
public function execScriptVal($element, $text)
{
$elements = $this->getSession()->getPage()->findAll("css", $element);
foreach($elements as $i => $row)
{
$this->getSession()->evaluateScript('$("'.$element.'").val("'.$text.'")');
}
}
/**
* @Then 日付形式の :element 要素に、 :ymd を入力する
*
* @param string $element
* @param string $ymd
*/
public function setDateColumnYmd($element, $ymd)
{
/* memo: Windows環境にstrptimeは無い */
// if(false !== strptime($ymd, "%Y-%m-%d"))
// {
$elements = $this->getSession()->getPage()->findAll("css", $element);
foreach($elements as $i => $row)
{
$this->getSession()->evaluateScript('$("'.$element.'").val("'.$ymd.'")');
}
// }else{
// throw new PendingException("\"".$ymd."\" - 日付形式は、YYYY-MM-DD である必要があります");
// }
}
/**
* @When :account / :password でログインしている
*
* @param string $account
* @param string $password
*/
public function doLogin($account, $password)
{
$this->getSession()->getPage()->fillField("mail_address", $account);
$this->getSession()->getPage()->fillField("password", $password);
$submits = $this->getSession()->getPage()->findAll("css", "#doLogin");
$loginButton = $submits[0];
$loginButton->click();
}
/**
* @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);
if("last" === $index)
{
$index = count($nodes) - 1;
}else{
$index = $index - 1;
}
if (isset($nodes[$index]))
{
$nodes[$index]->setValue($text);
} else {
throw new PendingException($element." エレメントが見つかりませんでした");
}
}
/**
* @When ファイル :path を :index 番目の :element に添付する
*/
public function attachFileToField($path, $index, $element)
{
if ($this->getMinkParameter('files_path')) {
$fullPath = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;
if (is_file($fullPath)) {
$path = $fullPath;
}
}
$nodes = $this->getSession()->getPage()->findAll('css', $element);
// if("last" === $index)
// {
// $index = count($nodes) - 1;
// }else{
$index = $index - 1;
// }
if (isset($nodes[$index]))
{
$nodes[$index]->attachFile($path);
} else {
throw new PendingException($element." エレメントが見つかりませんでした");
}
}
/**
* @When :index 番目の :element エレメントをクリックする
*/
public function clickElement($index, $element)
{
$nodes = $this->getSession()->getPage()->findAll('css', $element);
if("last" === $index)
{
$index = count($nodes) - 1;
}else{
$index = $index - 1;
}
if (isset($nodes[$index])) {
$nodes[$index]->click();
} 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);
if("last" === $index)
{
$index = count($nodes) - 1;
}else{
$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." エレメントが見つかりませんでした");
}
}
/**
* @When :index 番目の :element エレメントに :text テキストが含まれていないこと
*/
public function checkElementTextNotImplode($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);
if("last" === $index)
{
$index = count($nodes) - 1;
}else{
$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." エレメントが見つかりませんでした");
}
}
/**
* @When :index 番目の :element エレメントに :text と表示されていること
*/
public function checkElementText($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);
if("last" === $index)
{
$index = count($nodes) - 1;
}else{
$index = $index - 1;
}
if (isset($nodes[$index]))
{
if($text === $nodes[$index]->getHtml())
{
}else {
throw new PendingException("テキストが一致しません - ".$text." - ".$nodes[$index]->getHtml());
}
} else {
throw new PendingException($element." エレメントが見つかりませんでした");
}
}
/**
* @Then :element 要素の :attribute 属性に :text がセットされていること
*
* @param string $element
* @param string $attribute
* @param string $text
*/
public function attributeAssartText($element, $attribute, $text)
{
$elements = $this->getSession()->getPage()->findAll("css", $element);
if(count($elements) > 0)
{
foreach($elements as $i => $row)
{
if($text === $row->getAttribute($attribute))
{
}else{
throw new PendingException(" \"".$text."\" と \"".$row->getAttribute($attribute)."\" は一致しません");
}
}
}else{
throw new PendingException($element. "要素が見つかりません");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment