RSpec + Capybara: How do I test a Bootstrap progre

2019-07-30 14:58发布

问题:

I am testing a page that has a bootstrap progress bar using RSpec. How do I test the different Aria attributes in the Div?

<div class="progress-bar progress-bar-success" 
    role="progressbar"  
    aria-valuenow="40"  
    aria-valuemin="0"  
    aria-valuemax="100"  
    style="width: 40%"> 
</div>

I appreciate any help you can provide.

Thanks!

回答1:

You can get the attribute values of an element using the [] method. For example:

find('div.progress-bar')['aria-valuenow']
#=> "40"
find('div.progress-bar')['aria-valuemax']
#=> "100"

You can test the values by doing:

expect(find('div.progress-bar')['aria-valuenow']).to eq('40')
expect(find('div.progress-bar')['aria-valuemax']).to eq('100')

However, that will not use Capybara's built-in wait methods. If you using the wait methods is important, you should do:

expect(page).to have_css('div.progress-bar[aria-valuenow="40"]')
expect(page).to have_css('div.progress-bar[aria-valuemax="100"]')


回答2:

You can also do cool things like this:

it 'reports the status of dropdowns (expanded/collapsed) to non-visual agents', js: true do
  within '#sign_in_panel' do
    expect {
      click_link 'Sign in'
    }.to change { find('a.dropdown-toggle')['aria-expanded'] }.from('false').to 'true'
  end
end