Selenium and Laravel 5.2

安稳与你 提交于 2019-12-01 01:47:20

问题


I'm getting sad,

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

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

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

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


回答1:


You need to install PHPUnit_selenium package using composer

composer require --dev phpunit/phpunit-selenium

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;
    }
}

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

and Run command PHPunit test from terminal

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

Reference document:

  • 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/


来源:https://stackoverflow.com/questions/35856675/selenium-and-laravel-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!