Copy-paste using Capybara?

不问归期 提交于 2019-12-11 12:53:20

问题


I would love to do something like this:

div = find '#some-div'
copy_to_clipboard(div)

input = find '#my-input'
paste_from_clipboard(input)

I do not want to simulate this with send_keys and using Ctrl+C and Ctrl+V; I want this to work cross-browser (especially on mobile).

Does this API exist?


回答1:


There is no Capybara copy/paste API - If all you want to do is copy the visible text into an input then you could do

div_text = find('#some-div').text()
find('#my-input').set(div_text)

If that's not correct for what you want, then you could use #execute_script to create a selection range like

var range = document.createRange();
range.setStart( <start node>, <start node character offset> );
range.setEnd( <end node>, <end node character offset> ); 

window.getSelection().removeAllRanges();
window.getSelection().addRange(range);

then find your target element and set it's value to window.getSelection().toString(). Note that's not really emulating what a user would do, so if you are actually using this for testing an app I would still recommend using the ctrl/cmd-c/v after setting the selection range for browsers that support it since it emulates user behavior better.




回答2:


It's an old one, however you don't need to use capybara however the workaround would to use this incredibly simple gem:

https://github.com/janlelis/clipboard




回答3:


There is no API to do it.

You can get element from one browser

div = page.find('#some-div')

Then you can pass it to another browser

fill_in '#some-other-div' with => div

You can read more about capybara here: https://github.com/jnicklas/capybara



来源:https://stackoverflow.com/questions/34816485/copy-paste-using-capybara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!