Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to PHPUnit and Selenium - AAPUG

Jacob Mather
December 02, 2012

Introduction to PHPUnit and Selenium - AAPUG

This talk gives a basic overview of the different types of tests and tools out there, and then goes into specific detail of writing your first Selenium test in PHPUnit.

For more information, go to: http://jmather.com/talks/2012-12-01/

Jacob Mather

December 02, 2012
Tweet

More Decks by Jacob Mather

Other Decks in Programming

Transcript

  1. Intro  to  PHP  Integra,on  Tes,ng  
    A  rapid  guide  to  ge4ng  started  with  
    Selenium  and  PHPUnit  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  2. Why  We  Don’t  Test  
    Even  though  we  know  we  should…  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  3. Why  We  Don’t  Test  
    •  We  don’t  know  about  the  tools  
    •  We  don’t  understand  the  philosophy  
    •  We  don’t  think  we  have  the  ,me  
     
    Jacob  Mather  
    @thejmather  

    View full-size slide

  4. Why  We  Don’t  Test  
    •  We  don’t  know  about  the  tools  
    •  We  don’t  understand  the  philosophy  
    •  We  don’t  think  we  have  the  ,me  
     
    Jacob  Mather  
    @thejmather  

    View full-size slide

  5. Why  We  Don’t  Test  
    •  We  don’t  know  about  the  tools  
    •  We  don’t  understand  the  philosophy  
    •  We  don’t  think  we  have  the  ,me  
     
    Jacob  Mather  
    @thejmather  

    View full-size slide

  6. Why  We  Don’t  Test  
    •  We  don’t  know  about  the  tools  
    •  We  don’t  understand  the  philosophy  
    •  We  don’t  think  we  have  the  ,me  
     
    Jacob  Mather  
    @thejmather  

    View full-size slide

  7. Why  We  Don’t  Test  
    We  don’t  know  how.  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  8. Ge4ng  Started  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  9. Picking  A  Test  Type  
     
    Unit  Tests  
    Integra,on  Tests  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  10. Picking  A  Test  Type  
     
    Unit  Tests  
    Integra,on  Tests  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  11. Picking  A  Test  Type  
     
    Unit  Tests  
    Integra,on  Tests  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  12. Picking  A  Test  Tool  
     
    phpspec2  
    Behat  
    PHPUnit  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  13. Picking  A  Test  Tool  
     
    phpspec2  
    Behat  
    PHPUnit  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  14. Picking  A  Test  Tool  
     
    phpspec2  
    Behat  
    PHPUnit  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  15. Picking  A  Test  Tool  
     
    phpspec2  
    Behat  
    PHPUnit  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  16. Test  Types  &  Tools  
    Unit  Tests   Func,onal  Tests  
    phpspec2   X   X  
    Behat   X   X  
    PHPUnit   X   X  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  17. Installing  PHPUnit  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  18. Installing  PHPUnit  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  19. Installing  PHPUnit  
    File:  composer.json  
    {  
     “require-­‐dev”:  {  
       “phpunit/phpunit”:  “3.7.*”  
     },  
     “config”:  {  
       “bin-­‐dir”:  “bin”  
     }  
    }  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  20. Installing  PHPUnit  +  Selenium  Tools  
    File:  composer.json  
    {  
     “require-­‐dev”:  {  
       “phpunit/phpunit-­‐selenium”:  “*”  
     },  
     “config”:  {  
       “bin-­‐dir”:  “bin”  
     }  
    }  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  21. Installing  PHPUnit  +  Selenium  Tools  

    View full-size slide

  22. Installing  Selenium  Server  
    For  more  informa,on,  visit:  
    h_p://seleniumhq.org/download/  

    View full-size slide

  23. Configuring  PHPUnit  
    File:  phpunit.xml.dist  
       colors="true"  
       bootstrap="vendor/autoload.php">  
     
         
             
               tests/  
             
         
     
    Jacob  Mather  
    @thejmather  

    View full-size slide

  24. Pre-­‐Flight  Check  
    Jacob  Mather  
    @thejmather  

    View full-size slide

  25. Our  First  Test  
    File:  tests/ExampleTest.php  
     
    class  ExampleTest  extends  PHPUnit_Extensions_Selenium2TestCase  
    {  
    }  
     
     
    Jacob  Mather  
    @thejmather  

    View full-size slide

  26. Our  First  Test  
    File:  tests/ExampleTest.php  
     
    class  ExampleTest  extends  PHPUnit_Extensions_Selenium2TestCase  
    {  
           public  func,on  setUp()  
           {  
                   $this-­‐>setBrowser('*firefox');  
                   $this-­‐>setBrowserUrl('h_p://jmather.com/');  
           }  
    }  
     
      Jacob  Mather  
    @thejmather  

    View full-size slide

  27. Our  First  Test  
    File:  tests/ExampleTest.php  
     
    class  ExampleTest  extends  PHPUnit_Extensions_Selenium2TestCase  
    {  
           public  func,on  setUp()  {  /*  …  */  }  
     
           public  func,on  testTitle()  
           {  
                   $this-­‐>url('/');  
     $,tle  =  “It’s  Majax”;  
                   $this-­‐>assertEquals($,tle,  $this-­‐>,tle());  
           }  
    }  
     
     
    Jacob  Mather  
    @thejmather  

    View full-size slide

  28. Our  First  Test  
    File:  tests/ExampleTest.php  
     
    class  ExampleTest  extends  PHPUnit_Extensions_Selenium2TestCase  
    {  
           public  func,on  setUp()  {  /*  …  */  }  
           public  func,on  testTitle()  {  /*  …  */  }  
     
           public  func,on  testSomeNaviga,on()  
           {  
                   $this-­‐>url('/');  
                   $this-­‐>byXPath('//div[@class=”widget_categories"]/a[text()  =  ”PHP"]')-­‐>click();  
                   $,tle  =  “PHP|  It’s  Majax”;  
                   $this-­‐>assertEquals($,tle,  $this-­‐>,tle());  
           }  
    }  
     
      Jacob  Mather  
    @thejmather  

    View full-size slide

  29. Our  First  Test  
    File:  tests/ExampleTest.php  
     
    class  ExampleTest  extends  PHPUnit_Extensions_Selenium2TestCase  
    {  
           public  $browser  =  '*firefox';  
     
           public  func,on  setUp()  
           {  
                   $this-­‐>setBrowser($this-­‐>browser);  
                   $this-­‐>setBrowserUrl('h_p://jmather.com/');  
           }  
     
           public  func,on  testTitle()  {  /*  …  */  }  
           public  func,on  testSomeNaviga,on()  {  /*  …  */  }  
    }  
     

    View full-size slide

  30. File:  tests/ExampleTest.php  
     
    class  ExampleTest  extends  PHPUnit_Extensions_Selenium2TestCase  
    {  
           public  $browser  =  '*firefox';  
     
           public  func,on  setUp()  
           {  
                   $this-­‐>setBrowser($this-­‐>browser);  
                   $this-­‐>setBrowserUrl('h_p://jmather.com/');  
           }  
     
           public  func,on  testTitle()  
           {  
                   $this-­‐>url('/');  
                   $,tle  =  “PHP|  It’s  Majax”;  
                   $this-­‐>assertEquals($,tle,  $this-­‐>,tle());  
           }  
     
           public  func,on  testSomeNaviga,on()  
           {  
                   $this-­‐>url('/');  
                   $this-­‐>byXPath('//div[@class=”widget_categories"]/a[text()  =  ”PHP"]')-­‐>click();  
                   $,tle  =  “PHP|  It’s  Majax”;  
                   $this-­‐>assertEquals($,tle,  $this-­‐>,tle());  
           }  
    }   Jacob  Mather  
    @thejmather  

    View full-size slide

  31. Our  Second  Test  
    File:  tests/ChromeExampleTest.php  
     
    class  ChromeExampleTest  extends  ExampleTest    
    {  
           public  $browser  =  '*chrome';  
    }  
     
     
    Jacob  Mather  
    @thejmather  

    View full-size slide

  32. Our  Second  Test  
    File:  tests/ChromeExampleTest.php  
     
    class  ChromeExampleTest  extends  ExampleTest    
    {  
           public  $browser  =  '*chrome';  
     
           //  inherited  
           public  func,on  setUp()  
           {  
                   $this-­‐>setBrowser($this-­‐>browser);  
                   $this-­‐>setBrowserUrl('h_p://jmather.com/');  
           }  
    }  
     
     
    Jacob  Mather  
    @thejmather  

    View full-size slide

  33. Jacob  Mather  
    @thejmather  
     
    h_p://jmather.com/talks/2012-­‐12-­‐01/  
    h_ps://github.com/jmather/phpunit-­‐selenium-­‐first-­‐test  

    View full-size slide