How to Use Selenium 2 With PHPUnit
videos

How to Use Selenium 2 With PHPUnit

Tutorial Details
  • Difficulty: Intermediate
  • Completion Time: 30 Minutes
This entry is part 9 of 12 in the Test-Driven PHP Session
« PreviousNext »

In this lesson, we will learn how to work with Selenium 2 directly within PHPUnit. For those unfamiliar, Selenium gives us an easy way to automate the browser. This makes it perfect for writing user acceptances tests.

Choose 720p for the clearest picture.

Show Links


Closing Notes

I’m relatively new to working with Selenium, so, if any of you have some tips or advice related to improving workflow, definitely chime in below! I’m currently researching the best way to speed up the tests, or, at the very least, hide the browser during tests. Have you found a good way to deal with this? Virtual machines?

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://erusev.com/ Emanuil

    Excellent tutorial.

  • Agdm

    This is exactly what I have been looking for. Going to check Selenium out tonight

  • Jesus Bejarano

    I love the way that nettuts is trying promote testing to the PHP cummunity, but seriusly another php tut and i am gonna change my nettuts bookmark to phptuts, no joke XD.

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Well – Nettuts+ has been rooted in PHP and JavaScript tuts since day one. We certainly cover other languages/frameworks, but only about 25% worth.

  • ian

    Thank you, I was hoping there would be more of your video tuts in the phpunit session. The written ones are good and informative but blah blah :-)

    Firefox is super slow on Windows and Linux for me. Just watching the video I see it’s way faster on your Mac. I did get selenium working with swiftfox on Ubuntu once but it’s really outdated last time I checked, and have got Chrome working on Windows using chromedriver.exe http://code.google.com/p/chromedriver.

    I was wondering if you or anyone knows of a good stripped down browser made just for running selenium.

    Thanks again, great tutorial as always

    • http://brianscaturro.com brian

      For speed you would want a headless browser, maybe checkout Mink. This uses Goutte to do it’s headless testing. The upside is speed and simplicity; the downside is you can’t test rich interfaces (no js/ajax).

      I think Selenium’s greatest benefit is being able to test actual browsers. If you have a well designed test suite, it is easy to extend a base class or mix in a trait to run the same gamut of tests against every browser/platform. Great for acceptance testing.

      • ian

        Thanks Brian,

        I was thinking just in terms of testing the basics like we did in the video but cross browser testing does make good sense. Especially with js dependent interfaces since that is where a test might pass for one browser and fail in another due to cross browser compatibility issues.

        I’ll check out Mink and Goutte and also look into setting up that base class for all the browsers as you suggest. Sounds good to me :-)

      • scaturr

        I recently made a tool to run PHPUnit tests in parallel, which can have a significant impact on speed. So far only tested on mac and linux. https://github.com/brianium/paratest with an example specifically related to selenium at https://github.com/brianium/paratest-selenium

      • http://www.jeffrey-way.com Jeffrey Way
        Author

        Thanks, Brian!

  • srigi

    Amazing screencast! Sharing on the Twitter right-away.

    Jeffrey can you write quick sum of plugins/KB-shortcuts for ST which enhance your coding? I’m mostly interested in
    - html template
    - function body snippet
    - deleting function arguments by command

    Thanks.

  • s7anley

    Thank you for great screencast. Finally tutorial which show how to test application, no only examples like 1 + 1 should be 2 ;-)

  • http://resume.doobdargent.com dOob d’Argent

    I always thought building test cases would slow the development of my project a lot. But from what I can see, it’s not that big of a deal and it can save a lot of time later. And with Selenium we can test JS, I think I’ll use it for my next projects. Cheers!

  • http://www.samarpanda.com Samar

    This is exactly what i was looking for. Thank you very much.

  • Alex

    This is good for QA team

  • https://www.evernote.com/shard/s223/sh/a505cf17-902f-4451-9fc7-e9c5a92232d7/8499a4a1fb6758d9dec5e2c5b81417a1 jonwhittlestone

    Hi Jeffrey,

    Running Selenium server and PHPUnit on MAMP, I am unable to get past the stage of executing the first test (6m 10s). I get the error

    Argument 1 passed to PHPUnit_Extensions_Selenium2TestCase_Element::fromResponseValue() must be of the type array, null given, called in /Applications/MAMP/bin/php/php5.4.4/lib/php/PHPUnit/Extensions/Selenium2TestCase/Session.php on line 257

    Any ideas?

    ps. love your site and make sure that I get round to watching your screencasts. I even went out and bought a mac so as to more closely follow. Thanks for all your work and I’m sorry for the direct approach, Googling has yielded nothing..

    • jonwhittlestone

      Oh I just figured it out for anyone reading this – I hadn’t been running Selenium server.

      School boy error!

  • http://sethmohit.com Mohit Seth

    A nice tutorial for a beginner in testing. Selenium is very powerful tool.

  • Asher

    Another great tutorial Jeffrey!

    I’m wondering if you can direct me to a resource that will explain how to decide what should be covered in testing and what shouldn’t. It seems like extensive testing could chew up a huge amount of development time and may not be necessary, but too little testing would obviously not be sufficient to ensure quality.

    How do we decide where to draw the line?

    Additionally, how much overlap do you get when using a JavaScript test suite and something like Selenium which tests JavaScript, but from a user/browser perspective rather than from a function perspective?

  • answer

    good article

  • http://www.facebook.com/susanbuck Susan Buck

    Great post. Summarized the step-by-step I took while following the screencast, including some extra details needed to get PHPUnit up and running at the start: http://codagogy.com/blog/2012/12/15/programs-to-check-your-programs-phpunit-and-selenium Looking forward to checking out your other PHP testing tuts.

  • christie

    Nice Tutorial!! I am newbie and need some help, please!

    I have an error when execute the file, which is:

    $ phpunit testLogin.php
    PHP Fatal error: Class ‘PHPUnit_Extensions_SeleniumTestCase’ not found in /home/user/testLogin.php on line 3

    In my PHPUnit folder the above class exists.

    How can I execute it correctly???

    Thank you in advance.

  • Oleg

    Hi. Very nice tutorial. But can you also add some information about how run tests in different browsers. By default all test launch in Firefox, but how can I launch it on Chrome for example, or in IE

  • http://www.facebook.com/Lord.McBradlingtonfordshaw Kevin Bradshaw

    Hey great tutorial, I feel like I am missing something really obvious here. Where can I find a list of commands for phpunit with selenium 2? I have set up everthing on my machine and I can follow your example just fine, however this is the only good tut I have found on phpunit with selenium 2. I cannot find any documentation on the basic commands such as byName, byId etc.. I can follow this example fine, but when I try and do anything not covered I here I cant find any documentation anywhere,,, any ideas?

  • Evan Rolfe

    What a great introduction to selenium! Perfect for me having just managed to get it working with phpunit and fuelphp framework.

    @5800ab190a877b8ff9fb8fd39cd25fbf:disqus What is the sublime plugin that lets does the html autocompletion for you?

  • Robert Kraig

    Very useful