koel/tests/e2e/TestCase.php

174 lines
3.8 KiB
PHP
Raw Normal View History

2016-11-13 15:05:24 +00:00
<?php
namespace E2E;
use App\Application;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
2016-11-23 15:36:12 +00:00
use Facebook\WebDriver\WebDriverDimension;
use Facebook\WebDriver\WebDriverPoint;
2016-11-13 15:05:24 +00:00
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Artisan;
2016-11-15 10:03:59 +00:00
abstract class TestCase extends \PHPUnit_Framework_TestCase
2016-11-13 15:05:24 +00:00
{
2016-11-15 07:54:41 +00:00
use WebDriverShortcuts;
2016-11-13 15:05:24 +00:00
/**
* @var Application
*/
protected $app;
2016-11-18 07:56:18 +00:00
/**
2016-11-23 16:47:10 +00:00
* ID of the current screen wrapper (with leading #).
2016-11-18 07:56:18 +00:00
*
* @var string
*/
public $wrapperId;
2016-11-13 15:05:24 +00:00
/**
* The default Koel URL for E2E (server by `php artisan serve --port=8081`).
*
* @var string
*/
protected $url = 'http://localhost:8081';
protected $coverPath;
/**
* TestCase constructor.
*/
public function __construct()
{
parent::__construct();
$this->createApp();
$this->resetData();
2016-11-13 15:05:24 +00:00
$this->driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', DesiredCapabilities::chrome());
2016-11-23 15:36:12 +00:00
$this->driver->manage()->window()->setSize(new WebDriverDimension(1440, 900));
$this->driver->manage()->window()->setPosition(new WebDriverPoint(0, 0));
2016-11-13 15:05:24 +00:00
}
/**
* @return Application
*/
protected function createApp()
{
$this->app = require __DIR__.'/../../bootstrap/app.php';
$this->app->make(Kernel::class)->bootstrap();
return $this->app;
}
2016-11-24 03:56:00 +00:00
/**
* Reset the test data for E2E tests.
*/
protected function resetData()
2016-11-13 15:05:24 +00:00
{
// Make sure we have a fresh database.
@unlink(__DIR__.'/../../database/e2e.sqlite');
touch(__DIR__.'/../../database/e2e.sqlite');
Artisan::call('migrate');
Artisan::call('db:seed');
Artisan::call('db:seed', ['--class' => 'E2EDataSeeder']);
if (!file_exists($this->coverPath)) {
@mkdir($this->coverPath, 0777, true);
}
}
/**
* Log into Koel.
*
* @param string $username
* @param string $password
2016-11-23 13:22:47 +00:00
*
* @return $this
2016-11-13 15:05:24 +00:00
*/
protected function login($username = 'koel@example.com', $password = 'SoSecureK0el')
{
$this->typeIn("#app > div.login-wrapper > form > [type='email']", $username);
$this->typeIn("#app > div.login-wrapper > form > [type='password']", $password);
$this->enter();
2016-11-23 13:22:47 +00:00
return $this;
2016-11-13 15:05:24 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Log in and wait for the app to finish loading.
*
* @return $this
*
* @throws \Exception
*/
2016-11-15 07:54:41 +00:00
protected function loginAndWait()
{
2016-11-24 03:56:00 +00:00
$this->login()
->seeText('Koel Admin', '#userBadge > a.view-profile');
2016-11-15 07:54:41 +00:00
return $this;
}
2016-11-13 15:05:24 +00:00
/**
2016-11-24 03:56:00 +00:00
* Go to a specific screen.
2016-11-13 15:05:24 +00:00
*
* @param $screen
*
2016-11-15 07:54:41 +00:00
* @throws \Exception
2016-11-23 16:47:10 +00:00
*
* @return $this
2016-11-13 15:05:24 +00:00
*/
2016-11-15 07:54:41 +00:00
protected function goto($screen)
2016-11-13 15:05:24 +00:00
{
2016-11-18 07:56:18 +00:00
$this->wrapperId = "#{$screen}Wrapper";
2016-11-13 15:05:24 +00:00
if ($screen === 'favorites') {
2016-11-15 07:54:41 +00:00
$this->click('#sidebar .favorites a');
2016-11-13 15:05:24 +00:00
} else {
2016-11-15 07:54:41 +00:00
$this->click("#sidebar a.$screen");
2016-11-13 15:05:24 +00:00
}
2016-11-15 07:54:41 +00:00
2016-11-23 15:36:12 +00:00
$this->see($this->wrapperId);
2016-11-18 07:56:18 +00:00
return $this;
2016-11-13 15:05:24 +00:00
}
2016-11-24 03:56:00 +00:00
/**
* Log in and go to a specific screen.
*
* @param $screen string
*
* @return $this
*
* @throws \Exception
*/
2016-11-23 13:22:47 +00:00
protected function loginAndGoTo($screen)
{
return $this->loginAndWait()->goto($screen);
}
2016-11-24 03:56:00 +00:00
/**
* Wait for the user to press ENTER key before continuing.
*/
2016-11-15 07:54:41 +00:00
protected function waitForUserInput()
2016-11-13 15:05:24 +00:00
{
2016-11-15 07:54:41 +00:00
if (trim(fgets(fopen('php://stdin', 'rb'))) !== chr(13)) {
return;
2016-11-13 15:05:24 +00:00
}
}
2016-11-15 07:54:41 +00:00
protected function focusIntoApp()
{
$this->click('#app');
}
public function setUp()
{
$this->driver->get($this->url);
}
public function tearDown()
{
$this->driver->quit();
}
2016-11-13 15:05:24 +00:00
}