Connect/Automating Tests
From Facebook Developer Wiki
Contents |
Automating Functional Tests of Your Connect Implementation
In order to keep Facebook Connect fast and stable, and to help you ensure that your implementations are working and bug free, we created a library that would help you easily create automated tests for your own site.
Getting Started
First, you'll need to install Watir. Please be sure to install the Firefox plugin, as the library and this demo assume you're using Firefox for your testing.
Download FBWatir and the StreamdiffTest example.
Before diving into the code, let's just try running the tests on the command line to see them pass and confirm that everything is set up correctly. Go to the terminal and cd into the directory where you downloaded fbwatir.rb, streamdiff.rb, and so forth. Run:
fbtestrunner.rb utilizes a Ruby Unit TestRunner.
Every parameter handed to fbtestrunner.rb should be the name of a .rb file in the directory, which represents a site for testing. So in this case we have streamdiff.rb. Each of these site-test files should require 'fbwatir.rb' and 'test/unit' at the top, as we see in streamdiff.rb:
The site-test file (streamdiff.rb in our example) should then define a class that extends Test::Unit::TestCase, which should have the same name as the file, only capitalized, and appended with "Test", as we see inside streamdiff.rb:
The next few lines are for initialization, identifying a specific Connect test user and his/her credentials which we can utilize for our tests:
The method setup always gets called by the UnitTestRunner at the beginning of each test run. So this is where you would add any additional initialization code needed for your test. init() is a method in the FBWatir module that allows us to initialize the Connect credentials of the account we'll be using in our tests.
After calling setup, the Ruby UnitTestRunner will run every method in the defined class which begins with "test". Then, in those test methods, we'll add our assertions..
Methods for Interacting with Facebook Connect
Once init has been called, the FBWatir module provides four methods for easily interacting with Facebook Connect:
- disconnectFromFacebook(appId) : This deauthorizes our application, removes all extended permissions, and logs us out of Facebook.
- loginAndGoToFacebook : This logs us into Facebook and then returns only when fully logged in and loaded a logged in Facebook.com/home.php
- loginToConnect(urlRegex) : This fills out the credentials in the Facebook Connect iframe/popup, and also clicks all the necessary buttons to allow all extended permissions requested.
- You would call this once you had navigated to your website and clicked the Facebook Connect button -- which you'll see in Streamdiff.
- The URLRegex should be a simple /regex/ that matches the URL of your site, so that Watir can reattach to it once it's done interacting with the Connect login popup.
- goFB : Go to Facebook and don't return until either the home.php or login.php page is fully loaded.
Before looking at the complex "test" methods in our StreamdiffTest class, let's go to the bottom of the file and look at some of the utility methods we've defined. The structure utilized around these generic methods should be reusable for most Connect implementations.
You're going to want an easy to way to simply visit your website and confirm that the page is fully loaded before proceeding. Usually when your site loads it will render either some indicator that you need to log in (a "Log In" link, for example), or it will render some alternative indicator showing that you're logged-in already -- perhaps it will welcome you by your full name in a <div> near the top. We want to define a function for each of these two DOM objects, and then we'll have a go method that can wait for the existence of one of these two DOM objects in order to be sure the page has loaded. Here's how this looks in Streamdiff:
Now we're going to want some utility functions to connect/disconnect our test user from our Connect site. So let's implement connect and disconnect functions.
Adding Content to Streamdiff
There are three basic actions we can take on Streamdiff. We can:
- Post to the stream
- Comment on a post
- Like a post
So let's create a utility method for these three actions. Let's call them postToStream(), likePost(message), and commentOnPost(message).
Creating Simple Tests
Now, let's make some tests! First let's do simple tests that assert our user can connect/disconnect successfully:
Testing Interactions with the Stream
To test our interaction with the Stream, let's publish a new post to the Stream, then add a comment and like the post we just created. At each step along the way, we'll visit Facebook.com to make sure the post gets updated in the Stream, and then we'll re-visit our own website to make sure it still shows up there, too.
First, let's define a function that will create the post and assert that it's successful.
Now let's define a function that will "Like" a given post in the stream. Note that we're naming these with "_test" instead of "test" because we don't want our UnitTestRunner to run them directly. Rather we'll call them from within our "Comment" test. Here's the "Like" test:
Finally, let's define our Comment test method, which incorporates the Like and Post tests:
There you have it! Let us know what you think in the Developer Forum. We hope this helps!
