Closed lanort closed 3 years ago
Are you sure that the browser doesn't has any session based on previous tests/assertion?
Are you sure that the browser doesn't has any session based on previous tests/assertion?
@crynobone I think so. Why should this else happen on a new installation? I also dropped my previous docker app with sail down -v
. So I think there should be nothing persistent, should it?
The issue is not, that the assertGuest fails (which would sound like the session you mentioned), but that the type command failed. So if it would a redirect and there is no input email field, both tests would fail. Right?
So if it would a redirect and there is no input email field, both tests would fail. Right?
Looking at the code against and I think I know the reason why.
assertGuest()
actually require dusk to visit _dusk/user
page and it doesn't do any redirect from there.
You should change the code from:
$browser->visit('/login')->assertGuest();
// to
$browser->assertGuest()->visit('/login');
@crynobone Oh wow. Respect. That solved it. Wow 👍
I just saw there are two different assertGuest
functions for dusk and normal HTTP testing.
Normal Laravel HTTP testing seems to not has this side effect of changing the URL: vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithAuthentication.php
/**
* Assert that the user is not authenticated.
*
* @param string|null $guard
* @return $this
*/
public function assertGuest($guard = null)
{
$this->assertFalse($this->isAuthenticated($guard), 'The user is authenticated');
return $this;
}
and the dusk version which changes the current URL:
vendor/laravel/dusk/src/Concerns/InteractsWithAuthentication.php
/**
* Assert that the user is not authenticated.
*
* @param string|null $guard
* @return $this
*/
public function assertGuest($guard = null)
{
PHPUnit::assertEmpty(
$this->currentUserInfo($guard), 'The user is unexpectedly authenticated.'
);
return $this;
}
So I don't know if it makes sense to adapt those? Probably there is a reason for the difference. Or maybe we could document it, that the dusk assertGuests changes the URL.
Thanks anyway for the quick resolution. You are free to close this or take my thoughts into consideration.
Submitted a PR #912 to redirect after assertion.
This doesn't affect normal HTTP tests because that only mimic a Request, while Dusk tests require an actual browser to hit an actual request.
If I use the
assertGuest
function, the next call totype
(into eg the email field) causes aFacebook\WebDriver\Exception\NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"body email"}
.If I remove the call to assertGuest() , it works again.
Used System
Steps to reproduce:
1) Install laravel
C:\Users\alexa\Desktop>
laravel new guesttest
Install Breeze, Sail, Dusk
C:\Users\alexa\Desktop>
cd guesttest
C:\Users\alexa\Desktop\guesttest>
composer require laravel/breeze --dev
C:\Users\alexa\Desktop\guesttest>
php artisan breeze:install
C:\Users\alexa\Desktop\guesttest>
npm install
C:\Users\alexa\Desktop\guesttest>
npm run dev
C:\Users\alexa\Desktop\guesttest>
php artisan sail:install
C:\Users\alexa\Desktop\guesttest>
composer require --dev laravel/dusk
C:\Users\alexa\Desktop\guesttest>
php artisan dusk:install
Files
.env (not changed by me)
tests\Browser\LoginTest.php
C:\Users\alexa\Desktop\guesttest>
php artisan dusk:make LoginTest
Test created successfully.
This testclass contains two functions with the same code, except, that one uses assertGuest , while the other does not.
Setting up sail (in Windows Terminal -> Ubuntu)
alex@DESKTOP-2DSRHN6:/mnt/c/Users/alexa/Desktop/guesttest$
vendor/bin/sail up -d
Run the tests
alex@DESKTOP-2DSRHN6:/mnt/c/Users/alexa/Desktop/guesttest$
sail dusk tests/Browser/LoginTest.php
As you can see, the first test works, the second does not. I have absolutely no clue why 🤷♂️