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)

Timeless DEV post...

How to write a kickass README

Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.

If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.