All posts by GabyMG

Behavior-Driven Development on Laravel Homestead with Behat and Selenium

Oh, my!… I do love the PHP Laravel Framework very, very much!

And one of the things I love most about it is Laravel Homestead, an official, pre-packaged Vagrant “box” that provides a powerful and flexible PHP development environment and bundles a very useful set of software.

However, you can’t please everybody, and in order to do Behavior-Driven Development (BDD) on applications that make use of JavaScript, like I extensively do, you’ll need to do some adjustments to your Homestead installation.

In this article I’ll show you how to make Homestead work smoothly with:

  • Behat, a BDD framework for PHP
  • Selenium WebDriver

Step 1: Enable X11 Forwarding over SSH

The first thing we’re going to do is to enable X11 forwarding over SSH, so we can share the windowing system between the virtual machine and the host.

The only way I know how to do this is by modifying Homestead’s Vagrantfile. Please, feel free to leave a comment if you know a better solution.


config.ssh.forward_x11 = true

Step 2: Install Java, Firefox and xvfb

Next, we’re going to add some necessary software to our virtual machine. For this, Homestead provides a provisioning script, ~/.homestead/after.sh, that we’re going to edit:

#!/bin/sh

# Update app-get
apt-get update

# Install Java, Firefox, and Xvfb
apt-get install -y openjdk-7-jre firefox xvfb

Above we are installing:

  1. Java SE Runtime Edition (JRE), which is required by Selenium,
  2. Firefox
  3. xvfb, to run applications that need a display inside our virtual machine

Step 3: Install Selenium

Then, add these two lines to the script after.sh in order to install Selenium

npm install -g selenium-standalone
selenium-standalone install

We’re using Vincent Voyer’s selenium-standalone NPM package in order to install Selenium WebDriver. As you can see in the package documentation page, we can run selenium headlessly with xvfb with the following command:

xvfb-run --server-args="-screen 0, 1366x768x24" selenium-standalone start

For easier initialization, you can add something like this to your ~/.homestead/aliases file:

alias selenium-start="xvfb-run --server-args=\"-screen 0, 1366x768x24\" selenium-standalone start"

Step 4: Add Behat, Mink and the Selenium2Driver

Finally, execute the following command from the root of your project in order to add Behat, Mink and the Selenium2Driver to your composer.json file and install the necessary packages.

composer require behat/behat behat/mink behat/mink-extension behat/mink-selenium2-driver --dev

Step 5: Run your machine and go grab a tea!

Now you can run your virtual machine with

vagrant up

Be ready to wait for a few minutes so, don’t waste your time and go grab a cup of coffee / tea…

When you come back, it will be ready for you to login to your virtual machine and start Selenium


vagrant ssh
selenium-start

Then open a new tab on your terminal and login again to your VM. Navigate to your project and run your tests!!

Final notes

In this tutorial, I’ve shown you how to customize your Laravel Homestead machine in order to test applications with Behat and Selenium. However, I haven’t included instructions on how to configure Behat for a particular project or framework because Homestead allows you to organize all your PHP projects on the same VM, and I wanted to keep this article somewhat general.

You can get my complete after.sh provisioning script on this Gist. At this moment, I’m working on two things to improve it:

  • Install Composer packages globally instead of locally, as I use this programs in almost all of my projects.
  • Remove output from provisioning script and add some progress messages
  • Test other web browsers (Chrome, Chromium, PhantomJS)

Please, if you encounter any problem following this tutorial, feel free to add a comment below and I’ll do my best to help you solve it. Also, if you have any suggestions on how to improve this setup, feel free to leave a comment here or on the Gist page.

References

Decrypt PDF files through the command line interface

Have a bunch of password protected PDF files and you’re tired to fill in the credentials each time you want to read any of them?

I recently had a similar issue when one of my clients asked me to add a new page to their website with the brochures of all their products that were, password protected, on their provider site. Although still required to have some kind of protection on them, I thought It would be better to decrypt them and add a single password to the container web page instead.

