What We Just Learned

Testing web applications can be done in different ways. How to do it depends on what programming language your application is written in and whether it’s using JavaScript and Ajax. Let’s summarize what we’ve covered in this chapter:

  • Capybara has a simple DSL for navigating around URLs, filling in forms values, and clicking buttons and links.

  • Capybara has several drivers—some of them built-in, and some of them available as separate gems—that allow the same code to automate different browsers or browser simulators.

  • Web applications written in Ruby can use Capybara’s Rack driver to test functionality without JavaScript or Ajax. This is the fastest possible way to do an end-to-end test.

  • For Ruby/Rack applications, the @javascript tag tells Capybara to use the Selenium driver to test pages that use JavaScript, allowing you to mix scenarios that use the faster Rack driver with scenarios that need a browser.

  • Web applications that use Ajax (or that are not based on Ruby/Rack) cannot be tested with Capybara’s Rack driver, but it’s easy to switch over to the Selenium driver, which brings up a browser.

  • We learned to extract multiple text values from the page using Capybara’s all method and then compare them with a Gherkin table using diff!.

  • The A in Ajax stands for asynchronous, which means you sometimes need to think about where to put synchronization points into your tests. Capybara will wait for Ajax content to appear when you call the find method.

Try This

Capybara can be used outside of Cucumber too. A handy way to practice getting to grips with Capybara is to automate some dull repetitive task that you have to do on the Web, such as filling in timesheets or logging in to your Internet banking.

Create a plain old Ruby script like this one, and flesh it out with whatever you’d like it to automate:

 require ​'capybara/dsl'
 
 Capybara.run_server = ​false
 Capybara.default_driver = ​:selenium
 Capybara.app_host = ​'http://www.google.com'
 
 class​ WebRobot
 include​ Capybara::DSL
 
 def​ go
  visit ​'/'
 # ... etc
 
  puts(​'press any key to finish'​)
  gets
 end
 end
 
 WebRobot.new.go

Save that file as script/web_robot.rb and run it with this:

 $ ​​bundle​​ ​​exec​​ ​​ruby​​ ​​web_robot.rb

Now flesh out the robot to do your bidding!

Footnotes

[60]

http://rubygems.org/gems/capybara

[61]

See Chapter 4, Step Definitions: From the Outside to learn about tables.

[62]

Using GET instead of POST makes searches easier to share—users can paste the search URL into emails and other systems, or they can bookmark them. Here is an example search URL: http://localhost:3000/search?query=I%20am.

[63]

http://rubygems.org/gems/capybara-webkit

[64]

The Document Object Model is a tree-structure representing elements in a HTML page: http://www.w3.org/DOM/.

[65]

http://rubygems.org/gems/service_manager

[66]

Selenium is a web browser automation tool that will open a real browser to interact with a web server. You can learn more about Selenium at http://seleniumhq.org/.

[67]

http://rubygems.org/gems/ruby-debug19

[68]

http://docs.jquery.com/QUnit/

[69]

http://pivotal.github.com/jasmine/

[70]

http://en.wikipedia.org/wiki/Progressive_enhancement

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.39.60