Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shhetri's avatar

Has anyone tried Laravel Integrated package in Laravel 5.2?

Hello! I am trying to write some acceptance tests. This would be my first time writing an acceptance test. I also need to test javascript related stuffs which requires the use of selenium, I guess.

I do now want to directly jump into Behat or Codeception for this. I have watched Laravel Integrated testing series and loved the API it provided. It also has selenium integrated which is exactly what I need right now. But I am not able to install the package in Laravel 5.2. Has anyone tried the package in 5.2, if not, any suggestions on what to opt for writing acceptance tests in 5.2??

0 likes
16 replies
Jeremie's avatar

I'm at the same point. I don't really understand why it's not out of the box in Laravel 5.2. I will try to install it, if it doesn't work I will switch to codeception ...

JeffreyWay's avatar

Excluding the Selenium integration, much of that package has already been ported over to Laravel.

3 likes
shhetri's avatar

@JeffreyWay I do know that. But what I need right now is the Selenium integration, and your package is really awesome for that. Like I said, I do not want to dive into codeception or behat, as it will be a whole new thing to learn for me. Any possibility on updating your package to make it work with 5.2? If not the whole package, may be you can update the Selenium integration api and extract that to a new repo.

3 likes
indexis's avatar

@JeffreyWay Any updates on using Selenium and Laravel 5.2? I was able to run it today, but I couldn't login with valid credentials... :(

cabrerabywaters's avatar
Level 4

I ended up using selenium for PHP (Selenium2). Its actually a mix between Selenium and Facebook's Webdrivers. Is not as pretty as the one that Laravel 5.1 used to have, but you cant actually create a couple wrapper functions and make it look just like it. If you do want to use it, here are some stepts that could help you get started.