Keep reading and I’ll show you, step by step, how to decrypt password protected PDF files through the use of the command line interface as well as a few other tricks you can use in the future to solve similar problems.

Step 1: Find the right tool

The first step is finding the right app for the task at hand. We could certainly search for it using our web browser, but I want to point you a few other options available on Linux systems:

Using the man command

The man command is an interface to the online software documentation or manual pages (man-pages) of Linux systems. It’s an indispensable tool for beginners and experts, surely one of my most used shell commands…

Similarly to what we would do with our web browser, with man we can also search for keywords and we’ll also get a list of paged results.

In order to find all available programs with PDF capabilities we can change man mode of operation so it’ll search the short manual page descriptions for our keywords and display any matches. This is done through the use of the -k or --apropos option.

man -k pdf
man --apropos pdf

man-k_pdf_results

The apropos long option takes its name from the English word with the same spelling (and the same pronunciation), which means relevant. It’s also the name of another Linux command that we can use for searching commands without knowing their exact names.

Using the apropos command

apropos searches the manual page names and descriptions for the specified keywords and also prints any matches. So if we do:

apropos pdf

we’ll find the exact same set of results that before

apropos_pdf_results

This is the command that I normally use because, in my opinion, it’s the most semantic, but we have even another option to retrieve the same results.

Using the whatis command

whatis also searches the manual page names and displays the manual page descriptions of any keywords matched.

whatis -r pdf

will also print the same results. Try it by yourself!

The last result in our list seems promising:

qpdf (1)             - PDF transformation software

If we retrieve its manual page by doing:

man qpdf

we can see that:

It is capable of performing a variety of transformations such as linearization (also known as web optimization or fast web viewing), encryption, and decryption of PDF files.

Bingo!. Now we can continue with the second step, installing qpdf…

Step 2: Install QPDF

QPDF is a lightweight command-line program that does structural, content-preserving transformations on PDF files. As written in the QPDF Manual: It could have been called something like pdf-to-pdf.

QPDF’s website is located at http://qpdf.sourceforge.net/ and its source code is hosted on GitHub at https://github.com/qpdf/qpdf.

Installing QPDF on any Debian GNU/Linux based system is as simple as:

sudo apt-get install qpdf

Step 3: Decrypt the files

Once we have installed QPDF on our system, the last step is to decrypt our passwod protected PDF files.

If we run the qpdf --help command, we can see two basics options that are just what we’ve been looking for:

--password=password
Specifies a password for accessing encrypted files.
--decrypt
Removes any encryption on the file. A password must be supplied if the file is password protected.

So, If we want to decrypt a single password protected PDF file we would do:

qpdf --password=your-password-here --decrypt input-file output-file

But, what about a folder of PDF files?… That was our original task, remember?.

find . -iname "*.pdf" -exec qpdf --password=your-user-password --decrypt {} {} \;

Building my site – Day Zero

I’ve just finished installing WordPress on my server, and I wanted to document the process as I build my site for future reference.  There is no much to look at, I know, but I promise that it’s going to change very fast. For now this is it:

  • The ‘Projects’ page is just a couple links to some of the web pages that I’ve built.  First thing tomorrow is to make a little plugin to handle a ‘Project’ custom post type.
  • The ‘About’ page is little more than the great, ‘Contact Form 7‘ plugin, by Takayuki Miyoshi. Aboslutely nothing to complain about it, in fact I believe it’s one of the best plugins available out there.

Welcome To My Site!

Hello! welcome to my site. Although you may not think so, for me, it has been quite overwhelming to write this post. As a first born myself I know that being so, one usually comes with a lot of hope and also with quite a few expectations.

But, which expectations can we have on a first post written in 2013? It may be very possible that everything that can be said has already been said before.

So, I just want to say two things: I hope that you will find  some of the information on this blog useful, and that I look forward to learn each day from your comments.