DEV Community

n350071๐Ÿ‡ฏ๐Ÿ‡ต
n350071๐Ÿ‡ฏ๐Ÿ‡ต

Posted on

Run Javascript by Capybara feature test

๐Ÿ”— Parent Note

๐Ÿค” Situation

Let's say you have following iframe. And, you want to assert that a selector inside the iframe is not shown when the iframe height is too short.

<div>
  <iframe src='https://n350071.com' id='n350071-frame'></iframe>
</div>

๐Ÿ‘ Solution

Use execute_script method on Capybara.

# run a script
page.execute_script("$('#n350071-frame').height(10)")

# then, test it.
within('n350071-frame') do
  expect(find('div.body').not_to have_selector('div.target')
end

The syntax is this.

execute_script(script, *args)

๐Ÿ’š one more tips: evaluate_script

If you need return value, please use evaluate_script. But, be careful, it might return complex jQuery object. So, you should use execute_script by default.

evaluate_script(script, *args)

ๆณจๆ„: jQueryใฎ่ค‡้›‘ใชๆˆปใ‚Šๅ€คใŒๅธฐใฃใฆใใ‚‹ๅ ดๅˆใซใฏใ€ๆˆปใ‚Šๅ€คใฎใชใ„execute_scriptใŒใ‚ˆใ„ใ€‚

๐Ÿ“š Reference

Top comments (0)