DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

Narrow down scope with `within` in Capybara Rails testing

🔗 Parent Note

🤔 By using find method

expect(find('div.greet > h1').text).to eq 'Hello'
expect(find('div.greet > p.message').text).to eq "world"

👍 By using within method

within('div.greet') do
  expect(find('h1').text).to eq 'Hello'
  expect(find('p.message').text).to eq "world"
end

📚 Method: Capybara::Session#within — Documentation for jnicklas/capybara (master)

Top comments (1)

Collapse
 
epigene profile image
Augusts Bautra

Sometimes it's also necessary to break out of any within scoping applied, for example in a helper. Use page.document to get back to the unscoped variant.