DEV Community

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

Posted on

3 1

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay