简体   繁体   English

如何重用 Dusk 测试浏览器实例?

[英]How to reuse a Dusk test browser instance?

My project on the Laravel 5.4 framework and I am using Dusk for browser tests.我在 Laravel 5.4 框架上的项目,我正在使用Dusk进行浏览器测试。 I have a page that has several sections I'd like to test independently, however I'm running into the problem where I have to start a new browser instance, login, and navigate to that page, for each individual test.我有一个页面,其中有几个部分我想独立测试,但是我遇到了一个问题,我必须为每个单独的测试启动一个新的浏览器实例、登录并导航到该页面。

public function testExample()
{
  $this->browse(function (Browser $browser) {
    $browser->loginAs(1)
            ->visit('/admin/dashboard')
            ->assertABC()
            ->assertXYZ();
  });
}

So when I have 4-5 of these in class allTheThingsTest extends DuskTestCase , I'm spawning 4-5 browser instances per test class .因此,当我在class allTheThingsTest extends DuskTestCase有 4-5 个这些时,我会为每个测试类生成4-5 个浏览器实例。 Obviously this gets out of hand quickly, especially when I'm running all of my tests pre-deployment.显然,这很快就会失控,尤其是当我在部署前运行所有测试时。

One browser instance per test class is acceptable as far as I'm concerned, but I can't figure out how to make that happen.就我而言,每个测试类一个浏览器实例是可以接受的,但我不知道如何实现这一点。 So here is what I'm asking:所以这就是我要问的:

  • Is it possible to remember/reuse a browser instance between test functions within a single test class?是否可以在单个测试类中的测试函数之间记住/重用浏览器实例?
  • If so, how?如果是这样,如何?

Quoting from laravel docs: 引用laravel文档:

Sometimes, you may wish to bind something into the container that should only be resolved once, and the same instance should be returned on subsequent calls into the container: 有时,您可能希望将某些内容绑定到仅应解析一次的容器中,并且应在后续调用容器时返回相同的实例:

$this->app->singleton('FooBar', function($app)
{
    return new FooBar($app['SomethingElse']);
});

I feel like typically you would want a fresh browser instance for each test in your test class so that each test is starting out in a "fresh" state.我觉得通常情况下,您希望测试类中的每个测试都有一个新的浏览器实例,以便每个测试都以“新鲜”状态开始。 Basically serving the same purpose as Laravel's DatabaseTransactions/RefreshDatabase testing traits.基本上与 Laravel 的 DatabaseTransactions/RefreshDatabase 测试特性具有相同的目的。

However, if you do not want to login every time/every test method, you could try something similar to the following:但是,如果您不想每次/每次测试方法都登录,则可以尝试类似于以下内容:

class ExampleTest extends DuskTestCase
{
    /**
     * An single instance of our browser.
     *
     * @var Browser|null
     */
    protected static ?Browser $browser = null;

    /**
     * Get our test class ready.
     *
     * @return void
     */
    protected function setUp(): void
    {
        parent::setUp();

        if (is_null(static::$browser)) {
            $this->browse(function (Browser $browser) {
                $browser->loginAs(1);
                static::$browser = $browser;
            });
        }
    }

    /** @test */
    public function first_logged_in_use_case()
    {
        static::$browser->visit('/admin/dashboard')->assertABC();
    }

    /** @test */
    public function second_logged_in_use_case()
    {
        static::$browser->visit('/admin/dashboard')->assertXYZ();
    }
}

I haven't tested this but essentially you're creating a static class property and assigning a logged in browser instance to it.我还没有测试过这个,但本质上你正在创建一个静态类属性并为其分配一个登录的浏览器实例。 Then you can use that same instance across all your test methods in your class.然后,您可以在类中的所有测试方法中使用相同的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM