【问题标题】:Laravel Dusk - Reuse browser with its session and cookiesLaravel Dusk - 重用浏览器及其会话和 cookie
【发布时间】:2018-06-30 01:50:01
【问题描述】:

我刚刚使用 Laravel Dusk 测试了一个 javascript 网站。

出于某种原因想要重用当前浏览器及其会话和 cookie(让我在网站上保持登录状态),所以我不需要通过身份验证过程。

有什么方法可以重用当前浏览器?

我已经对此进行了搜索,但没有找到实用的信息/示例。 注意:我默认使用 Dusk 和 Google Chrome 和独立的 ChromeDriver(不是 Selenium)

【问题讨论】:

  • Dusk 已经在一个测试类中重用了浏览器并让你保持登录状态。你想在多个类中重用它吗?
  • 是的,我的意思是在多个类上重用,实际上是在通过命令运行 laravel 黄昏时(php artisan黄昏 --filter=TestClass)。当我第一次通过命令运行测试时,它需要进行身份验证。当我运行另一个测试时,有没有办法使用第一个测试中的会话和 cookie?感谢您回复顺便说一句
  • 我认为最好的方法是使用 tearDownAfterClass()setUpBeforeClass() 保存 cookie 并在下次调用时恢复它们。
  • 我在 Laravel Dusk 使用的 facebook/php-webdriver 上找到了获取 cookie 和设置 cookie 的方法。我将尝试与 tearDownAfterClass() 和 setUpBeforeClass() 合作
  • @AriePratama 你解决了吗?我也有类似的要求。

标签: php laravel laravel-dusk


【解决方案1】:

我在使用 Laravel Dusk 时也有同样的要求。诀窍是使用 Facebook/WebDriver/Remote/RemoteWebDriver 类及其manage() method

伪代码:

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Cookie;
use Facebook\WebDriver\WebDriverOptions;
use Laravel\Dusk\Browser;
use Laravel\Dusk\Chrome\ChromeProcess;

//the cookie file name
$cookie_file = 'cookies.txt';

//create the driver
$process = (new ChromeProcess)->toProcess();
$process->start();
$options = (new ChromeOptions)->addArguments(['--disable-gpu','--enable-file-cookies','--no-sandbox']);
$capabilities = DesiredCapabilities::chrome()->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = retry(5, function () use($capabilities) {
    return RemoteWebDriver::create('http://localhost:9515', $capabilities);
}, 50); 

//start the browser
$browser = new Browser($driver);
$browser->visit('https://tehwebsite.com/login');

//Cookie management - if there's a stored cookie file, load the contents         
if (file_exists($cookie_file)) {
    //Get cookies from storage
    $cookies = unserialize(file_get_contents($cookie_file));
    //Add each cookie to this session
    foreach ($cookies as $key => $cookie) {
        $driver->manage()->addCookie($cookie);
    }
}

//if no cookies in storage, do the browser tasks that will create them, eg by logging in 
$browser
    ->type('email', 'login@email.com')
    ->type('password', 'sdfsdfsdf')
    ->check('rememberMe')
    ->click('#login');

//now the cookies have been set, get and store them for future runs
if (!file_exists($cookie_file)) {
    $cookies = $driver->manage()->getCookies();
    file_put_contents($cookie_file, serialize($cookies));
}

【讨论】:

  • 你救了我的命 :) ,非常感谢。我在使用会话时失败了:RemoteWebDriver::createBySessionId('localhost:9515', $capabilities); ...您知道这是如何工作的吗?为什么我不能以这种方式完全重用会话。
【解决方案2】:

当你使用 laravel 黄昏时,我找到了一种将cookie设置到浏览器的方法:

使用planCookie函数时使用两次访问,如下代码:


$this->browse(function (Browser $browser) {
    $url = 'https://google.com';
    $browser->visit($url);
    $browser->plainCookie('key', 'value');
    $browser->visit($url);
    $browser->assertSee('Some Text');
});

希望这有帮助。

【讨论】:

  • 这为我解决了问题——谢谢! :D
猜你喜欢
  • 2021-07-07
  • 2019-11-05
  • 2018-10-25
  • 2018-03-13
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
相关资源
最近更新 更多