Testing Facebook Login and Permissions With Capybara
Having begun to use Capybara and Rspec quiet heavily there have been a few challenges that need to be overcome. One of these is that the applications I develop day to day are heavily intertwined with Facebook and user flow relies heavily on Facebook login and accepting permissions at various points throughout the application.
Outside of the Facebook chrome (the non iframed version of the application) its quite easy interact with these events as if we were a normal user.
Use Facebook Test Users
First go to your Facebook applications roles dashboard at https://developers.facebook.com/apps/[APPID]/roles and create some test users and if needed make them friends with one another. Take a note of there email, fbid and create a password for them.
Capybara Settings
Since Facebook is an external service we need to extend Capybaras default wait time incase its being slow and also change the host that the specs run under so that it matches the settings for our Facebook app.
1 2 |
|
Test User Factory
The FactoryGirl gem provides a great way to create and manage test data based on rails models. FactoryGirl can even be used with things other than models so to keep my actual user model separate from my test users data I created a special TestUser class. By placing it in a subfolder of the support folder the default installation will load all files and sub folders at initialisations allowing us access to this special class in our tests.
1 2 3 4 |
|
With this class its then easily to make a test_user factory as you would a rails model factory. In this instance iv used a nested factory for additional test users with different states.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Using these tests users in a capybara spec only has one caveat. They must be created in memory using build since there isn’t a database table for them to be saved to
1 2 3 |
|
Automating Facebook
The following module is a collection of macros that take care of the Facebook login and permissions dialogues. It attempts to deal with the various possible flows that can happen when using login with Facebook depending on wether the user has logging in and is being asked for permissions in the same sequence or if the permission are taken at a different point. There is also some javascript macros for login the user out of Facebook and removing the app permissions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
The above module can be easily included in Rspec’s configure block with the config.includes command
1 2 3 4 5 6 7 |
|
An example scenario using these macros. Its important that they are run with javascript enabled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|