Laravel Automated Testing
Let's start with making sure PHP is available on your system.
For Windows:- You can download PHP for Windows from https://windows.php.net/download/.
- Run the installer and follow the setup wizard to install PHP.
- Make sure you can use PHP with your command line: https://www.php.net/manual/en/install.windows.php.
PHP should already be present on macOS by default.
Installation
- Make sure you have Composer installed.
-
Install the Laravel Dusk Framework and other dependencies creating this
composer.json
file:{ "name": "laravel/laravel", "type": "project", "description": "The Laravel Framework.", "keywords": [ "framework", "laravel" ], "license": "MIT", "require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "5.7.*", "laravel/tinker": "^1.0" }, "require-dev": { "beyondcode/laravel-dump-server": "^1.0", "filp/whoops": "^2.0", "fzaninotto/faker": "^1.4", "laravel/dusk": "^4.0", "mockery/mockery": "^1.0", "nunomaduro/collision": "^2.0", "phpunit/phpunit": "^7.0" }, "config": { "optimize-autoloader": true, "preferred-install": "dist", "sort-packages": true }, "extra": { "laravel": { "dont-discover": [] } }, "autoload": { "psr-4": { "App\\": "app/", "Tests\\": "tests/" }, "classmap": [ "database/seeds", "database/factories", "tests/TestCase.php" ] }, "autoload-dev": { "psr-4": { "Tests\\": "tests/" } }, "minimum-stability": "dev", "prefer-stable": true, "scripts": { "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover --ansi" ], "post-root-package-install": [ "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" ], "post-create-project-cmd": [ "@php artisan key:generate --ansi" ] } }
-
Install the dependencies by running:
-
Install the necessary Dusk files:
Configuration
If you followed the steps above, you should now have a tests/DuskTestCase.php
file.
You can now generate a sample test file, for example with:
To connect your test with TestingBot, you'll need to edit the tests/DuskTestCase.php
file with the following contents:
protected function driver()
{
return RemoteWebDriver::create(
"https://api_key:api_secret@hub.testingbot.com/wd/hub",
array("platform"=>"WINDOWS", "browserName"=>"chrome", "version" => "latest"), 120000);
}
To run the test, please enter this command:
Configuring capabilities
To run your existing tests on TestingBot, your tests will need to be configured to use the TestingBot remote machines. If the test was running on your local machine or network, you can simply change your existing test like this:
Before: After:Specify Browsers & Devices
To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.
$web_driver = RemoteWebDriver::create(
"http://api_key:api_secret@hub.testingbot.com/wd/hub",
$caps, 120000);
To see how to do this, please select a combination of browser, version and platform in the drop-down menus below.
Testing Internal Websites
We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.
The example below shows how to easily run a Laravel Dusk test with our Tunnel:
1. Download our tunnel and start the tunnel:
2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub'
like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'
. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.
This way your test will go securely through the tunnel to TestingBot and back:
Other PHP Framework examples
-
Behat
Behat is a BDD framework which runs on PHP.
Mink is used for its browser emulation and works nicely together with Behat. -
Codeception
Codeception is a BDD-styled PHP testing framework.
This testing framework offers good Selenium support. -
Laravel Dusk
Laravel Dusk provides an easy-to-use browser automation testing framework.
-
PHPUnit
PHPUnit is the most popular unit testing framework for PHP.
It comes with good Selenium WebDriver support and is easy to set up. -
SimpleTest
SimpleTest is a framework for unit testing, web site testing and mock objects for PHP.