Skip to content

Instantly share code, notes, and snippets.

@appkr
Created August 17, 2016 12:36
Show Gist options
  • Select an option

  • Save appkr/7e59474b3ef9fc1631c66734ada135f9 to your computer and use it in GitHub Desktop.

Select an option

Save appkr/7e59474b3ef9fc1631c66734ada135f9 to your computer and use it in GitHub Desktop.
<?php
namespace Test;
class SeleniumTestCase extends \PHPUnit_Extensions_Selenium2TestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://foo.dev';
/**
* The Illuminate application instance.
*
* @var \Illuminate\Foundation\Application
*/
protected $app;
/**
* The callbacks that should be run before the application is destroyed.
*
* @var array
*/
protected $beforeApplicationDestroyedCallbacks = [];
/**
* Visit the given URI with a GET request.
*
* @param $path
* @return $this
*/
public function visit($path)
{
$this->url($path);
return $this;
}
/**
* Visit the given named route with a GET request.
*
* @param $route
* @param array $parameters
* @return $this
*/
public function visitRoute($route, $parameters = [])
{
$this->url(route($route, $parameters));
return $this;
}
/**
* Assert that a given string is seen on the current HTML.
*
* @param $text
* @param string $tag
* @return $this
*/
public function see($text, $tag = 'body')
{
$this->assertContains($text, $this->byTag($tag)->text());
return $this;
}
/**
* Assert that a given string is not seen on the current HTML.
*
* @param $text
* @param string $tag
* @return $this
*/
public function dontSee($text, $tag = 'body')
{
$this->assertNotConatains($text, $this->byTag($tag)->text());
return $this;
}
/**
* Assert that a given link is seen on the page.
*
* @param $text
* @return $this
*/
public function seeLink($text)
{
$this->assertContains($text, $this->byLinkText($text));
return $this;
}
/**
* Assert that a given link is not seen on the page.
*
* @param $text
* @return $this
*/
public function dontSeeLink($text)
{
$this->assertNotContains($text, $this->byLinkText($text));
return $this;
}
/**
* Assert that the expected value is selected.
*/
public function seeIsSelected()
{
// Todo - Implement
}
/**
* Assert that the given value is not selected.
*/
public function dontSeeIsSelected()
{
// Todo - Implement
}
/**
* Assert that the current page matches a given URI.
*
* @param $path
* @return $this
*/
public function seePageIs($path)
{
$path = starts_with($path, '/') ? $path : "/{$path}";
$this->assertEquals($this->baseUrl . $path, $this->url());
return $this;
}
/**
* Assert that the current page matches a given named route.
*
* @param $route
* @param array $parameters
* @return $this
*/
public function seeRouteIs($route, $parameters = [])
{
$this->seePageIs(route($route, $parameters));
return $this;
}
/**
* Fill an input field with the given text.
*
* @param $value
* @param $name
* @return $this
*/
public function type($value, $name)
{
$this->byName($name)->value($value);
return $this;
}
/**
* Fill the form with the given data.
*
* @param $buttonText
* @param array $inputs
* @return $this
*/
public function fillForm($buttonText, $inputs = [])
{
if (! is_string($buttonText)) {
$inputs = $buttonText;
$buttonText = null;
}
foreach ($inputs as $name => $value) {
$this->type($value, $name);
}
return $this;
}
/**
* Submit a form using the button with the given text value.
*
* @param $buttonText
* @return $this
*/
public function press($buttonText)
{
$this->byXPath("//button[contains(text(), '{$buttonText}')]")
->click();
return $this;
}
/**
* Submit a form on the page with the given input.
*
* @param $buttonText
* @param array $inputs
* @return $this
*/
public function submitForm($buttonText, $inputs = [])
{
$this->fillForm($buttonText, $inputs)
->press($buttonText);
return $this;
}
/**
* Hold the browser for given seconds.
*
* @param int $seconds
* @return $this
*/
public function hold($seconds = 3)
{
sleep($seconds);
return $this;
}
/**
* Set up the test environment.
*/
protected function setUp()
{
parent::setUp();
$this->setBrowser('firefox');
$this->setBrowserUrl($this->baseUrl);
if (! $this->app) {
$this->refreshApplication();
}
$this->setUpTraits();
}
/**
* Clean up the testing environment before the next test.
*/
protected function tearDown()
{
parent::tearDown();
if ($this->app) {
$this->app->flush();
}
}
/**
* Refresh the application instance.
*/
protected function refreshApplication()
{
putenv('APP_ENV=testing');
$this->app = $this->createApplication();
}
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
protected function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
/**
* Register a callback to be run before the application is destroyed.
*
* @param callable $callback
* @return void
*/
protected function beforeApplicationDestroyed(callable $callback)
{
$this->beforeApplicationDestroyedCallbacks[] = $callback;
}
/**
* Boot the testing helper traits.
*
* @return void
*/
protected function setUpTraits()
{
$uses = array_flip(class_uses_recursive(static::class));
if (isset($uses[DatabaseMigrations::class])) {
$this->runDatabaseMigrations();
}
if (isset($uses[DatabaseTransactions::class])) {
$this->beginDatabaseTransaction();
}
if (isset($uses[WithoutMiddleware::class])) {
$this->disableMiddlewareForAllTests();
}
if (isset($uses[WithoutEvents::class])) {
$this->disableEventsForAllTests();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment