我使用Trix Editor在我的表单中使用WISIWIG。我想用RSpec和Capybara进行测试,但是trix-editor把这个字段隐藏起来了。
<div class="form-group">
<trix-editor class="formatted_content form-control" placeholder="Description" input="annonce_ad_body_trix_input_annonce_ad"></trix-editor><input type="hidden" name="annonce_ad[body]" id="annonce_ad_body_trix_input_annonce_ad" />
</div>我需要知道如何用Capybara填充这个隐藏字段以使我的测试通过。我尝试过这些尝试:
fill_in "annonce_ad[body]", with: "my value"
find(:css, 'trix-editor').set("New text")
find("trix-editor", visible: false).set("my value")
find(:xpath, "//input[@id='annonce_ad_body_trix_input_annonce_ad']", visible: false).set "my value"
find(:xpath, "//*[@id='annonce_ad_body_trix_input_annonce_ad']", visible: false).set("value")
first('input#annonce_ad_body_trix_input_annonce_ad.class', visible: false).set("your value")这些对我来说都不起作用。有人知道在这种情况下我该如何填充我的身体(用trix)吗?
发布于 2017-08-31 00:07:24
当使用Capybara时,处理非标准控件的规则是模仿用户会做的事情。在本例中,单击可见字段(trix-editor元素),然后键入所需内容。你永远不应该尝试填充不可见的元素,事实上,在测试应用程序时,visible选项应该很少使用(如果有的话)(如果使用Capybara进行抓取,这是有意义的)。所以在你的情况下,这应该是
find('trix-editor').click.set('New text')它可能会在没有点击的情况下工作,但更全面地复制用户并不会有什么坏处。既然你已经说过类似的东西对你不起作用(但没有提供实际的错误),我不得不假设你实际上没有使用支持JS的驱动程序。由于trix是JS驱动的编辑器,因此在测试它时需要使用支持JS的驱动程序-- https://github.com/teamcapybara/capybara#drivers。
下面的基本ruby代码片段显示了Capybara填充在trix-editor.org上的demo trix编辑器中
require 'capybara/dsl'
require 'selenium-webdriver'
sess = Capybara::Session.new(:selenium_chrome, nil)
sess.visit('https://trix-editor.org/')
editor = sess.find('trix-editor')
editor.click.set('My test text')
editor.assert_text('My test text')发布于 2017-08-31 00:00:19
我犯了个错误,这对我很有效。
find(:xpath, "//*[@id='annonce_ad_body_trix_input_annonce_ad']", visible: false).set("some value here")https://stackoverflow.com/questions/45962746
复制相似问题