【发布时间】:2019-09-20 19:23:45
【问题描述】:
我的前端 UI 中有一个“复制链接”按钮。单击时,输入框内的 URL 会使用此 JS 复制到用户的剪贴板:
const copyTextarea = document.querySelector("#copy-link-button");
copyTextarea.focus();
copyTextarea.select();
document.execCommand('copy');
当我在本地试用时,此功能完美运行,因此我知道该功能本身运行正常。
但是,我无法使用 Capybara 测试副本。我从this post 知道 Capybara 不提供剪贴板 API,但我的解决方法是 -
- 使用“复制链接”按钮复制链接
- 导航到其他一些输入/文本字段
- 使用 CTRL+V 粘贴到字段中并读取字段内容进行验证
我的测试:
# Copy the link
page.find("#copy-link-button").click
wait_for_ajax
# Visit some other page that I know has an input/text field
visit account_settings_path
input = page.find("#user_email")
# Clear the field
fill_in("user[email]", with: "")
# Paste in the contents of the clipboard
input.base.send_keys([:control, "v"])
# Validate
expect(input.value).to eq("some value");
但是没有任何内容粘贴到该输入中(input.value 和 input.text 返回 "")。
这是一种有效的方法吗?首先是复制文本的问题还是我在粘贴数据时出错了?
谢谢!
【问题讨论】: