LoginSignup
4
5

More than 5 years have passed since last update.

behatのFeatureContext.php

Posted at

いまこんな感じで書いてます。

FeatureContext.php
<?php

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

use Behat\MinkExtension\Context\MinkDictionary;

use Behat\Mink\Selector\CssSelector,
    Behat\Mink\Selector\NameSelector,
    \Behat\Mink\Selector\SelectorsHandler;
//
// Require 3rd-party libraries here:
//
//   require_once 'PHPUnit/Autoload.php';
//   require_once 'PHPUnit/Framework/Assert/Functions.php';
//

/**
 * Features context.
 */
class FeatureContext extends Behat\MinkExtension\Context\MinkContext
{


    //use MinkDictionary;
    /**
     * Initializes context.
     * Every scenario gets it's own context object.
     *
     * @param array $parameters context parameters (set them up through behat.yml)
     */
    public function __construct(array $parameters)
    {
        // Initialize your context here
    }

    /**
     * @Then /^I wait for the suggestion box to appear$/
     */
    public function iWaitForTheSuggestionBoxToAppear()
    {
        $this->getSession()->wait(5000, "$('.suggestions-results').children().length > 0");
    }

   /**
     * @Given /^I am on "([^"]*)"$/
     */
    // public function iAmOn($arg1)
    // {
    //     // throw new PendingException();
    // }

    /**
     * @When /^I fill in "([^"]*)" with "([^"]*)"$/
     */
    public function iFillInWith($arg1, $arg2)
    {
        // throw new PendingException();
        $this->getSession()->wait(15000);
    }

    /**
     * @Given /^I click "([^"]*)"$/
     */
    public function iClick($arg1)
    {
        // throw new PendingException();
        $this->getSession()->wait(20000);
    }

    /**
     * @Then /^I should see "([^"]*)"$/
     */
    // public function iShouldSee($arg1)
    // {
    //     // throw new PendingException();
    // }
    /**
     * @When /^I click_link "([^"]*)"$/
     */
    public function iClickLink($arg1)
    {
        // throw new PendingException();
    }

    /**
     * @Given /^"ログイン"リンクをクリックする$/
     */
    public function stepDefinition1()
    {
        // throw new PendingException();
    }

    /**
     * @Given /^"([^"]*)" に "([^"]*)" を選択する$/
     */
    public function fang($arg1, $arg2)
    {
        // throw new PendingException();
    }

    /**
     * @Given /^一秒待つ$/
     */
    public function ta()
    {
        // throw new PendingException();
        sleep(1);
    }

    /**
     * @Given /^(\d+)秒待つ$/
     */
    public function ta2($arg1)
    {
        // throw new PendingException();
        sleep($arg1);
    }

    /**
     * @Given /^Ajaxを待つ$/
     */
    public function ajax()
    {
        // throw new PendingException();
        sleep(2);
    }



    /**
     * @Given /^"(.+)"に "(.+)"を選択する$/
     */
    public function radioButtonSelect($arg1, $arg2)
    {
        $radio_button = $this->getSession()->getPage()->find('css', 'input[type=radio][name="'.$arg1.'"][value='.$arg2.']');
        if (null === $radio_button) {
            throw new ElementNotFoundException(
              $this->getSession(), 'form field', 'id|name|label|value', $radio_button
            );
        }
        // $value = $radio_button->getAttribute('value');
        $radio_button->click();
    }
    /**
     * @Given /^"([^"]*)" checkboxを クリックする$/
     */
    public function chekboxClick($arg1)
    {
        $el = $this->getSession()->getPage()->find('css', 'input[type=checkbox][name="'.$arg1.'"]');
        if (null === $el) {
            throw new ElementNotFoundException(
              $this->getSession(), 'form field', 'id|name|label|value', $el
            );
        }
        // $value = $radio_button->getAttribute('value');
        $el->click();
    }

    /**
     * 
     * @Given /^"([^"]*)" の"class:(.+)"リンクをクリックする$/
     */
    public function gotoLinkForClass($arg1,$arg2)
    {
        $elAry = $this->getSession()->getPage()->findAll('css', 'a.'.$arg2);
        if (null === $elAry) {
            throw new ElementNotFoundException(
              $this->getSession(), 'link', 'id|name|label|value', $elAry
            );
        }
        //print_r($el);
        $url = null;
        foreach($elAry as $k => $el){
            $url = $el->getAttribute('href');
            if(preg_match('/'.$arg1.'/',$url)){
                break;
            }   
        }
        if (null === $url) {
            throw new ElementNotFoundException(
              $this->getSession(), 'link', 'id|name|label|value', $url
            );
        }

        //header("Location: ".$url);
        $this->getSession()->visit($url);
        return;
    }

    /**
     * @Given /^"(.+)" から "(.+)"を選択する$/
     */
    public function pulldownSelect($arg1, $arg2)
    {
        // echo $arg1;
        $pulldown = $this->getSession()->getPage()->find('css', 'select[name="'.$arg1.'"]');
        if (null === $pulldown) {
            throw new ElementNotFoundException(
              $this->getSession(), 'form field', 'id|name|label|value', $pulldown
            );
        }
        // $value = $radio_button->getAttribute('value');
        $pulldown->selectOption($arg2);
    }

    /**
     * @Given /^"id:(.+)"のプルダウンで "(.+)"を選択する$/
     */
    public function pulldownSelectForId($arg1, $arg2)
    {
        $pulldown = $this->getSession()->getPage()->findById($arg1);
        if (null === $pulldown) {
            throw new ElementNotFoundException(
              $this->getSession(), 'form field', 'id|name|label|value', $pulldown
            );
        }
        // $value = $radio_button->getAttribute('value');
        $pulldown->selectOption($arg2);
    }

    /**
     * @Given /^"([^"]*)" をクリックする$/
     */
    public function clickLinkForId($arg1)
    {
        $el = $this->getSession()->getPage()->find('css', 'a#'.$arg1);
        if (null === $el) {
            throw new ElementNotFoundException(
              $this->getSession(), 'form field', 'id|name|label|value', $el
            );
        }
        // $value = $radio_button->getAttribute('value');
        $el->click();
    }


//
// Place your definition and hook methods here:
//
//    /**
//     * @Given /^I have done something with "([^"]*)"$/
//     */
//    public function iHaveDoneSomethingWith($argument)
//    {
//        doSomethingWith($argument);
//    }
//
}
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5