How to do Selenium with PHPUnit on Sauce

So I heard you have a PHP website you want to test. I guess you already heard that Selenium is the way to test a website, and that Sauce OnDemand is the way to use Selenium. Since you want to do the right thing and use Sauce, I have two (2) pieces of good news for you:

  1. I wrote this post on how to use Sauce and PHP to test your website
  2. You're reading it

Okay, PHP has this cool thing called pear that you can use to download useful php scripts. We'll spend pretty much this whole blog post getting pear, then using it to get the Sauce OnDemand pear package and all of the other pear packages it depends on. Things to know about pear:

  • You download pear packages from channels.
  • Most of the time, when you need a package, you first have to tell pear about the channel. Pear calls this "discovering" the channel.
  • If a package depends on another package, it will tell you about the other package you need, but it won't automatically go get it for you.
  • There's a new version of pear called Pyrus and we aren't going to use it

Okay. So, before we can use pear, we have to go get it.

Get Pear

This part depends on what operating system you're using. Below are the easiest ways I've found to install pear for each OS. If they don't work for you, you can always follow the instructions on the pear website.

Mac

Get mac ports. Then: $sudo port install php5 +pear This might complain that you don't have all of the stuff php5 needs to install. If it does, it should also tell you what you need to do to wrap that up.

Linux

$ sudo apt-get update $ sudo apt-get install php-pear

Windows

There's a php installer here you can use to reinstall php. One of the steps (shown here) lets you install pear along with php. That's all you need to do.

Now that you have pear

The rest of your setup happens from the command line. If you're a linux or mac person, you're already there. Windows people, push windows key+R, type "cmd", and push enter; when you see a black window with "C:\>" in it, you're at a command prompt and you're ready to proceed. First, upgrade pear:

  • pear upgrade pear

Next, discover and update all the channels we'll need to install pear packages from:

  • pear channel-discover pear.phpunit.de
  • pear channel-update pear.phpunit.de
  • pear channel-discover components.ez.no
  • pear channel-update components.ez.no
  • pear channel-discover pear.symfony-project.com
  • pear channel-update pear.symfony-project.com
  • pear channel-discover saucelabs.github.com/pear
  • pear channel-update saucelabs.github.com/pear

Now, finally, install phpunit for Sauce OnDemand:

  • pear install -a saucelabs/PHPUnit_Selenium_SauceOnDemand

Last, configure PHPUnit_Selenium_SauceOnDemand to use your Sauce Labs account and access key (which you can find at your account page):

  • sauce configure <account name> <access key>

If you don't have an account name and access key, then it's time to sign up for Sauce OnDemand!

You're ready to cook with hot sauce

You get to run tests with hot Sauce! Here's an example script, and how to run it. Put the following PHP into a file named ExampleTest.php:

<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase/SauceOnDemandTestCase.php';

class ExampleTest extends PHPUnit_Extensions_SeleniumTestCase_SauceOnDemandTestCase {

    function setUp() {
        $this->setOs('Windows 2003');
        $this->setBrowser('firefox');
        $this->setBrowserVersion('3.6.');
        $this->setBrowserUrl('http://example.saucelabs.com');
    }

    function test_example() {
        $this->open('/');
        $this->assertTitle('Cross browser testing with Selenium - Sauce Labs');
    }
}

Notice that the filename, ExampleTest.php, is the same as the name of the test class. Get excited, because here's where we finally use Sauce OnDemand:

  • phpunit ExampleTest.php

Congratulations! You ran a job with Sauce OnDemand. Go to your jobs page to see it. From here, you can edit the example test to open your own website instead of example.saucelabs.com, and add clicks and assertions to your brain's content.

Written by

The Sauce Labs Team

Topics

Unit TestingSelenium