I've been trying to integrate Laravel Dusk into my testing scheme for a week and can't get any test to actually deliver expected results. Here's the situation:
Out of the box the tests didn't work
I added the steps found in this article on "Laravel Dusk on Homestead" and the gist found here in "setup-headless-selenium-xvfb.sh" this to my provisioning file. This removed a lot of the exceptions I was getting.
I also added all my existing environment vars to the php node of my phpunit.dusk.xml
file exactly as they were done so in the already successfully running phpunit tests from phpunit.xml
However now when I run the tests I just can't get the expected output. This is what I am doing. I add an input field in my home page ('/') view file as such: <input id="dusk-test" value="1234">
I run this test which is a mod of the original example test and is the only test:
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
class ExampleTest extends DuskTestCase
{
public function testBasicExample()
{
$this->browse(function (Browser $browser)
{
$browser->visit('/')->refresh()
->assertValue('#dusk-test', '1234')
;
});
}
}
...by running php artisan dusk
and this is my output EVERY time
PHPUnit 6.4.3 by Sebastian Bergmann and contributors.
E 1
/ 1 (100%)
Time: 1.07 seconds, Memory: 12.00MB
There was 1 error:
1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\NoSuchElementException: no such element:
Unable to locate element: {"method":"id","selector":"dusk-test"}
(Session info: headless chrome=62.0.3202.62)
(Driver info: chromedriver=2.32.498513 (2c63aa53b2c658de596ed550eb5267ec5967b351),platform=Linux 4.4.0-92-generic x86_64)
/home/vagrant/landing/vendor/facebook/webdriver/lib/Exception/WebDriverException.php:102 /home/vagrant/landing/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:320
/home/vagrant/landing/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:535
/home/vagrant/landing/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:175
/home/vagrant/landing/vendor/laravel/dusk/src/ElementResolver.php:281
/home/vagrant/landing/vendor/laravel/dusk/src/ElementResolver.php:327
/home/vagrant/landing/vendor/laravel/dusk/src/Concerns/MakesAssertions.php:632
/home/vagrant/landing/tests/Browser/ExampleTest.php:22
/home/vagrant/landing/vendor/laravel/dusk/src/TestCase.php:92
/home/vagrant/landing/tests/Browser/ExampleTest.php:24
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
To make this even more confusing, this is my output when I dump from the test. Here's how I dump
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
class ExampleTest extends DuskTestCase
{
/**
* A basic browser test example.
*
* @return void
*/
public function testBasicExample()
{
$this->browse(function (Browser $browser)
{
$browser->visit('/')
->dump()
;
});
}
}
and my output after running php artisan dusk
again is
PHPUnit 6.4.3 by Sebastian Bergmann and contributors.
"<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>"
Which is absolutely NOT my homepage. I also dumped the $url
value from vendor/laravel/dusk/src/Browser.php
and got my projects correct APP_URL
.
I'm at a loss. Dusk is being sent the right location and the page definitely has the input and value. But, I can't get Dusk to give the expected output which would be that 12345 was retrieved from the element.
All help appreciated.
For laravel dusk to be able to run the browser automation test on your local environment, It needs to know your application url. For this, Go to your environment file . env , and set the APP_URL property to be your application url. This value should match the URL you use to access your application in the browser.
Laravel Dusk provides an expressive, easy-to-use browser automation and testing API. By default, Dusk does not require you to install JDK or Selenium on your local computer. Instead, Dusk uses a standalone ChromeDriver installation. However, you are free to utilize any other Selenium compatible driver you wish.
The latest Laravel version is version 9, which was released on February 8, 2022.
You can also find out the browser version by passing the result of Agent::browser() to the version() method like so: 1$browser = Agent::browser(); 2$version = Agent::version($browser);
maybe what i'm saying is wrong ... but Laravel dusk seems to need dusk
instead of id
: like dusk="dusk-test"
. also call it after :
$browser->visit('/')>refresh()
->assertValue('@dusk-test', '1234')
and it should be like the doc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With