Ini*_*igo 13 php unit-testing laravel laravel-dusk
我开始使用Laravel Dusk进行浏览器测试,并创建了几个测试来测试我的登录表单.我有以下代码:
class LoginTest extends DuskTestCase
{
public function testLogin()
{
$this->browse(function (Browser $browser) {
$browser->visit('/admin')
->type('email', 'inigo@mydomain.co.uk')
->type('password', 'MyPass')
->press('Login')
->assertSee('Loading...');
});
}
public function testLoginFailure(){
$this->browse(function (Browser $browser){
$browser->visit('/admin/logout'); // I have to add this to logout first, otherwise it's already logged in for this test!
$browser->visit('/admin')
->type('email', 'someemail@afakedomain.com')
->type('password', 'somefakepasswordthatdoesntwork')
->press('Login')
->assertSee('These credentials do not match our records.');
});
}
Run Code Online (Sandbox Code Playgroud)
看评论.第一个函数运行正常,但是当涉及第二个函数时,我必须先注销,因为用户已经因运行第一个函数而登录.这让我感到意外,因为我认为单元测试是完全独立的,会话数据会自动销毁.
有没有更好的方法来做到这一点 - 我可能缺少一些黄昏方法 - 而不是打电话$browser->visit('/admin/logout');?
谢谢
编辑感谢目前为止的2个答案,这两个答案似乎都是有效的解决方案.我已将第二个函数更新为以下内容:
public function testLoginFailure(){
$this->createBrowsersFor(function(Browser $browser){
$browser->visit('/admin')
->type('email', 'someshit@afakedomain.com')
->type('password', 'somefakepasswordthatdoesntwork')
->press('Login')
->assertSee('These credentials do not match our records.');
});
}
Run Code Online (Sandbox Code Playgroud)
这项工作是做什么的.所以
Zal*_*alo 15
在我的情况下,tearDown()还不够,出于某种原因,记录的用户仍然在测试之间持续存在,所以我放置deleteAllCookies()了setUp().
所以,在我的DuskTestCase.php中,我添加了:
/**
* Temporal solution for cleaning up session
*/
protected function setUp()
{
parent::setUp();
foreach (static::$browsers as $browser) {
$browser->driver->manage()->deleteAllCookies();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我可以在所有测试中刷新会话的唯一方法.我希望它有所帮助.
注意:我正在使用Homestead和Windows 10.
如果您只想注销登录的用户,则在登录测试后,只需使用:
$browser->visit('/login')
->loginAs(\App\User::find(1))
...
some assertions
...
->logout();
Run Code Online (Sandbox Code Playgroud)
小智 6
我不确定这是否是您正在寻找的答案,但您可以创建多个浏览器来执行需要干净历史记录/cookies/缓存的测试。
例如,如果您有很多测试,其中用户应该登录,并且您不想仅在一项检查重置密码表单的测试中注销,那么您可以针对这种情况创建一个额外的浏览器并切换到进行下一个测试时使用的浏览器。
看起来可能是这样的:
public function testfirstTest()
{
$this->browse(function ($browser) {
//some actions where you login etc
});
}
public function testSecondTestWhereYouNeedToBeLoggedOut()
{
$this->browse(function ($currentBrowser, $newBrowser) {
//here the new browser will be created, at the same time your previous browser window won't be closed, but history/cookies/cache will be empty for the newly created browser window
$newBrowser->visit('/admin')
//do some actions
$newBrowser->quit();//here you close this window
});
}
public function testWhereYouShouldContinueWorkingWithUserLoggedIn()
{
$this->browse(function ($browser) {
$browser->doSomething()//here all actions will be performed in the initially opened browser with user logged in
});
}
Run Code Online (Sandbox Code Playgroud)
您可以使用以下tearDown()方法刷新会话:
class LoginTest extends DuskTestCase
{
// Your tests
public function tearDown()
{
session()->flush();
parent::tearDown();
}
}
Run Code Online (Sandbox Code Playgroud)
如果万一有帮助的话。您可以使用tearDown方法清除所有cookie。下面是这样做的示例,您可以在DuskTestCase.php文件中添加此方法
public function tearDown()
{
parent::tearDown();
$this->browse(function (Browser $browser) {
$browser->driver->manage()->deleteAllCookies();
});
}
Run Code Online (Sandbox Code Playgroud)
我希望这将有所帮助。
您可以致电
$this->logout();
Run Code Online (Sandbox Code Playgroud)
InteractsWithAuthentication - Github
编辑:
这是 Dusk 测试用例的行为,主浏览器实例保留用于其他测试。
第二种解决方案,创建第二个浏览器实例,该实例将在单次测试后销毁
| 归档时间: |
|
| 查看次数: |
4902 次 |
| 最近记录: |