Skip to content

Instantly share code, notes, and snippets.

@shashikantjagtap
Last active August 16, 2017 23:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shashikantjagtap/3c25990235e7c343de42 to your computer and use it in GitHub Desktop.
Save shashikantjagtap/3c25990235e7c343de42 to your computer and use it in GitHub Desktop.
Code Snippets for MinkBundle, PHPUnit and GhostDriver post
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
.........
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
..........
);
if (in_array($this->getEnvironment(), array('test'))) {
$bundles[] = new Behat\MinkBundle\MinkBundle();
}
return $bundles;
}
......
}
<?php
namespace path\to\yourBundle\Tests;
abstract class BaseBrowserTestCase extends MinkTestCase
{
const FIXTURES_PATH = 'path\to\yourBundle\DataFixtures\ORM\\';
protected $baseUrl;
protected $session;
protected function setUp()
{
$this->baseUrl = $this->getContainer()->getParameter('mink.base_url');
$this->getMink()->setDefaultSessionName('Selenium2');
$this->session = $this->getMink()->getSession();
}
protected function webAssert()
{
return new WebAssertProxy($this->getMink()->assertSession());
}
public function visit($path)
{
$this->session->visit($this->baseUrl.$path);
}
protected function loadFixtures(array $fixtureNames, $omName = null, $registryName = 'doctrine', $purgeMode = null)
{
$classNames = array();
foreach ($fixtureNames as $fixtureName) {
$className = self::FIXTURES_PATH.$fixtureName;
array_push($classNames, $className);
}
parent::loadFixtures($classNames, $omName, $registryName, $purgeMode);
}
}
"require-dev":
{
"behat/mink-bundle": "~1.4.2",
"behat/mink-selenium2-driver": "^1.2"
}
imports:
- { resource: config_dev.yml }
mink:
base_url: 'http://localhost:7777'
selenium2:
wd_host: "http://localhost:8643/wd/hub"
<?php
namespace path\toyourBundle\Tests;
use Rmp\Branding\BrowserTestBundle\Tests\BaseMinkPHPUnitTestCase;
class HomePageTest extends BaseMinkPHPUnitTestCase
{
protected $heading = ".heading";
protected $navigation = ".navigation";
public function testhomePage()
{
$this->loadFixtures();
$this->visit('/');
$this->webAssert()->pageTextContains('BBC');
$this->webAssert()->elementExists('css', $this->heading);
$this->webAssert()->elementTextContains('css', $this->navigation, 'BBC');
}
}
<?php
namespace path\to\yourBundle\Tests;
use Behat\Mink\WebAssert;
use Behat\Mink\Exception\ExpectationException;
use BadMethodCallException;
use PHPUnit_Framework_Assert;
class WebAssertProxy
{
protected $webAssert;
/**
* Initializes assertion engine.
*
* @param Session $session
*/
public function __construct(WebAssert $webAssert)
{
$this->webAssert = $webAssert;
}
public function __call($name, $args)
{
if (!is_callable([$this->webAssert, $name])) {
throw new BadMethodCallException(sprintf(
'Tried to call "%s" on WebAssert but that method does not exist',
$name
));
}
$success = true;
$message = '';
try {
call_user_func_array(array($this->webAssert, $name), $args);
} catch (ExpectationException $e) {
$success = false;
$message = $e->getMessage();
}
PHPUnit_Framework_Assert::assertTrue($success, $message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment