Created
November 2, 2012 23:18
-
-
Save bayleedev/4004960 to your computer and use it in GitHub Desktop.
Mink + Behat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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\Mink\Driver\GoutteDriver, | |
Behat\Mink\Driver\Selenium2Driver, | |
Behat\Mink\Session, | |
Behat\Mink\Mink; | |
use Behat\Mink\Selector\CssSelector; | |
// Browser drivers | |
require_once(__DIR__ . '/../../mink.phar'); | |
/** | |
* Features context. | |
*/ | |
class FeatureContext extends BehatContext | |
{ | |
private $output; | |
private $driver; | |
protected $mink; | |
protected $base; | |
public function __construct() { | |
// Define variables | |
$this->base = 'http://api.jquery.com/'; | |
$this->mink = new \Behat\Mink\Mink(); | |
// Set browsers | |
$this->mink->registerSession('selenium', new Session(new Selenium2Driver( | |
'firefox', | |
null, | |
'http://api.jquery.com/' | |
))); | |
$this->mink->registerSession('goutte', new Session(new GoutteDriver())); | |
// Set default | |
$this->mink->setDefaultSessionName('goutte'); | |
// Start it | |
$this->mink->getSession()->start(); | |
// open some page in browser: | |
$this->mink->getSession()->visit('http://api.jquery.com/'); | |
// get the current page URL: | |
$page = $this->mink->getSession()->getPage(); | |
// El | |
$el = $page->findById('jq-primarySearch'); | |
echo 'Tag Name: ' . $el->getTagName(); // undefined function getTagName() on a non-object | |
exit; | |
} | |
/** | |
* Since not all Session drivers support base urls this is a wrapper to help | |
* | |
* @param string $path The path of the file you want | |
* @param string $base The base of the url | |
* @return string | |
*/ | |
protected function uri($path, $base = null) { | |
// Get default value | |
$base = is_null($base) ? $this->base : $base; | |
// Add trailing slash to $base | |
if(substr($base, -1, 1) !== '/') { | |
$base .= '/'; | |
} | |
// Remoev it from $path | |
if(substr($path, -1, 1) === '/') { | |
$path = substr($path, 0, -1); | |
} | |
// Return | |
return $base . $path; | |
} | |
/** @Given /^I am in a directory "([^"]*)"$/ */ | |
public function iAmInADirectory($dir) | |
{ | |
if (!file_exists($dir)) { | |
mkdir($dir); | |
} | |
chdir($dir); | |
} | |
/** @Given /^I have a file named "([^"]*)"$/ */ | |
public function iHaveAFileNamed($file) | |
{ | |
touch($file); | |
} | |
/** @When /^I run "([^"]*)"$/ */ | |
public function iRun($command) | |
{ | |
exec($command, $output); | |
$this->output = trim(implode("\n", $output)); | |
} | |
/** @Then /^I should get:$/ */ | |
public function iShouldGet(PyStringNode $string) | |
{ | |
if ((string) $string !== $this->output) { | |
throw new Exception( | |
"Actual output is:\n" . $this->output | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment