测试Web应用程序与Cucumber的Facebook集成

我想通过执行以下场景来使用Cucumber和Capybara来测试我的Rails应用程序的Facebook注册过程:

@javascript Scenario: Connect account When I go to the home page And I press "Connect your Facebook account" And I fill in "test@address" for "Email" And I fill in "test" for "Password" And I press "Login" And I press "Allow" Then I should be on the dashboard page 

人们如何解决基于浏览器的Facebook集成的集成测试问题? 网络搜索只显示了几个早于图API的博客文章,他们似乎只讨论实现一个前提条件,即设置一个代表已登录到Facebook的用户的环境。

要解决的一些具体问题:

如何模拟单击元素? 我想出的最好的事情(但尚未测试)是手动触发元素上的click事件。

一旦Facebook对话框出现,Capybara可以访问它吗? 怎么样? 这似乎是为了输入测试Facebook帐户的电子邮件地址和密码以及为测试帐户安装Facebook应用程序所必需的。

就此而言,如何管理测试帐户? http://developers.facebook.com/docs/test_users/表示可以使用Graph API创建和删除测试用户,但API不会提供对测试帐户的电子邮件地址或密码的访问权限,这似乎使它们无法用于测试这个特定的流程。

我敢肯定我也忽略了其他重要问题。 我很好奇其他人是如何攻击这个问题的!

我已经设法使用以下代码在rspec验收规范中使用它:

https://gist.github.com/1084767

这使用mogli来创建和销毁测试用户和capybara以编排浏览器测试。 我相信你可以通过删除shared_context块并在钩子之前/之后使用Cucumber来使用Cucumber,例如:

 Before '@fb_test_user' do @fb_user = create_test_user(installed: false) end After '@fb_test_user' do @fb_user.destroy end World(FacebookIntegrationHelpers) 

我目前正在使用omniauth gem来处理这种类型的身份validation。 FBML已被弃用,因此我建议转到JS SDK进行客户端FB交互。 无论如何,如果你正在使用omniauth,你可以通过在env文件夹中指定一些Before标签来相对容易地存根响应,如下所示:

配置成功案例:

 Before('@omniauth_test_success') do OmniAuth.config.test_mode = true OmniAuth.config.mock_auth[:facebook] = { "provider" => "facebook", "uid" => '12345', "user_info" => { "email" => "email@email.com", "first_name" => "John", "last_name" => "Doe", "name" => "John Doe" # any other attributes you want to stub out for testing } } end 

配置失败案例

 Before('@omniauth_test_failure') do OmniAuth.config.test_mode = true [:default, :facebook, :twitter].each do |service| OmniAuth.config.mock_auth[service] = :invalid_credentials # or whatever status code you want to stub end end 

Cukesfunction

 Scenario: A user unsuccessfully signs in with their email/password Given I am on the homepage When I follow "Login" And I "unsuccessfully" sign in with email and pass Then I should see "Failed." @omniauth_test_success Scenario: A user successfully signs in with Facebook Given I am on the homepage And I follow "Login" When I follow "facebook" Then I should see "Login successful." @omniauth_test_failure Scenario: A user unsuccessfully signs in with Facebook Given I am on the homepage And I follow "Login" When I follow "facebook" Then I should see "Failed."