Skip to content

Instantly share code, notes, and snippets.

@htuscher
Created September 11, 2013 15:46
Show Gist options
  • Save htuscher/6525514 to your computer and use it in GitHub Desktop.
Save htuscher/6525514 to your computer and use it in GitHub Desktop.
FeatureContext using Mink for TYPO3 Backend automation
<?php
use Behat\Behat\Context\ClosuredContextInterface,
Behat\Behat\Context\TranslatedContextInterface,
Behat\Behat\Context\BehatContext,
Behat\Behat\Context\Step,
Behat\Behat\Exception\PendingException,
Behat\Behat\Event\ScenarioEvent;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
/**
* Features context.
*/
class FeatureContext extends MinkContext {
/**
* URL paths
*
* @var array
*/
public $paths = array();
/**
* Initializes context.
* Every scenario gets it's own context object.
*
* @param array $parameters context parameters from behat.yml
*/
public function __construct(array $parameters) {
if ($parameters['paths']) {
foreach ($parameters['paths'] as $key => $path) {
$this->paths[$key] = $path;
}
}
}
/**
*
* Initialize TYPO3 Backend-User Admin with random generated password
*
* @BeforeSuite
* @todo Vagrant local db must be taken into account
*/
public static function setup(\Behat\Behat\Event\SuiteEvent $event)
{
// require_once('./typo3/init.php');
$username = 'behat';
$pass = 'behat';
$count = array( // $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
'uid',
'be_users',
'username=behat' // $GLOBALS['TYPO3_DB']->fullQuoteStr($username, 'be_users')
);
if (!$count) {
$insertFields = array(
'username' => $username,
'password' => md5($pass),
'admin' => 1,
'uc' => '',
'fileoper_perms' => 0,
'tstamp' => mktime(),
'crdate' => mktime()
);
// $GLOBALS['TYPO3_DB']->exec_INSERTquery('be_users', $insertFields);
}
}
/**
*
* Remove TYPO3 Backend-User Admin
*
* @AfterSuite
* @todo Vagrant local db must be taken into account
*/
public static function teardown(\Behat\Behat\Event\SuiteEvent $event)
{
// $GLOBALS['TYPO3_DB']->exec_DELETEquery('be_users', array('username' => 'behat'));
}
/**
* Fakes the browser user agent.
*
* This is needed when a scenario for the TYPO3 Backend is executed using goutte driver.
* The TYPO3 Backend throws a RuntimeException with the default goutte user agent.
*
* @BeforeScenario @useragent
*/
public function fakeUserAgent() {
$this->getSession()->setRequestHeader('User-Agent', 'Mozilla');
}
/**
* @Given /^I am on the backend login page$/
*/
public function iAmOnTheBackendLoginPage() {
return array(
new Step\When('I am on "' . $this->paths['backend'] . '"')
);
}
/**
* @When /^I open the quickbooker$/
*/
public function iOpenTheQuickbooker() {
$this->getSession()->getPage()->find("css", "h2.booking-headline")->click();
}
/**
* @Given /^I click on the hotel selector$/
*/
public function iClickOnTheHotelSelector() {
$this->getSession()->getPage()->find("css", ".m-booker a.select2-choice")->click();
}
/**
* @BeforeScenario @backend
*/
public function loginBackend()
{
$this->getSession()->reset();
$this->getSession()->visit($this->locatePath($this->paths['backend']));
$this->fillField("t3-username","behat");
$this->fillField("t3-password","behat");
$this->pressButton("t3-login-submit");
}
/**
* @Given /^I am on the backend$/
*/
public function iAmOnTheBackend()
{
$this->getSession()->visit($this->locatePath($this->paths['backend']) . 'backend.php');
$this->getSession()->wait(2000);
}
/**
* @Given /^I wait for ajax to finish$/
*/
public function iWaitForAjaxToFinish()
{
$this->getSession()->wait(30000, 'Ext.Ajax.isLoading() == false;');
}
/**
* @Given /^I wait for "([^"]*)" seconds$/
*/
public function iWaitForSeconds($arg1)
{
$this->getSession()->wait($arg1 * 1000);
}
/**
* @Given /^I am on the backend-module "([^"]*)"$/
*/
public function iAmOnTheBackendModule($arg1)
{
$this->getSession()->getPage()->findById($arg1)->click();
$this->getSession()->wait(30000, 'Ext.Ajax.isLoading() == false;');
$this->getSession()->getDriver()->switchToIFrame('content');
}
/**
* @Given /^I click on the tab "([^"]*)"$/
*/
public function iClickOnTheTab($arg1)
{
$this->getSession()->getPage()->find("xpath", "a[@data-toggle='tab'][text()='" . $arg1 . "']")->click();
}
/**
* Take screenshot when step fails. Works only with Selenium2Driver.
* Screenshot is saved at [Date]/[Feature]/[Scenario]/[Step].jpg
*
* @TODO Add a check if WebDriver Session fully works.
*
* @AfterStep @javascript
*/
public function takeScreenshotAfterFailedStep(Behat\Behat\Event\StepEvent $event) {
if ($event->getResult() === Behat\Behat\Event\StepEvent::FAILED) {
$driver = $this->getSession()->getDriver();
if ($driver instanceof Behat\Mink\Driver\Selenium2Driver) {
$step = $event->getStep();
$path = array(
'date' => date("Ymd-Hi"),
'feature' => $step->getParent()->getFeature()->getTitle(),
'scenario' => $step->getParent()->getTitle(),
'step' => $step->getType() . ' ' . $step->getText()
);
$path = preg_replace('/[^\-\.\w]/', '_', $path);
$filename = '/tmp/behat-screenshots/' . implode('/', $path) . '.jpg';
// Create directories if needed
if (!@is_dir(dirname($filename))) {
@mkdir(dirname($filename), 0775, TRUE);
}
file_put_contents($filename, $driver->getScreenshot());
#echo sprintf('[NOTICE] A screenshot was saved to %s %s', $filename, PHP_EOL);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment