硒和Laravel 5.2 [英] Selenium and Laravel 5.2

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

问题描述

我很难过,

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

I use Laravel 5.2 and I am developping my unit tests.

在Laravel 5.1中,您可以使用强大的Integrated lib来使用Selenium,但在Laravel 5.2中似乎不起作用

In Laravel 5.1, you could use the great Integrated lib to use selenium, but it doesn't seem to work in Laravel 5.2

因此,基本上,L5.2和Selenium之间是否存在任何形式的集成,或者不能很好地使用它?

So basically, Is there any kind of integration between L5.2 and Selenium, or is it imposible to use it nicely?

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

In this case, I should definitively have stayed in L5.1 as testing is a fundamental part of my app :(

推荐答案

您需要使用composer来安装PHPUnit_selenium软件包

You need to install PHPUnit_selenium package using composer

composer require --dev phpunit/phpunit-selenium

在laravel/tests/

Create Selenium Test Case class inside laravel/tests/

<?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的新测试用例

and Create new test case for visiting home page url

<?php    
class ExampleTest extends SeleniumTestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testTitle()
    {
        $this->visit('/')
            ->see('Site title','title');
    }
}

并从终端运行命令PHPunit test

and Run command PHPunit test from terminal

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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