Skip to content

Debugging with Pry

davidnquach edited this page Sep 22, 2014 · 5 revisions

Debugging with Pry

Introduction

Pry: is a powerful debugging tool for ruby automation. There are a lot of things that pry can do, a few of the more notable ones are:

  1. Runtime invocation (use Pry as a developer console or debugger)
  2. We can use the pry to inspect variables and step through a program.
  3. Navigation around state (cd, ls and friends)
  4. Pry allows us to jump into another object’s scope by using the command cd, and listing it’s methods with ls.
  5. Live help system
  6. At any point during a pry session the help command can be called to show you commands that pry can do and show you documentation on methods with show-doc
  7. Stepping through a test
  8. Using the extension pry-byebug you can step through a test using the command next, step, finish, and continue.

Pry Tutorial

Note You should have already cloned down the ruby-automation repository and gone through the introductory tutorial. Also create a new branch before starting this tutorial.

There are two gems needed, pry and pry-byebug. Pry-byebug is an extension to pry that allows pry to use commands such as step and next to walk through a program. To add these Gems to your project, open the Gemfile and add the lines

gem 'pry'
gem 'pry-byebug'

Then in your IDE’s terminal, type the command:

bundle install

This will install all the gems in that file including the ones you just added.

To use pry all you have to do is to require 'pry' at the top of the file you want to debug and then add binding.pry anywhere you want the program to stop at.

For this tutorial we will be debugging in the add_departments_steps file. So in your ide open add_department_steps.rb file and add

require 'pry' 

at the top of the file. Then place the line

binding.pry

after the line departments = [] in the Then step definition. This will place a breaking point in that step definition.

Now run the add departments feature with cucumber by typing in the terminal

cucumber features/add_department.feature

You will see a browser pop up and run through the test like normal until it stops at the line where we placed binding.pry.

The arrow is pointing to the next line that is going to be executed next. From here you can view your variables, check if your methods are choosing the correct object and jump into another object to view it’s methods. You can also step through the test to ensure that the test does what it is supposed to do.

Checking Variables

First type help in the terminal to bring up the list of commands Pry has available.

If you just want to check your variables then you can just type out the variable you want to check, type departments in the terminal to see the value of departments. You can also run your methods to check if they’re working correctly. In the terminal type department_page.departments_elements to get an array of all the department objects.

A very useful command to use is ls. By default typing ls shows you the local variables defined in the current context, and any public methods or instance variables defined on the current object.

Type ls in the terminal skipping the first few pages, which are nonsense. To scroll through the page you can press the spacebar to scroll by page or the down arrow to scroll by line. When you get towards the bottom of the page you will see all the methods and variables that you can call at that moment.

Navigating Around State

The cd command is used to move into a new object (or scope) inside a Pry session. When inside the new scope it becomes the self for the session and all commands and methods will operate on this new self.

Type cd department_page in the terminal to move into the department_page object. Within the scope of the department_page you have access to the methods and variables for that object.

To have pry step through the test use one of the following commands:

Step: s; will step into the next line or method

Next: n; step over to the next line within the same frame

Finish: f; execute until current stack frame returns

Continue: c; continue program execution and end Pry session

Conclusion

This is a good starting point to help you figure out some of the problems you’re having with your tests. Just add a require pry statement to the top of the file and binding.pry wherever the problem occurred.

Resources

Pry Wiki

Pry Byebug github

Clone this wiki locally