1.-Make sure you have the selenium Server Jar downloaded (http://www.seleniumhq.org/download/). You can place it under Tests/bin.

2.- You could just use firefox or chrome as testing browsers, but I usually don't want my browser to open each time so I use Phantomjs (http://phantomjs.org/), just download the current version and place the complete phantomJS folder under de same directory Tests/bin

3.- Make sure you include both Selenium and Webdrivers in your composer.json dependencies. "facebook/webdriver": "^1.1", "phpunit/phpunit-selenium": "^3.0"

4.- Just as Laravel provides a TestCase class for you to extend, I did the same but for Selenium. Create a TestLoader class at the root of your Tests Folder. Here is part of my TestLoader class

<?php
 require_once __DIR__ . '../../../vendor/autoload.php';

class TestLoader extends \PHPUnit_Extensions_Selenium2TestCase {

    protected $Baseurl = 'BASE URL FOR YOUR APP';
    protected $env = 'local';
 

    protected function setUp() {
        error_reporting(0);
        if($this->env == 'local')
        {            
            $this->setBrowser('phantomjs');
            $this->setBrowserUrl($this->Baseurl);
            $this->run_selenium_server();
            $this->run_phantom_js();
        }
//THIS ELSE IS TO USE TRAVIS AND SOUCE LABS TO RUN THE TEST FOR CONTINOUS INTEGRATION
//IF YOU DONT USE IT, JUST DELETE IT
else{
            $this->setPort(80);
            $user = 'YOUR USERNAME';        
            $token = 'YOUR TOKEN';
            $this->setHost("$user:$token@ondemand.saucelabs.com");
            $this->setPort(80);
            $this->setBrowser( 'firefox' );
            $this->setBrowserUrl( $this->Baseurl );
        }       
      
    }

    protected function run_selenium_server()
    {
        if($this->selenium_server_already_running())
        {
            fwrite(STDOUT, "Selenium server already running\n");
        }
        else
        {
            shell_exec("java -jar " . __DIR__ . "..\bin\selenium-server-standalone-2.52.0.jar");
        }
    }

    protected function run_phantom_js()
    {
        if ($this->phantom_js_already_running()) {
            fwrite(STDOUT, "PhantomJS already running\n");
        } else {
            fwrite(STDOUT, "Starting PhantomJS\n");
            shell_exec(__DIR__ . "tests\bin\phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444");
        }
    }

    protected function selenium_server_already_running()
    {
        return fsockopen("localhost", 4444);
    }

    protected function phantom_js_already_running()
    {
        try {
            return fsockopen("localhost", 8080);
        } catch (Exception $e) {
        }
    }

protected function setUpPage()
    {
         $this->currentWindow()->maximize();
    }

In this TestLoader I also created a couple functions to make my life easier. I really liked the way Laravel wrapped Selenium so I pretty much used the same names. Here ara a couple of them if you want to take a look

    protected function takeScreenShot($location){
        $fp = fopen($location,'wb');
        fwrite($fp,$this->currentScreenshot());
        fclose($fp);
    }

    protected function see($name)
    {
        return $this->byXpath("//*[contains(text(),'".$name."')]");
    }

    protected function click($name)
    {
        $element = $this->byXpath("//*[contains(text(),'".$name."')]");
        $element->click();
    }

    protected function seePageIs($name)
    {
        $this->assertEquals($this->Baseurl.$name, $this->url()); 
    }

    protected function clickLink($link)
    {
        $element = $this->byXpath("//a[@href='".$link."']");
        $element->click();
    }

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

If you are like me, and don't feel like learning WebDrivers API, the good thing about using Selenium2 is that you can also use some good old Jquery to help you around selecting items or putting some data in your forms. I ended up creating my own Javascript functions like this.

 protected function clickCustomSelectWith($attribute,$name,$value)
    {
      $script =  '$(\'select['.$attribute.'="'.$name.'"]\').next().find(\'ul\').find(\'input:radio[value="'.$value.'"]\').trigger(\'click\');';

       $this->execute(array(
            'script' => $script,
            'args'   => array()
        ));
    }

I really dont know if is the best solution, but gets the job done fast.

Now the only thing left to do is just write your own test

Just create a new test class in your Acceptance folder and extend the test loader

<?php
include_once '..\TestLoader.php';

class OpeningABCTest  extends TestLoader
{
    /**
     * @test
     */
    public function it_should_open_menu() {

        dump("Opening RFC Menu");
        $this->login() ;
        $this->visit('es/abc/menu2');  
        $this->seePageIs('/es/abc/menu2');     
        dump('OK');
    }

I still have some issues to start selenium with the start_selenium function so you will have to do it manually, just type "java -jar selenium-server-standalone-2.52.0.jar &"

And just like before, run phpunit in your console, and hope for GREEN!

6 likes
Tetsajwjg's avatar

@cabrerabywaters your directions are my lifesaver! it worked almost perfect. but one thing that I couldn`t make was starting phantomjs.

I had got the error below.

PHPUnit_Extensions_Selenium2TestCase_WebDriverException: The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded from http://phantomjs.org/download.html

To remove this error, I had to install phantomjs through npm instead of the way you showed.

npm install -g phantomjs

After installing phantomjs, I changed a line of your code like below.

shell_exec("phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444");

This worked on my homestead.

Thank you!

1 like
cabrerabywaters's avatar

@Tetsuya Suzuki Glad it worked for you! I think that installing it through NPM its a good way to avoid downloading it from the Official page. I'm pretty sure that you can do the same for selenium if you want to use it differently.

npm install selenium-webdriver

But for stand-alone version you will still need to download the JAR file.

If you get to fix the

  protected function run_selenium_server()
    {
        if($this->selenium_server_already_running())
        {
            fwrite(STDOUT, "Selenium server already running\n");
        }
        else
        {
            shell_exec("java -jar " . __DIR__ . "..\bin\selenium-server-standalone-2.52.0.jar");
        }
    }

Please let me know =)

willvincent's avatar

I haven't tried it yet, but one of my coworkers pointed me at the Laravel TestTools chrome extension earlier today... looks pretty awesome.

2 likes
cabrerabywaters's avatar

@willvincent Haven't use the Laravel Testools extension, but looks very useful! Looks like it uses Codeception to create the tests. As I used the same names in the functions for Selenium, you probably can import the same tests generated by this extension to your Selenium tests. I'm going to give it a try! Than you!

shhetri's avatar

@cabrerabywaters Your solution looks pretty good to me. I'll give it a try the next time I have to write acceptance tests.

MarceloG's avatar

@cabrerabywaters just like i mentioned, i trying with your approach, for now i have fine results. Nexts steps for me are driving user authentication.

kodemania's avatar

Hey everyone,

I used it as it is stated and now all seleniums tests are getting skipped.

MarceloG's avatar

Are you a selenium server standalone running when you excecute the tests suit?

Please or to participate in this conversation.