@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Play with Capybara from interactive shell

Interactive shell like irb or pry is best friend to learn how to write code in programming language. Playing with Capybara from interactive shell session also very good to get used to Capybara's powerful API.

You can load Capybara from irb/pry console, but it requires some additional setup. Instead of it, just put a prepared executable script and run it from the shell.

$ capybara example.com
including Capybara::DSL in the global scope is not recommended!
Visit to http://example.com

Frame number: 0/2
[1] pry(main)> page.title
=> "Example Domain"
[2] pry(main)> page.has_css?('h1', text: 'Example Domain')
=> true
[3] pry(main)> find(:xpath, '//p[1]').text
=> "This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission."

This tiny script is build on pry and poltergeist (phantomjs driver of capybara).

Capybara console