Testing Java Web Apps With Selenium

  • Published on

  • View

  • Download


San Francisco Java User Group presents Chris Bedford who talks about:- How to write functional tests with Selenium (including explaining its IDE, architecture, RC, and alternatives like Canoo WebTest)- How to set up Selenium testing for web apps in continuous integration using Maven, Ant, Cargo, etc.- How to use Hudson for build server (brief overview) January 12, 2010 in San Francisco, CAhttp://www.sfjava.org/calendar/11982857/Hosted by SUPINFO International UniversitySponsored by TEKsystems, Guidewire Software, Sun, O'Reilly, JetBrains, and Marakana.Video by Max WalkerOrganized by Marakana


<ul><li> 1. - Selenium - Browser-Based Automated Testing of Web Apps Under Continuous Integration<ul><li>Presented By </li></ul></li></ul> <ul><li>Chris Bedford </li></ul> <ul><li>Founder &amp; Lackey at Large </li></ul> <ul><li>Build Lackey Labs </li></ul> <ul><li>Company Overview</li></ul> <ul><li><ul><li>Founded2008 </li></ul></li></ul> <ul><li><ul><li>Services </li></ul></li></ul> <ul><li><ul><li>Release engineering&amp;Build/Test Automation for Java Environments </li></ul></li></ul> <ul><li><ul><li>Spring/Hibernate &amp; Groovy/Grails App Development </li></ul></li></ul> <ul><li><ul><li>Training:Spring/Groovy/Grails &amp; Build/Test Automation Technologies </li></ul></li></ul> <ul><li><ul><li>Employees</li></ul></li></ul> <ul><li><ul><li>Chris Bedford </li></ul></li></ul> <ul><li><ul><li>Mike Jackson </li></ul></li></ul> <p> 2. Agenda Different Ways To Test a Grails App Unit, Integration, Functional Selenium Demo of IDE Architecture Alternatives (Canoo WebTest) Automating Testing Using Selenium Maven DemoCargoViewing Test Results Lab 1 maven project Ant Continuous Integration With Hudson How to write functional tests with Selenium KEY TAKE-AWAYS How to set up Selenium TestingOn Grails AppsIn ContinuousIntegration BuildingWith Ant Building with maven ContinuousIntegration With Hudson 3. </p> <ul><li>Unit </li></ul> <ul><li>Unit Tests are created and run developer and run using framework like Junit or TestNG </li></ul> <ul><li>test class or package functionality in isolation </li></ul> <ul><li>heavy use of mocks and stubs</li></ul> <ul><li>tests should execute quickly and be run often to catch problems early on </li></ul> <ul><li>Integration </li></ul> <ul><li>If you are dependent on someone elses stuff, the test includes your stuff and theirs </li></ul> <ul><li>Verifies that two or more components work together,</li></ul> <ul><li><ul><li>usually directly, and not with mocks </li></ul></li></ul> <ul><li>To simplify test set-up components typically execute same JVM process </li></ul> <ul><li>Functional Testing </li></ul> <ul><li>Run application in container</li></ul> <ul><li>Exercise functionality via a client side agent that executes Javascript. </li></ul> <ul><li><ul><li>Agent could be either: </li></ul></li></ul> <ul><li><ul><li>your actual target browser (Firefox, IE, etc.)[Selenium's approach] </li></ul></li></ul> <ul><li><ul><li>a Javascript engine embedded into test framework[Canoo Webtest's approach] </li></ul></li></ul> <p>Different Ways To Test a Java Web App Real http requests Separate client Mock http requests Client requests from same process Package or class level scope increasinglycoarsegrainedcomponentsundertest 4. Manual Steps Involved In Running Selenium </p> <ul><li>Before running Selenium Functional Tests we need to</li></ul> <ul><li><ul><li>Compile classes </li></ul></li></ul> <ul><li><ul><li>Run unit and integration tests</li></ul></li></ul> <ul><li><ul><li><ul><li>bail on functional tests if we catch problems with lighter weight tests </li></ul></li></ul></li></ul> <ul><li><ul><li>Package .war file </li></ul></li></ul> <ul><li><ul><li>Install and start the container in which we want to run the .war</li></ul></li></ul> <ul><li><ul><li>Deploy the .war to the container</li></ul></li></ul> <ul><li><ul><li><ul><li>unless your running Jetty, which runs embedded in your app </li></ul></li></ul></li></ul> <ul><li><ul><li>Launch target browser </li></ul></li></ul> <ul><li><ul><li>Launch Selenium on desired test suite </li></ul></li></ul> <p> 5. Launching Selenium 6. Recording New Selenium Tests 7. Exporting test commands to 3GL (Java, etc.) 8. Individual tests referenced by the suite are recorded using their paths relative to the suite. For simplicity put your suite and all tests in the same directory (to start) Saving New or Modified Selenium Tests 9. Selenium Components </p> <ul><li><ul><li>Selenium IDE </li></ul></li></ul> <ul><li><ul><li>Selenese Commands </li></ul></li></ul> <ul><li><ul><li><ul><li>Enable test author to</li></ul></li></ul></li></ul> <ul><li><ul><li><ul><li><ul><li>simulate navigation, clicks </li></ul></li></ul></li></ul></li></ul> <ul><li><ul><li><ul><li><ul><li>Make assertions about expected responses </li></ul></li></ul></li></ul></li></ul> <ul><li><ul><li>Selenium RC </li></ul></li></ul> <ul><li><ul><li>Client side library that enables you to program more sophistication into your tests than the IDE allows(conditions, looping, error handling) </li></ul></li></ul> <ul><li><ul><li>The Selenium Server which launches and kills browsers, interprets and runs the Selenese commands passed from the test program, and acts as anHTTP proxy ,</li></ul></li></ul> <p> 10. Selenium Client Side Library &amp; Server In Action HTTP Selenium-server.jar Source: http://seleniumhq.org/docs/05_selenium_rc.html Reports backresults of test to client HTTP (javascript) (client side java script / Ajax portion of application under Test originates fromhere ) Applicationunder test (server side) 11. A Look Same Origin Policy (Which Seleniums Architecture Circumvents) Same origin policy: Forbids JavaScript code running on aweb pagefrom interacting with resources which originate from any web site other than the one which served up thatweb page 12. Selenium RC Server Acting As Proxy To Avoid Same Origin Policy Restrictions What happens when a test suite starts ? 1) client/driver establishesconnection w.selenium-RC2)Selenium-RC server launches a browser (or reuses an old one) with URL that injects Selenium-Cores javascript into browser-loaded web page. 3) client-driver passes a Selenese command to the server e.g.: open command 4) Server interprets the command and then triggers the corresponding javascript execution to execute that command within the browser (say open page in app under test)s 5) Request to open the page is routed through proxy server 6) 7) Proxy forwards request to appserver App server returns response 13. Functional Test Alternatives: Canoo vs Selenium </p> <ul><li>Canoo Web Test </li></ul> <ul><li>built on HtmlUnit </li></ul> <ul><li>pros: </li></ul> <ul><li><ul><li>excellent test reporting allows you to pin point errors in test very easily. </li></ul></li></ul> <ul><li><ul><li>faster to run (no browser spin up overhead) </li></ul></li></ul> <ul><li><ul><li>better support for non HTML content (like spread sheets) </li></ul></li></ul> <ul><li>cons: </li></ul> <ul><li><ul><li>Weaker IDE (for test recording and playback) </li></ul></li></ul> <ul><li><ul><li>develop tests in Ant or Gant only </li></ul></li></ul> <ul><li>Selenium </li></ul> <ul><li>pros: </li></ul> <ul><li><ul><li>develop tests in HTML markup or 3 GL's like Java, Ruby, etc. </li></ul></li></ul> <ul><li><ul><li>run test in actual browser</li></ul></li></ul> <ul><li><ul><li>vs.embedded Javascript engine used by NO popular browser platform </li></ul></li></ul> <ul><li>cons: </li></ul> <ul><li><ul><li>slower to start. </li></ul></li></ul> <ul><li><ul><li>see 'pros' listed Canoo </li></ul></li></ul> <ul><li>RECOMMENDATION:Id go with Canoo for testing a REST-ful API </li></ul> <p> 14. Canoo Web Test Reports Canoo's reports show overall test results and let you drill down into any test 15. Canoo Web Test Reports (cont.) Click to review a copy of the response HTML page corresponding to the first test step that failed 16. Gluing together the steps in your build process </p> <ul><li>Before running Functional Tests need to</li></ul> <ul><li><ul><li>Compile </li></ul></li></ul> <ul><li><ul><li>Run unit and integration tests</li></ul></li></ul> <ul><li><ul><li><ul><li>bail on functional tests if we catch problems with lighter weight tests </li></ul></li></ul></li></ul> <ul><li><ul><li>Package .war file </li></ul></li></ul> <ul><li><ul><li>Install the container in which we want to run the .war(optional) </li></ul></li></ul> <ul><li><ul><li>Deploy the .war to the container (optional can just do grails run-app) </li></ul></li></ul> <ul><li><ul><li>Launch your target browser </li></ul></li></ul> <ul><li><ul><li>Launch Selenium on your desired test suite </li></ul></li></ul> <ul><li>To automate this process you can use </li></ul> <ul><li><ul><li><ul><li>ant </li></ul></li></ul></li></ul> <ul><li><ul><li><ul><li>maven </li></ul></li></ul></li></ul> <ul><li><ul><li><ul><li>Groovy/Gant scripts </li></ul></li></ul></li></ul> <ul><li><ul><li><ul><li>Gradle </li></ul></li></ul></li></ul> <ul><li><ul><li>Our example uses Maven </li></ul></li></ul> <ul><li><ul><li>(demo) </li></ul></li></ul> <ul><li><ul><li>(tour of pom.xml files that wire together our build steps)</li></ul></li></ul> <p> 17. deploy Compile Unit TestIntegration Test Package .war file Download And Install Tomcat 18. Structure of our demo project mvn install Maven Repository Lives in $HOME/.m2/repostitory or/Docuemts and Settings//.m2/repository declares the artifact it produces to beorg.example:demo:1.1 declares a a dependency on org.example:demo:1.1 org.example:demo:1.1 19. Demo All Tests Pass Some Tests Fail 20. Maven Basics </p> <ul><li>Convention over configuration </li></ul> <ul><li><ul><li>Standardizes where things live and what they are named</li></ul></li></ul> <ul><li><ul><li>Lets you know where to look for the things you need</li></ul></li></ul> <ul><li><ul><li><ul><li><ul><li>and what to do when you find them </li></ul></li></ul></li></ul></li></ul> <ul><li>Project Object Model(pom.xml) specifies the complete recipe for your build </li></ul> <ul><li><ul><li>what artifact type are your producing ? (the name, the version) </li></ul></li></ul> <ul><li><ul><li>what are the dependencies (things you need) to produce that artifact ? </li></ul></li></ul> <ul><li><ul><li>What plug-ins activate at what phases in the build life cycle ? </li></ul></li></ul> <ul><li>Shared Repository for storing artifacts that result from a build </li></ul> <ul><li><ul><li>Convention for naming artifacts </li></ul></li></ul> <ul><li>Build Life Cycle </li></ul> <ul><li><ul><li>each project is typically responsible for producing a distinct artifact (a.k.a. packaging) type</li></ul></li></ul> <ul><li><ul><li><ul><li><ul><li>.jar, .war, .ear, etc.</li></ul></li></ul></li></ul></li></ul> <ul><li><ul><li>each packaging type has an associatedlife cycle (ordered set of build phases) </li></ul></li></ul> <ul><li>Nested build project (module) structure </li></ul> <ul><li><ul><li>overall driver for your build project lives at top level of a directory hierarchy </li></ul></li></ul> <ul><li><ul><li>sub-modulesunderneaththe parent can be either</li></ul></li></ul> <ul><li><ul><li><ul><li>individual components of your product or.</li></ul></li></ul></li></ul> <ul><li><ul><li><ul><li>key phases in your build </li></ul></li></ul></li></ul> <p> 21. Maven Nested Module Structure, Dependencies and Shared Repo mvn install Maven Repository Lives in $HOME/.m2/repostitory or/Docuemts and Settings//.m2/repository declares the artifact it produces to beorg.example:demo:1.1 declares a a dependency on org.example:demo:1.1 org.example:demo:1.1 22. Maven pom.xml Nested Module Structure 23. Maven pom.xml Dependency Relationships Maven Repository org.example:demo:1.1 24. Hooking Maven Plug-ins Maven Into the Build Life CycleBuild Life Cycle Phases validategenerate/process-sources process-sourcesgenerate/process-resourcescompile test prepare-packagepackage pre-integration-test integration-testpost-integration-testverifyinstalldeploy pom.xml 25. Cargo</p> <ul><li>A set of APIs that assists in </li></ul> <ul><li>installing web containers (such as Tomcat, JBoss) </li></ul> <ul><li>booting and shutting them down </li></ul> <ul><li>deploying web applications (.wars and .ears) </li></ul> <ul><li>Invokable via </li></ul> <ul><li>ant tasks </li></ul> <ul><li>maven plugin </li></ul> <ul><li>Java API </li></ul> <p> ... cargo-maven2-pluginfalsetomcat6xhttp://somewhere/tomcat-6.0.zip ... Installer installer =new URL("http://somewhere/tomcat-6.0.zip")); installer.iZipURLInstaller(new nstall(); LocalConfiguration configuration =new DefaultConfigurationFactory().createConfiguration("tomcat6x")...) container = new DefaultContainerFactory() .createContainer("tomcat6x"....); container.setHome(installer.getHome()); WAR deployable = new WAR("foo.war);deployable.setContext("ROOT"); configuration.addDeployable(deployable); 26. Launching Selenium Via Ant Walk through of ant script that launches Selenium server in separate JVM,Waits for server ready, then launches Selenium tests,then waits for shut down 27. 28. 29. Continuous Integration Dedicated box runs regular full builds (including tests) of your software Build triggers whenever any developer checks into SCMTeam is notified of any failures 30. Continuous Integration Benefits Replaces big (and long) integration cycles with small frequent ones. Same benefits as continous compilation in Eclipse Immediate feedback when an error is introduced. Fewer deltas between when it worked and when it broke=&gt; easier resolution of failures Lower ripple through impact when colleagues checks in broken code CI server build fails &amp; team is notifiedOther developers know it is not safe to pull from source Mail sent out when build goes back to normal 31. Workflow After Adopting Continous Integration CI Server: 0- Receive notification of change in SCM repo 1- Check out latest sources2- Build from scratch 3Build passes? yes:publish, e.g., deploy .war to QA serverno:send out email, flash red lights ... Developer: 0- Check CI server for current build status broken ?don't update !... otherwise.. 1- check out from SCM 2- code new feature3- run automated build on your box4- Tests pass ? no? go back to 2 ! 5- Commit changesSCM Repo broken ! Build ok? 32. Continuous Integration With HudsonHudson orchestrates the execution of repeatable jobs periodic builds (e.g., nightly)builds that are triggered by some event (like checkin to source control) Keeps History of Past Builds (Trend Reports) Notifies When Builds Fail Written in Java and runs from a .jar (no install required)Integrates with wide range of SCM systems (SVN, P4, etc.) Supports ant, maven, shell script based builds 33. Small Sample of Available Hudson Plug-ins Project Metrics Code Coverage (clover, cobertura, emma) CheckstyleFrameworks Grails(invoke grails tasks as build steps) GradleBuild Wrappers Locks and Latches- allows orchestration ofmultiple jobs Build Time Out 34. Resources </p> <ul><li>Selenium Mailing List</li></ul> <ul><li>http://old.nabble.com/Selenium-f14083.html </li></ul> <ul><li>Selenium Web Site </li></ul> <ul><li>http://seleniumhq.org/ </li></ul> <ul><li>Ant and Maven CI Tips &amp; Tricks </li></ul> <ul><li>http://wiki.openqa.org/display/SRC/Selenium-RC+and+Continuous+Integration </li></ul> <p> 35. Thank You! 36. Content Licensing Terms for This Work You may present, distribute, copy, or incorporate into your own work, any and all portions of this presentation as long as such copies, or derivative works are made available without charge. If you would like to redistribute this work on any type of fee-for-use or subscription basis, or if you wish incorporate any or all portions of this work into content which you charge for, please contact info buildlackey.com to discuss licensing terms. </p>


View more >