Aug 25, 2011

Capybara + RSpec to test Facebook connect

In order to set up Capybara with Facebook connect, we need to set up a few things:

1. You need to specify a server port for Capybara and set localhost:port in your Facebook App setting. Otherwise Facebook won't redirect user back to your URL (security reason)

In order to do that, just drop this line in spec_helper.rb:


Capybara.server_port = 57124


2. You need to properly handle Facebook login and allow page. Here is the method that I use:


def facebook_connect(username,password)

if current_path == '/login.php'
within_window "Log In | Facebook" do
fill_in 'email', :with => username
fill_in 'pass', :with => password
click_button "Log In"
end
end

if current_path == '/connect/uiserver.php'
click_button "อนุญาต" rescue ""
click_button "Allow" rescue ""
end
end


That's it.