【问题标题】:Use rspec / capybara to test that css element has received correct attribute?使用 rspec / capybara 测试 css 元素是否已收到正确的属性?
【发布时间】:2015-02-20 20:29:22
【问题描述】:

在我的 rails 应用程序中,我询问用户他们最喜欢的颜色。他们选择一个值(十六进制输入)。颜色被保存(例如#D2D2D2),然后作为背景颜色元素应用到预定义的css类:

<html>
 <body>
   <style>
     .favorite_color { background-color: #D2D2D2; } /* updated dynamically <%= @user.favorite_color %> */
   </style>
   <div class="favorite_color">My favorite color</div>
   <div class="some_content">Some content</div>
   <div class="favorite_color">My favorite color again</div>
 </body>

一切正常,但我现在想知道如何测试保存的值是否正确应用为 div 出现的任何地方的背景颜色。

我尝试过(使用 rspec 和 capybara): p page.find('.favorite_color')[:style]

但我最终以 nil 作为输出。有没有人对我如何获得背景颜色有任何建议,以便我可以测试它是否正确应用?

谢谢!

【问题讨论】:

    标签: ruby-on-rails-4 rspec capybara


    【解决方案1】:

    您应该填写它并期望它在您重新访问该页面时出现。

    fill_in 'the_id_of_the_input_field', with: '#D2D2D2'
    
    expect(page).to have_field('the_id_of_the_input_field', with: '#D2D2D2')
    

    或者如果您更喜欢使用该类

    fill_in '.favorite_color', with: '#D2D2D2'
    
    expect(page).to have_field('.favorite_color', with: '#D2D2D2')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多