Laravel and Selenium with Chrome using Codeception

tnylea

Sep 7th, 2016 09:02 PM

So, I've been trying for the longest time to get Laravel Tests to work with Selenium and Chrome. I tried checking out the Laravel Integrated package and in the repo it says that much of the Laravel Integrated package has been included in Laravel core:

https://github.com/laracasts/Integrated

But I have been unsuccessful on integrating Selenium with my tests. So instead I went with Codeception. http://codeception.com

Codeception has some amazing documentation and they even have a section that describes how to install it for laravel: http://codeception.com/for/laravel

So, here's what I did after going through the quick start guide.

  1. Create a simple test file
  2. Download selenium and chromedriver
  3. Start selenium with chromedriver
  4. Run our test

 

1. Create a simple test file

I created a new file located in 'tests/acceptance/HomeRouteCept.php', which contained the following small bit of code:

<?php
<p>$I = new AcceptanceTester($scenario);
$I->wantTo('Testing out the homepage route');
$I->amOnPage('/');

2. Download selenium and chromedriver

Next we need to download the Selenium Server file located at: http://www.seleniumhq.org/download/

Then, you will also need to download the chromedriver located at: http://chromedriver.storage.googleapis.com/index.html (choose the latest folder and download the driver)

3. Start selenium with chromedriver

In order to start selenium we would typically write the following command:

java -jar selenium-server.jar

But if you get a command that it does not recognize the 'java' command you will need to download java from https://www.java.com/en/download/, you may also need to add it to your PATH file, which is explained here http://apple.stackexchange.com/questions/165022/problem-with-installing-java-java-command-not-found

After it recognizes the command we should be able run the following command to start Selenium Server:

java -jar selenium-server.jar

But that's not all.

We also want to be able to run it with the chromedriver so we will use the following command instead:

java -Dwebdriver.chrome.driver=./chromedriver -jar selenium-server.jar

And now that the server is running. Let's run our test.

4. Run our test

Next, navigate to your terminal and run the following command inside of your laravel app:

vendor/bin/codecept run

And low and behold, you'll see chrome open up and open your home route. Then inside of your terminal you will get a green pass message that your tests have run successful!

Now I can get a real time play of what my tests are doing. Now, you will probably want to use a headless browser like phantonjs when running these tests with continuous deployment apps and you could even run Phantomjs browser on your machine when you don't want it to open up in chrome.

But, if there is a way to get Laravel tests running with chrome and selenium, please feel free to post your solution below.

Thanks!

josh-peckar

Sep 24th, 2018 08:54 AM

This has been one of the greatest post to read here never seen such important post which could be an all important detail about the coding work which is different Custom Paper Writing Company have been providing a proper sources and guide tha how to deal with things like these which is an all important and practical thing to get through here which is an all important detail to read.

chris-silva

Jun 27th, 2019 01:25 PM

Despite the fact that it's illegal to build with asbestos in London (or anywhere else in the UK for that matter) today, many older container houses still contain large quantities of asbestos on and in everything from cement, to toilet cisterns, to heater cupboards, and even in wall panelling. This means that if you try to perform certain home repairs yourself, such as a window replacement, you risk exposure to hazardous waste. But this article will help clear some things up.