Laravel Dusk,如何在测试之间销毁会话数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44906797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Dusk, how to destroy session data between tests
提问by Inigo
I am getting started using Laravel Dusk for browser testing, and have created a couple of tests to test my login form. I have the following code:
我开始使用 Laravel Dusk 进行浏览器测试,并创建了几个测试来测试我的登录表单。我有以下代码:
class LoginTest extends DuskTestCase
{
public function testLogin()
{
$this->browse(function (Browser $browser) {
$browser->visit('/admin')
->type('email', '[email protected]')
->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', '[email protected]')
->type('password', 'somefakepasswordthatdoesntwork')
->press('Login')
->assertSee('These credentials do not match our records.');
});
}
See the comment. The first function runs fine, but when it comes to the second function, I have to logout first, since the user is already logged in as a result of running the first function. This came as a surprise to me as I thought unit tests were completely independent, with session data being destroyed automatically.
见评论。第一个函数运行良好,但是当涉及到第二个函数时,我必须先注销,因为运行第一个函数的结果是用户已经登录。这让我感到惊讶,因为我认为单元测试是完全独立的,会话数据会自动销毁。
Is there a better way of doing this- some Dusk method that I'm missing perhaps- than having to call $browser->visit('/admin/logout');
?
有没有更好的方法来做到这一点——我可能缺少一些 Dusk 方法——而不是必须调用$browser->visit('/admin/logout');
?
Thanks
谢谢
EDITThanks for the 2 answers so far, which both seem valid solutions. I've updated the second function to the following:
编辑感谢到目前为止的 2 个答案,这两个答案似乎都是有效的解决方案。我已将第二个函数更新为以下内容:
public function testLoginFailure(){
$this->createBrowsersFor(function(Browser $browser){
$browser->visit('/admin')
->type('email', '[email protected]')
->type('password', 'somefakepasswordthatdoesntwork')
->press('Login')
->assertSee('These credentials do not match our records.');
});
}
Which does the job. So
哪个工作。所以
- I can safely assume that this second browser only exists for the duration of this single function, correct?
- What are the obvious advantages/disadvantages of creating a second browser instance rather than using the teardown method?
- 我可以安全地假设第二个浏览器只在这个单一功能的持续时间内存在,对吗?
- 创建第二个浏览器实例而不是使用拆卸方法有哪些明显的优点/缺点?
回答by Zalo
In my case, tearDown()
was not enough, for some reason, a logged users was still persisted between tests, so I placed deleteAllCookies()
at setUp()
.
就我而言,tearDown()
是不够的,由于某种原因,一个登录用户仍坚持测试之间,所以我放置deleteAllCookies()
在setUp()
。
So, in my DuskTestCase.phpI added:
所以,在我的DuskTestCase.php 中,我添加了:
/**
* Temporal solution for cleaning up session
*/
protected function setUp()
{
parent::setUp();
foreach (static::$browsers as $browser) {
$browser->driver->manage()->deleteAllCookies();
}
}
It was the only way I could flush up session around all tests. I hope it helps.
这是我可以围绕所有测试刷新会话的唯一方法。我希望它有帮助。
Note:I'm using Homestead and Windows 10.
注意:我使用的是 Homestead 和 Windows 10。
回答by branci
If you just want to logout your signed in user, after login test, simply use:
如果您只想注销已登录的用户,在登录测试后,只需使用:
$browser->visit('/login')
->loginAs(\App\User::find(1))
...
some assertions
...
->logout();
回答by Martin Bean
You could flush the session in a tearDown()
method:
您可以在一个tearDown()
方法中刷新会话:
class LoginTest extends DuskTestCase
{
// Your tests
public function tearDown()
{
session()->flush();
parent::tearDown();
}
}
回答by hashmi
If in case it helps some one else. You can use tearDown
method to clear all cookies. Following is the example of doing so, you can add this method in DuskTestCase.php file
如果它可以帮助其他人。您可以使用tearDown
方法清除所有 cookie。下面是这样做的例子,你可以在 DuskTestCase.php 文件中添加这个方法
public function tearDown()
{
parent::tearDown();
$this->browse(function (Browser $browser) {
$browser->driver->manage()->deleteAllCookies();
});
}
I hope this will help.
我希望这将有所帮助。
回答by OuailB
You can call
你可以打电话
$this->logout();
InteractsWithAuthentication - Github
InteractsWithAuthentication - Github
Edit:
编辑:
This is the behaviour of the Dusk Test case, the primary browser instance remains for the other tests.
这是 Dusk 测试用例的行为,主要浏览器实例保留用于其他测试。
A second solution, create a second browser instance, which will be destroyed after the single test
第二种方案,创建第二个浏览器实例,单次测试后销毁
回答by Irina Malishevskaya
I'm not sure if its the answer you are looking for, but you can create multiple browsers to perform a test that requires clean history/cookies/cache.
我不确定它是否是您正在寻找的答案,但是您可以创建多个浏览器来执行需要清理历史记录/cookies/缓存的测试。
For example, if you have lots of tests where the user should be logged in and you don't want to log out for only one test that checks reset password form, then you can create an additional browser for this exact case and switch to the previous browser when moving to the next tests.
例如,如果您有很多用户应该登录的测试,并且您不想只退出一个检查重置密码表单的测试,那么您可以为这种情况创建一个额外的浏览器并切换到移动到下一个测试时的前一个浏览器。
It might look the next way:
它可能看起来像下一个方式:
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
});
}