如何重用Dusk测试浏览器实例? [英] How to reuse a Dusk test browser instance?

查看:112
本文介绍了如何重用Dusk测试浏览器实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel 5.4框架上的项目,我正在使用黄昏进行浏览器测试.我有一个页面,其中有几个部分我想独立测试,但是我遇到了一个问题,对于每个单独的测试,我都必须启动一个新的浏览器实例,登录并导航到该页面.

My project on the Laravel 5.4 framework and I am using Dusk for browser tests. 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();
  });
}

因此,当我在class allTheThingsTest extends DuskTestCase中有4-5个浏览器实例时,每个测试类将生成4-5个浏览器实例 .显然,这很快就失控了,特别是当我在部署所有我的测试时运行时.

So when I have 4-5 of these in class allTheThingsTest extends DuskTestCase, I'm spawning 4-5 browser instances per test class. 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:

  • 是否可以在单个测试类中的测试函数之间记住/重用浏览器实例?
  • 如果是,怎么办?

推荐答案

从laravel文档引用:

Quoting from laravel docs:

有时候,您可能希望将某些东西绑定到容器中,而该容器只能被解析一次,并且在随后对该容器的调用中应该返回相同的实例:

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

这篇关于如何重用Dusk测试浏览器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