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 ...
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??
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!
Please or to participate in this conversation.