ruby-on-rails 如何使用Rspec、Capybara和Chromedriver为模态设置系统测试

t1qtbnec  于 2023-08-08  发布在  Ruby
关注(0)|答案(1)|浏览(182)

我是新手,我目前的理解是,你不能把JavaScript和Rspec测试混合在一起(?).
对于我的第一个Rspec/Capybara系统测试,我希望用户登录,然后单击模态链接以查看模态是否存在(稍后与模态交互)。
相关的宝石(这是一个旧的2017年项目)

  1. group :development, :test do
  2. # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  3. gem 'byebug', platform: :mri
  4. gem 'rspec-rails', '~> 3.5.0'
  5. gem 'puma', '~> 3.0'
  6. end
  7. group :test do
  8. gem 'capybara', '~> 3.6.0'
  9. gem 'factory_girl_rails', '~> 4.7'
  10. gem 'faker'
  11. gem 'selenium-webdriver', '~> 3.0.0'
  12. gem "database_cleaner", "~> 1.5"
  13. gem 'shoulda-matchers', '~> 3.1'
  14. gem 'launchy'
  15. end

字符串
rspec

  1. scenario "Fans in Canada can sign in, visit signed in home page, and log out in" do
  2. create_fan_in_canada
  3. visit root_path
  4. #fist LOG IN link
  5. within('#myNavbar') do
  6. expect(page).to have_link "LOG IN"
  7. end
  8. #second LOG IN link
  9. within('.follow-login') do
  10. expect(page).to have_link "LOG IN"
  11. end
  12. within('#myNavbar') do
  13. click_link('LOG IN')
  14. end
  15. fill_in "Email", with: @fan.email
  16. fill_in "Password", with: @fan.password
  17. click_button "Sign in"
  18. find('.dropdown-toggle').click
  19. expect(page).to have_link "Logout"
  20. expect(page).to have_current_path root_path
  21. ## MODAL TEST ##
  22. find('#actionLink').click
  23. within('#actionModal') do
  24. page.should have_content('#fan1')
  25. end
  26. expect(page).to have_css '#fan1'
  27. ## MODAL TEST END ##
  28. click_link "Logout"
  29. expect(page).to have_css '#splash_page_loads'
  30. end
  31. ############
  32. ############


Selenium chromedrive是按照Selenium文档下载并放在一个路径文件中的,这个rails-helper代码由this blog提供。

  1. Capybara.register_driver :selenium_chrome do |app|
  2. Capybara::Selenium::Driver.new(app, browser: :chrome)
  3. end
  4. Capybara.javascript_driver = :selenium_chrome


结果如下:
如果不包括## MODAL TEST ##代码,测试通过。
如果我包括## MODAL TEST ##,则测试失败,控制器将显示以下错误:

  1. Failures:
  2. 1) Splash and devise: Fans in Canada can sign in, visit signed in home page, and log out in
  3. Failure/Error:
  4. respond_to do |format|
  5. format.js # this goes to actionMain.js.erb
  6. end
  7. ActionController::UnknownFormat:
  8. ActionController::UnknownFormat
  9. # ./app/controllers/actionmodal_controller.rb:41:in `actionMain'


如果我将:js => true添加到scenerio中,我会得到以下错误:

  1. Failures:
  2. 1) Splash and devise: Fans in Canada can sign in, visit signed in home page, and log out in
  3. Failure/Error:
  4. within('#myNavbar') do
  5. expect(page).to have_link "LOG IN"
  6. end
  7. NameError:
  8. uninitialized constant Selenium::WebDriver::Error::ElementNotInteractableError


让我相信我要么设置错误,要么我不得不在它自己的单独场景中添加模态测试,专门用于JavaScript Chromedriver。
如果我这样做了,但是,我将无法登录用户事先等,将需要存根他们也许,但我也有多个模式与许多链接,他们改变了模式的内容,我也想测试以后,其中我不相信处理Chromedriver.
如何设置水豚和Rspec,以便在常规系统测试中可以毫无瑕疵地打开模态?因为只处理Chromedriver或其他在广泛的系统测试中处理正常用户操作似乎不正确。

iyzzxitl

iyzzxitl1#

名称错误:未初始化常量Selenium::WebDriver::Error::ElementNotInteractableError
可能是由于使用了Capybara和selenium-webdriver的不兼容版本造成的。

相关问题