硒和 Laravel 5.2

Posted

技术标签:

【中文标题】硒和 Laravel 5.2【英文标题】:Selenium and Laravel 5.2 【发布时间】:2016-06-21 18:24:10 【问题描述】:

我很难过,

我使用 Laravel 5.2,并且正在开发我的单元测试。

在 Laravel 5.1 中,您可以使用强大的集成库来使用 selenium,但它似乎在 Laravel 5.2 中不起作用

所以基本上,L5.2 和 Selenium 之间是否有任何集成,或者说不能很好地使用它?

在这种情况下,我绝对应该留在 L5.1,因为测试是我的应用程序的基本部分:(

【问题讨论】:

laracasts.com/discuss/channels/testing/… 【参考方案1】:

你需要使用 composer 安装 phpUnit_selenium 包

composer require --dev phpunit/phpunit-selenium

在 laravel/tests/ 中创建 Selenium 测试用例类

<?php

class SeleniumTestCase extends PHPUnit_Extensions_Selenium2TestCase

    /**
     * The base URL to use while testing the application.
     *
     * @var string
     */
    protected function setUp()
    
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost:8000/');
    

    protected function visit($path)
    
        $this->url($path);
        return $this;
    

    protected function see($text, $tag = 'body')
    
        print_r(request()->session()->all());
        //method call by tag name;
        $this->assertContains($text,$this->byTag($tag)->text());
        return $this;
    

    protected function pressByName($text)
        $this->byName($text)->click();
        return $this;
    
    protected function pressByTag()
        $this->byTag('button')->click();
        return $this;
    
    protected function type($value, $name)
    
        $this->byName($name)->value($value);
        return $this;
    

    protected function hold($seconds)
        sleep($seconds);
        return $this;
    

并为访问主页 url 创建新的测试用例

<?php    
class ExampleTest extends SeleniumTestCase

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testTitle()
    
        $this->visit('/')
            ->see('Site title','title');
    

并从终端运行命令 PHPunit test

java -jar /usr/local/bin/selenium-server-standalone-2.35.0.jar

参考文档:

https://gist.github.com/dhavalv/85cd0e8a9a5355543787f882dca0b7cf https://www.leaseweb.com/labs/2013/09/testing-your-project-with-phpunit-and-selenium/ https://www.sitepoint.com/using-selenium-with-phpunit/

【讨论】:

以上是关于硒和 Laravel 5.2的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5.2 无法打开 laravel.log

Laravel 5.2 更改用户密码功能

在laravel 5.2中,Laravel手动登录始终不匹配

Laravel 5.2 分页自定义

Laravel 5.2 - 使用 composer 和 git bash 安装 laravel 时出错

从其他域登录 laravel 5.2