使用Codeception和laravel [英] Working with codeception and laravel

查看:160
本文介绍了使用Codeception和laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在代码接收方面有些挣扎. 我从事laravel 4项目.从现在开始,我们通常使用PhpUnit进行uni测试,但是页面中包含太多的Javascript,无法通过简单的DomCrawler进行接受测试,这是没有意义的. 首先,我在一个无所事事的VM上安装了Selenium + Chrome.花了我一些时间,但我设法使其正常工作,这意味着我可以浏览Webdriver网页(/wd/hub),然后成功使用chrome创建一个新会话.

I'm struggling a bit with codeception right now. I work on a laravel 4 project. From now on we used PhpUnit to uni test normally but we have too much Javascript in our pages to do acceptance tests with a simple DomCrawler it's a non sense. First i installed selenium + chrome on a vagrant VM. Took me some times but i managed to get it to work, meaning i can browse the Webdriver webpage (/wd/hub) then create a new session using chrome with success.

接下来,我通过composer安装了Codeception 我做了

Next i installed codeception via composer I did

codecept引导程序

codecept bootstrap

我将Laravel4作为模块添加到了accept.suite.yml

I added Laravel4 as a module to acceptance.suite.yml

class_name: AcceptanceTester
modules:
    enabled:
        - WebDriver
        - AcceptanceHelper
        - Laravel4
    config:
        WebDriver:
            url: 'https://192.168.33.10/'
            browser: 'chrome'

我做了一个codecept的构建. 然后我写了一个非常简单的acceptanceTest只是为了检查一切是否正常:

I did a codecept build. Then i wrote a very simple acceptanceTest just to check if everything works :

$I = new AcceptanceTester($scenario);
$I->am('a member');
$I->wantTo('connect');
$I->amOnRoute('login');
$I->see('someText');

当我执行一个Codecept时,它会在屏幕上引发一个错误:

When i do a codecept run it raises an error on screen :

[LogicException] 在遍历Finder之前,必须先调用in()或append()方法之一.

[LogicException] You must call one of in() or append() methods before iterating over a Finder.

在Selenium WebDriver页面中,它表示已创建chrome会话.

In the Selenium WebDriver Page , it says a chrome session has been created.

我搜索了有关thet错误的信息.它来自Symfony的Finder组件.

I made some searches about thet error. It comes from the Finder component of Symfony.

有人可以帮助我吗?

推荐答案

您必须选择是否要将WebDriver或Laravel4用作浏览器",不幸的是,您不能同时拥有两者,这可能是问题的根源.如果您需要Javascript,则可能需要保留WebDriver并释放一些Laravel功能,但我认为并不需要太多.如果您真的需要Laravel,这是我创建的一个帮助程序,可将其放入_support/FunctionalHelper:

You'll have to choose if you want WebDriver or Laravel4 as your "browser", unfortunately you cannot have both and this might be the source of your problems. If you need Javascript, you probably will need to keep WebDriver and loose some Laravel functionalities, but not much, in my opinion. And if you really need Laravel, this is a helper I created to get it inside _support/FunctionalHelper:

public function getLaravel4()
{
    if ( ! isset($this->laravel4))
    {
        $this->laravel4 = (new \Codeception\Module\Laravel4());

        $this->laravel4->kernel = app();
    }

    return $this->laravel4;
}

因此,在测试中,您只需要:

So in your test you just need to:

$L = $I->getLaravel4();

但是,请记住,由于Laravel不是您的浏览器,因此类似:

But, remember, since Laravel is not your browser, things like:

$L->amOnRoute('login');

将无法正常工作,但您可以执行与浏览无关的操作,例如:

Will not work, but you'll be able to do things not related to browsing, like:

$L->seeRecord('users', [
    'email' => 'a@b.com',
    'first_name' => 'a',
    'last_name' => 'b'
]);

此外,您还可以在_bootstrap文件上启动laravel:

Also, you can boot laravel on your _bootstrap file:

include __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/start.php';
$app->boot();

并可以直接访问IoC容器:

And have direct access to the IoC container:

$app = app();

$app['config']->get('...');

甚至是外墙:

DB::table('users')->...

这篇关于使用Codeception和laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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