...
  View open merge request
Commits (8)
version: '3.6'
services:
chromedriver:
container_name: ddev-${DDEV_SITENAME}-chromedriver
image: robcherry/docker-chromedriver
restart: "no"
ports:
- "4444"
environment:
- CHROMEDRIVER_WHITELISTED_IPS=""
external_links:
- "ddev-router:${DDEV_SITENAME}.ddev.site"
......@@ -10,3 +10,5 @@
sequelpro.spf
.ddev/db_snapshots/
.idea/
/Tests/_output/
/Tests/_support/_generated/
# extensions.typo3.org documentation
## Acceptance Tests
### Concepts
The acceptance tests run in their own environment. In this environment we import a basic database via codeception
to have a working site and add new data per test via yaml files and steps provided by the
`punktde/codeception-database` package. When a yaml file is imported via the gherkin step, every database table
found in the yaml file is truncated before the new data is imported.
In `Tests/acceptance.suite.yml` the config for the database module can be found. We have set
`populate` and `reset` to true, so before running any test and between every test the command given as the config
option `populator` is run. If no `populator` is given, the `dumpFile` is imported.
At the moment (Nov 4th 2019) GeckoDriver and FireFox testing is only possible by using the detour via selenium,
and even then it's not really reliable.
We are running chromedriver within ddev, so if you use ddev everything just works ;)
### Run codeception tests
#### Run the tests
```
ddev run --service web vendor/bin/codecept --steps run acceptance
```
With `--steps`, all run steps are outputted to the command line. All errors are reported even if you don't use the
flag. You can also add `--xml` as a flag to get JUnit compatible output.
If you get errors, you can find the output in the directory `Tests/_output`. In there is a list of failed tests and
a screenshot and the html output of the page the moment a test failed.
##### Single feature
To run a single feature just add the path to the feature file to your command, e.g.
```
ddev run --service web vendor/bin/codecept --steps run acceptance Tests/acceptance/Features/Login.feature
```
You can also run single scenarios by providing step names, e.g.
```
ddev run --service web vendor/bin/codecept --steps run acceptance Tests/acceptance/Features/Login.feature:Login\ and\ Logout
```
### Writing tests
The acceptance tests are put into the directory `Tests/acceptance/Feature`. You can use any folder structure to
group your tests.
To get all the available step definitions just run the command
```
ddev run --service web vendor/bin/codecept gherkin:steps acceptance
```
which outputs the steps added to the project.
To add new steps, just write the new step in your `.feature` file and run
```
ddev run --service web vendor/bin/codecept gherkin:snippets acceptance
```
This will output a function stub with the proper step definition phpdoc. Put this in a appropriate `ActorTrait`
and (if needed) add the `ActorTrait` to the `AcceptanceTester` (`Tests/_support/AcceptanceTester.php`). Example
traits can be found in the directory `Classes/ActorTraits/` of the `punktde/codeception-*` packages.
<?php
use PunktDe\Codeception\Database\ActorTraits\Database;
use PunktDe\Codeception\Webdriver\ActorTraits\Webdriver;
/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class AcceptanceTester extends \Codeception\Actor
{
use _generated\AcceptanceTesterActions;
use Webdriver;
use Database;
}
<?php
namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
use Codeception\Configuration;
use Codeception\Exception\ConfigurationException;
use Codeception\Exception\ModuleException;
use Codeception\Module\Cli;
use Codeception\Module\Filesystem;
use Neos\Utility\Files;
use PunktDe\Codeception\Database\Module\Database;
class Acceptance extends \Codeception\Module
{
/**
* @param string[] $settings
* @throws ConfigurationException
* @throws ModuleException
*/
public function _beforeSuite($settings = []): void
{
$filesystem = $this->getModule('Filesystem');
/** @var Filesystem $filesystem */
$filesystem->cleanDir(Configuration::outputDir());
$cli = $this->getModule('Cli');
/** @var Cli $cli */
$db = $this->getModule(Database::class);
/** @var Database $db */
$this->debug('dumping development database');
$dumpCommand = sprintf(
'mysqldump --host=%s --user=%s --password=%s %s > %s',
$db->_getConfig('user'),
$db->_getConfig('user'),
$db->_getConfig('user'),
$db->_getConfig('user'),
Files::concatenatePaths([Configuration::dataDir(), 'localDatabase.sql'])
);
$cli->runShellCommand($dumpCommand);
}
public function _afterSuite()
{
$cli = $this->getModule('Cli');
/** @var Cli $cli */
$db = $this->getModule(Database::class);
/** @var Database $db */
$dumpfilePath = Files::concatenatePaths([Configuration::dataDir(), 'localDatabase.sql']);
$this->debug('restoring development database');
$restoreCommand = sprintf(
'mysql --host=%s --user=%s --password=%s %s < %s',
$db->_getConfig('user'),
$db->_getConfig('user'),
$db->_getConfig('user'),
$db->_getConfig('user'),
$dumpfilePath
);
$cli->runShellCommand($restoreCommand);
$filesystem = $this->getModule('Filesystem');
/** @var Filesystem $filesystem */
$filesystem->deleteFile($dumpfilePath);
}
}
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.
actor: AcceptanceTester
modules:
enabled:
# order matters - \Helper\Acceptance has to be first!
- \Helper\Acceptance
- Filesystem
- Cli
- WebDriver:
browser: chrome
url: https://ter.ddev.site/
restart: true
window_size: 1920x2080
host: chromedriver
path: /
capabilities:
chromeOptions:
args:
- '--headless'
- '--disable-gpu'
- '--disable-dev-shm-usage'
- '--no-sandbox'
- PunktDe\Codeception\Database\Module\Database:
dsn: 'mysql:host=db;dbname=db;charset=UTF8'
user: 'db'
password: 'db'
host: 'db'
dump: 'Tests/_data/Dumps/Fixture.sql'
populate: true
cleanup: true
populator: 'mysql --host=$host --user=$user --password=$password $dbName < $dump'
step_decorators: ~
paths:
tests: Tests
output: Tests/_output
data: Tests/_data
support: Tests/_support
envs: Tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
This diff is collapsed.