Execute your automation test scripts with Selenium WebDriver instantly through LambdaTest which is a cloud-based, cross browser testing tool offering a Selenium grid consisting 2000+ browsers and browser versions running on real operating systems to pace up automation testing of your web-app or website. In this topic, you will learn how to automate your website testing using Laravel Dusk Selenium on LambdaTest.
By the end of this topic, you will be able to:
Before you begin automation testing with Laravel Dusk Selenium, be ready with the below essentials:
1 |
sudo apt-get install curl libcurl3 libcurl3-dev php |
1 |
$ composer require laravel/dusk |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
// composer.json { "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" ] } } |
1 2 |
composer install composer dump-autoload |
First, let us create .env from the example file:
1 |
cp .env.example .env |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
APP_NAME=Laravel APP_ENV=local APP_KEY= APP_DEBUG=true APP_URL=http://localhost LT_USERNAME="your username" LT_ACCESS_KEY="your access key" LOG_CHANNEL=stack DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret BROADCAST_DRIVER=log CACHE_DRIVER=file QUEUE_CONNECTION=sync SESSION_DRIVER=file SESSION_LIFETIME=120 REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= PUSHER_APP_CLUSTER=mt1 MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" |
Now we need to replace, LT_USERNAME with your LambdaTest username & LT_ACCESS_KEY with your access key. You can acquire both using LambdaTest Dashboard.
After that, you need to update platform configuration in driver method of tests/DuskTestCase.php, to specify the target where tests should run. (List of supported OS platfrom, Browser, resolutions can be found at LambdaTest Capability Generator)Sample configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//php-laravel-dusk-todo-master\tests\DuskTestCase.php return RemoteWebDriver::create($url, DesiredCapabilities::chrome() ->setCapability("platform", "win10") ->setCapability("browserName", "chrome") ->setCapability("version", "71.0") ->setCapability("resolution", "1024x768") ->setCapability("build", "LaravelDusk Build") ->setCapability("name", "LaravelDusk Test") ->setCapability("network", true) ->setCapability("video", true) ->setCapability("visual", true) ->setCapability("console", true) ->setCapability("tunnel", false) ); |
You can run Selenium test for your locally hosted webpages or web apps using the Lambda Tunnel or Underpass(GUI Tunnel Application). You will need to do the following for running Selenium test automation for locally or privately hosted projects.
Let us look at each of these steps in detail.
Download the binary file of:
To install and run Lambda Tunnel, you can follow our step-by-step documentation.
Or if you wish to avoid Command Line Interface then you can use the Underpass application.
Download:
For more details, refer to the support documentation of Underpass.
After installing and running the Lambda Tunnel, you now need to specify the tunnel value to true in your desired capabilities.
1 2 3 4 |
return RemoteWebDriver::create($url, DesiredCapabilities::chrome() ->setCapability("tunnel", true) // running tests on locally hosted web pages ); |
Let us have a look at an example which will validate your LambdaTest credentials for authentication purpose. Later, the code will select the basic capabilities such as OS, browser, browser version and so on. You will find the capabilities in the driver method of tests/DuskTestCase.php
on our Laravel Dusk Testing GitHub Repository.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php namespace Tests; use Laravel\Dusk\TestCase as BaseTestCase; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\Remote\DesiredCapabilities; abstract class DuskTestCase extends BaseTestCase { use CreatesApplication; /** * Prepare for Dusk test execution. * * @beforeClass * @return void */ public static function prepare() { // static::startChromeDriver(); } /** * Create the RemoteWebDriver instance. * * @return \Facebook\WebDriver\Remote\RemoteWebDriver */ protected function driver() { $username = env('LT_USERNAME'); $access_key = env('LT_ACCESS_KEY'); $url = "https://".$username.":".$access_key."@hub.lambdatest.com/wd/hub"; return RemoteWebDriver::create($url, DesiredCapabilities::chrome() ->setCapability("platform", "win10") ->setCapability("browserName", "chrome") ->setCapability("version", "71.0") ->setCapability("resolution", "1024x768") ->setCapability("build", "LaravelDusk Build") ->setCapability("name", "LaravelDusk Test") ->setCapability("network", true) ->setCapability("video", true) ->setCapability("visual", true) ->setCapability("console", true) ->setCapability("tunnel", false) ); } } |
You can easily generate the DesiredCapabilities with our Selenium Capabilities Generator.
Important Note: 127.0.0.1
IP address won’t work when using tunnel as different browsers exhibit different behaviours. You can use localhost
, localhost.lambdatest.com
, any custom domain if that’s entered into your etc/hosts file, or you can specify a custom domain that is managed by a custom DNS server, provided that you explicitly pass DNS server details while initiating the tunnel.
To explicitly pass DNS server details through Lambda Tunnel you will need to use the --dns
argument and pass the local host string i.e. 127.0.0.1:port number
as the value while triggering the tunnel.
Note: For PHP Laravel, 127.0.0.1:53
is the default value so you will need to specify that.
The command-syntax would look like:
1 |
LT --user {LambdaTest login email} --key {LambdaTest access key} --cui --tunnelName dusk-sample --dns 127.0.0.1:53 |
Here is an example of this command:
1 |
LT --user example@lambdatest.com --key 123asd123 --cui --tunnelName dusk-sample --dns 127.0.0.1:53 |
To explicitly pass DNS server details through the Underpass application, you will need to go to Advanced settings and provide 127.0.0.1:53
for PHP Laravel as the value for the DNS Server field.
You can find all the code files in our Laravel Dusk Testing GitHub Repository.
Let us quickly analyze our test scenario: Visit the Sample To Do App, check the first and second item from the list and add a new item i.e. ‘sampletodotext’. Here is the code file for it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace Tests\Browser; use Tests\DuskTestCase; use Laravel\Dusk\Browser; use Illuminate\Foundation\Testing\DatabaseMigrations; class TodoTest extends DuskTestCase { /** * A Dusk test example. * * @return void */ public function testExample() { $this->browse(function (Browser $browser) { $browser->visit('https://lambdatest.github.io/sample-todo-app/') ->assertTitleContains('Sample page - lambdatest.com') ->check("li1") ->check("li3") ->type("#sampletodotext", "Let's add new to do item") ->press('#addbutton'); sleep(10); }); } } |
You would need to execute the below command in your terminal/cmd:
1 |
php artisan dusk |