Selenium and Laravel 5.2

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 04:19:02

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:

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