簡體   English   中英

Laravel 黃昏,如何在測試之間破壞 session 數據

[英]Laravel Dusk, how to destroy session data between tests

我開始使用 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.');
    });
}

見評論。 第一個 function 運行良好,但是當涉及到第二個 function 時,我必須先注銷,因為運行第一個 ZC1C425268E68385D1AB5074C17A 后用戶已經登錄。 這讓我感到驚訝,因為我認為單元測試是完全獨立的,session 數據會被自動銷毀。

有沒有比調用$browser->visit('/admin/logout');更好的方法來做到這一點 - 我可能缺少一些 Dusk 方法 - ?

謝謝

編輯感謝到目前為止的 2 個答案,這兩個答案似乎都是有效的解決方案。 我已將第二個 function 更新為以下內容:

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.');
    });
}

哪個工作。 所以

  1. 我可以放心地假設第二個瀏覽器只存在於這個單一的 function 期間,對嗎?
  2. 創建第二個瀏覽器實例而不是使用拆卸方法有哪些明顯的優點/缺點?

在我的情況下, 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();
    }
}

這是我可以圍繞所有測試刷新會話的唯一方法。 我希望它有幫助。

注意:我使用的是 Homestead 和 Windows 10。

如果您只想注銷已登錄的用戶,在登錄測試后,只需使用:

 $browser->visit('/login')
     ->loginAs(\App\User::find(1))
     ...
     some assertions
     ...
     ->logout();

您可以在tearDown()方法中刷新會話:

class LoginTest extends DuskTestCase
{
    // Your tests

    public function tearDown()
    {
        session()->flush();

        parent::tearDown();
    }
}

如果它可以幫助其他人。 您可以使用tearDown方法清除所有 cookie。 下面是這樣做的例子,你可以在 DuskTestCase.php 文件中添加這個方法

public function tearDown()
{
    parent::tearDown();

    $this->browse(function (Browser $browser) {
        $browser->driver->manage()->deleteAllCookies();
    });
}

我希望這會有所幫助。

你可以打電話

$this->logout();

InteractsWithAuthentication - Github

編輯:

這是 Dusk 測試用例的行為,主要瀏覽器實例保留用於其他測試。

第二種方案,創建第二個瀏覽器實例,單次測試后銷毀

見黃昏/測試案例#L103

我不確定它是否是您正在尋找的答案,但是您可以創建多個瀏覽器來執行需要清理歷史記錄/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
  });
}

如果您只對注銷當前用戶感興趣,您可以通過執行

     $this->browse(function($browser) {
        $browser->visit('/logout')
                ->waitForText('Sign Into Your Account');
    });

注意:請將“登錄您的帳戶”文本更改為登錄頁面上顯示的文本。

您可以在Browser實例上將setUp方法與 Dusk 的logout方法配對:

/**
 * Log the user out before each test.
 *
 * @return void
 */
protected function setUp(): void
{
    parent::setUp();

    $this->browse(
        fn (Browser $browser) => $browser->logout()
    );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM