Ruby 在水豚中,如何用包含制表符的文本填充字段?
我的每个场景都读取文件中的一个示例,并将其复制到文本字段中:Ruby 在水豚中,如何用包含制表符的文本填充字段?,ruby,capybara,capybara-webkit,Ruby,Capybara,Capybara Webkit,我的每个场景都读取文件中的一个示例,并将其复制到文本字段中: def sample(name) IO.read("spec/samples/#{name}.bib") end feature 'Import a record' do scenario 'from JabRef' do fill_in 'bibtex', :with => sample('jabref') in_dialog.click_button 'Import' ... end
def sample(name)
IO.read("spec/samples/#{name}.bib")
end
feature 'Import a record' do
scenario 'from JabRef' do
fill_in 'bibtex', :with => sample('jabref')
in_dialog.click_button 'Import'
...
end
end
这很好,直到其中一个样本有了一个表格:当手动复制和粘贴工作时,测试失败了
从其他问题[],我了解到将
\t
和\n
解释为键控输入应该是一项“功能”。是否有办法停用此功能并仅“粘贴”内容?如果其他方法都失败,您可以使用以下方法插入文本:
然后,在您的特性规范中,您只需
feature 'Import a record' do
scenario 'from JabRef' do
fill_in_plain 'textarea[name="bibtex"]', with: sample('jabref')
in_dialog.click_button 'Import'
...
end
end
请注意,
fill\u in \u plain
帮助程序现在只理解jQuery选择器(即CSS选择器)字符串作为其第一个参数。在浏览器的文本字段中,如果按Tab键,它是否切换到另一个字段?在文本字段中插入文字选项卡是一个众所周知的难题,有帮助吗?@alex.frost我的问题是fill\u in
模拟键入而不是粘贴。我绝对希望用户不要在字段中粘贴标签。然而,有些人已经这样做了,而且可能还会这样做。我修复了处理此问题的软件,现在我想对此进行一次水豚回归测试。好的,这是一个长期的尝试,但可能会在ruby中将“\t”转换为“\u0009”。这是制表符的unicode。您也尝试过使其尽可能简单:填写'bibtex',:with=>“text with\u0009 tab in”
?谢谢您的建议。我在IO.read
之后添加了.gsub(“\t”,“\u0009”)
。我的其他示例仍然正常,但带有选项卡的示例仍然失败。如果我用.gsub(“\t”,“”“)
更改它,测试通过,但它是一个完全不同的测试。我担心s.gsub(“\t”,“\u0009”)
与s
具有相同的精确值。如果您使用Selenium作为水豚驱动程序,请尝试find(locator).native.send\u keys Seleniu::Webdriver::keys[:tab]
而不是在locator中填充“\t”
。我会试试的。我已经扩展了我的答案,可能也会有帮助;-)
module CapybaraWebkitWorkarounds
def fill_in_plain(selector, with: nil)
value = with.gsub '"', "\\\"" # poor man's escaping
page.execute_script %Q{ $("#{selector}").val("#{value}") }
end
end
RSpec.configure do |config|
# make it available in all feature specs
config.include CapybaraWebkitWorkarounds, type: :feature
end
feature 'Import a record' do
scenario 'from JabRef' do
fill_in_plain 'textarea[name="bibtex"]', with: sample('jabref')
in_dialog.click_button 'Import'
...
end
end