Capybara Capybara vă ajută să testați aplicațiile web simulând modul în care un utilizator real ar interacționa cu platforma în specificațiile de integrare.
Navigare și Acțiuni
visit '/login'
visit articles_path
Clic pe linkuri și butoane
click_on 'Submit' # Potrivește textul linkului sau butonului
click_button 'Save Changes'
click_link 'Log Out'
Controale de formular
fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'secret'
choose 'Male' # Buton radio
check 'Accept Terms' # Casetă de selectare
uncheck 'Send Newsletter'
select 'California', from: 'State'
unselect 'Hawaii', from: 'State'
attach_file 'Avatar', '/path/to/image.png'
Delimitare și Context (Scoping)
Limitați acțiunile la un anumit nod DOM sau formular:
within '.modal-dialog' do
fill_in 'Username', with: 'admin'
click_button 'Confirm'
end
within_fieldset 'Shipping Address' do
fill_in 'City', with: 'San Francisco'
end
within_table 'Orders' do
expect(page).to have_content('$99.00')
end
Potriviri RSpec (Matchers)
expect(page).to have_content('Welcome Back')
expect(page).to have_current_path('/dashboard')
# Prezența elementelor
expect(page).to have_selector('div.alert', text: 'Success')
expect(page).to have_css('button.primary')
expect(page).to have_xpath('//table/tr')
# Controale de formular
expect(page).to have_field('First Name', with: 'John')
expect(page).to have_checked_field('Agree to Terms')
expect(page).to have_select('Role', selected: 'Developer')
expect(page).to have_button('Submit', disabled: false)
expect(page).to have_link('Profile', href: '/users/1')
# Aserțiuni negative
expect(page).to have_no_content('Error')
expect(page).not_to have_selector('.modal')
Selectori de căutare (Finders)
btn = find('button.submit')
btn.click
field = find_field('Email')
link = find_link('Home')
all('li.nav-item').each do |item|
puts item.text
end
Alerte și ferestre modale JavaScript
# Acceptare alertă nativă a browserului
accept_alert do
click_button 'Show Alert'
end
# Acceptare confirmare modală
accept_confirm do
click_button 'Delete Account'
end
# Anulare confirmare modală
dismiss_confirm do
click_button 'Cancel Order'
end
Depanare (Debugging)
# Deschide pagina HTML randată în browser
save_and_open_page
# Salvare captură de ecran
save_and_open_screenshot
# Executare JavaScript
page.execute_script("$('.dropdown').show()")
result = page.evaluate_script("window.innerWidth")