Testing social networks using capybara, cucumber-rails and selenium

Integration of social networks is very common in every application. We have many gems are available for doing integration. But testing this social integration is complicated task. Using capybara with cucumber we can easily test this similar to the normal UI testing through capybara.

Step 1:
Set up default driver to selenium.

#features/support/env.rb
Capybara.default_driver = :selenium

Step 2:
Generate cucumber feature and follow below steps to write step definitions:
1) To click on ‘Facebook’ login:

   page.find(:xpath, "//a/img[@alt='Fb-login-button']/..").click

Here I am using xpath selector to find facebook login button.’Fb-login-button’ alt value of my facebook button.

2) Fill up the Facebook login form. This will differ if we are doing login through a popup.

For popup login:

      begin
      within_window(page.driver.browser.window_handles.last) do
         fill_in('email', :with => Fb-Email)
         fill_in('pass', :with => Fb-password)
         click_on('Log In')
      end
      rescue Exception => e
         p "Selenium Exception: Session has no driver"
      end
   

Sometimes selenium will throw an exception. So its better to use exception handling.

For Standard Facebook login, just write:

       fill_in('email', :with => Fb-Email)
       fill_in('pass', :with => Fb-password)
       click_on('Log In')

Now your are done. You automated social networking testing. Follow above steps even for ‘twitter’ login also.

I hope everyone will enjoy this article. Any queries or suggestions would be welcome.

Advertisements

2 thoughts on “Testing social networks using capybara, cucumber-rails and selenium

  1. this was so helpful. i had been looking for something like this for a while. But unfortunately, facebook’s jssdk now puts the “Log In” button inside a element and that seems to be unclickable. That is where I am stuck now. Would you be able to help me out with that??

    After I get over that hurdle, I am sure this post will take me the rest of the way to testing facebook login.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s