【问题标题】:Utilizing selenium to test Javascript elements via RSpec causes other parts of the RSpec test to fail通过 RSpec 使用 selenium 测试 Javascript 元素会导致 RSpec 测试的其他部分失败
【发布时间】:2015-07-22 04:57:32
【问题描述】:

我正在尝试将 Stripe 集成到我正在构建的网络应用程序中。然而,当我尝试测试“用卡付款”按钮时,我遇到了一个小问题。无论我做什么,rspec 都会抛出 ElementNotFound 错误。

经过一番搜索,我发现这是因为默认驱动程序 rack_test 不支持 Javascript。然后我参考了一些文档:https://github.com/jnicklas/capybara#drivers 并将 :js => true 添加到我的 RSpec 场景之一,并将 selenium-webdriver gem 添加到我的 gemfile。

但是,这带来了另一组问题。现在,每当我运行测试时,我都会被告知我使用了无效的用户名/密码组合,并且我无法进入测试的下一部分以查看是否可以单击该死的按钮! Selenium 似乎没有识别和/或使我的用户工厂无效。

举手。

任何帮助将不胜感激。

spec/features/user_upgrade_premium_spec.rb:

require 'rails_helper'

describe "Upgrade from Standard to Premium Plan" do
    before do
     @user = create(:user)
     visit root_path
     click_link "Sign In"
     fill_in 'Email', with: @user.email
     fill_in 'Password', with: @user.password
     click_button "Sign In"
     expect(page).to have_content "Signed in successfully."
  end

  scenario "Successfully", :js => true do
    click_link "My Account"
    click_link "Upgrade Account"

    click_button "Pay with Card"
 end
end  

spec/factories/user.rb

FactoryGirl.define do
  factory :user do
    name "User One"
    sequence(:email, 100) { |n| "person#{n}@example.com" }
    password "helloworld"
    password_confirmation "helloworld"
    confirmed_at Time.now
  end
end

【问题讨论】:

  • 你使用的是页面对象模式吗?
  • replace describe "Upgrade from Standard to Premium Plan" do with RSpec.describe "Upgrade from Standard to Premium Plan" do

标签: javascript ruby-on-rails ruby selenium rspec


【解决方案1】:

在超时类别下尝试script_timeout,例如implicit_waitpage_load;全局声明它,让我们看看它在我们的案例中是如何工作的!

@driver.manage.timeouts.script_timeout = 15

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    相关资源
    最近更新 更多