-
-
Save shashikantjagtap/3c25990235e7c343de42 to your computer and use it in GitHub Desktop.
Code Snippets for MinkBundle, PHPUnit and GhostDriver post
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 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; | |
} | |
...... | |
} | |
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 | |
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); | |
} | |
} |
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
"require-dev": | |
{ | |
"behat/mink-bundle": "~1.4.2", | |
"behat/mink-selenium2-driver": "^1.2" | |
} |
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
imports: | |
- { resource: config_dev.yml } | |
mink: | |
base_url: 'http://localhost:7777' | |
selenium2: | |
wd_host: "http://localhost:8643/wd/hub" |
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 | |
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'); | |
} | |
} |
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 | |
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