Skip to content

Instantly share code, notes, and snippets.

@douglasmartins7
Created June 6, 2019 21:09
Show Gist options
  • Save douglasmartins7/5074bd207959092d5a6d6437f7720762 to your computer and use it in GitHub Desktop.
Save douglasmartins7/5074bd207959092d5a6d6437f7720762 to your computer and use it in GitHub Desktop.
capybara
# Find element by css
find(:css, 'css selector', options)
# Find element by xpath
find(:xpath, 'xpath value', option)
Element Examples
# Set some text to some input form
find(:xpath, 'some xpath').set('some text')
# Show text in the element
find('id_selector', visible: false).text
# Show value in the element
find_field('id_selector').value
# Check checkbox
find('checkbox_selector').checked?
# Check Selectbox
find('select_tag_selctor').selected?
# Check Visibility
find('a', text: 'next').visible?
# Waiting & try to check the element for 30 seconds
Capybara.using_wait_time(30) do
expect(find('#message')).to have_text('Complete')
end
# Click alert
accept_alert do
click_on 'Show Alert'
end
# Dismiss confirm
dismiss_confirm do
click_on 'Delete'
end
# Execute JavaScript
execute_script 'window.scrollTo(0, 900)'
# Capture screen
save_screenshot('xxx.png')
# Debugging
save_and_open_page
🚜 Querying Examples
# Check existance of the element by css
expect(page).to have_css('#something')
# Check existance of the element by xpath
expect(page).to has_xpath('#something')
# Check existance of the element by content
expect(page).to has_content('#something')
# Check no existance of the element by content
expect(page).to has_no_content('#something')
# Check no existance of title
expect(page).to has_title('#something')
😸 Scope Examples
# Scope with xpath
within("//li[@id='example']") do
fill_in 'Full Name', with: 'John Due'
end
# Scope with CSS
within(:css, "li#example") do
fill_in 'Full Name', :with => 'John Due'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment