Rails项目,用水豚测试一个隐藏字段:Selenium::WebDriver::Error::ElementNotVi



(其他堆栈溢出答案没有回答我的问题(

我有一个带有hidden_field_tag的表格:

<form action="/score" method="post">
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<%= hidden_field_tag :letters, @letters, id: "idgrid" %>
<input type="text" name="word" placeholder="type world">
<input type="submit" value="send">
</form>

我想用水豚运行以下测试(迷你测试(

需要"application_system_test_case">

class GamesTest < ApplicationSystemTestCase
test "word in the grid but not english" do
visit new_url
fill_in "word", with: "car"
find('#idgrid', visible: false).set("N e w t e x t")
click_on "send"
assert_text "Sorry, can't build from grid"
end
end

我收到以下错误

Error:
GamesTest#test_word_in_the_grid_but_not_english:
Selenium::WebDriver::Error::ElementNotVisibleError: element not visible
(Session info: headless chrome=68.0.3440.106)
(Driver info: chromedriver=2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db),platform=Mac OS X 10.13.6 x86_64)
test/system/games_test.rb:22:in `block in <class:GamesTest>'

我已经阅读了与该主题相关的其他堆栈溢出问题,但没有找到正确的答案。还有github问题。我知道该元素是不可见的,但是我已经读到,如果我使用可见的:false,水豚可以设置值。

有人可以帮我吗?

谢谢

使用 Capybara,您可以查找和检查不可见的元素,但您无法直接设置它们的值,因为普通用户无法设置,因此在功能/系统测试中直接更改它们并没有多大意义。如果您确实需要更改隐藏字段值,则需要通过 JS 使用类似

find('#idgrid', visible: false).execute_script("this.value = "N e w t e x t")

由于这不会复制用户通常会执行的行为,因此在功能/系统测试中通常被认为是不好的做法,您确实应该考虑是否有更好的方法来设置实际复制用户的字段值(或者它是否更有意义作为不同类型的测试(。

最新更新