黄瓜 +水豚 + Selenium:选择文本

发布于 2024-10-08 12:07:05 字数 110 浏览 0 评论 0原文

我正在对文本编辑器进行更改,并且需要能够选择文本以使用 JavaScript 对其进行操作。 如何使用 Cucumber、Capybara 和 Selenium 选择文本?

I'm making changes to a text editor, and I need to be able to select text to manipulate it with JavaScript. How do I select text with Cucumber, Capybara and Selenium?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀念你的温柔 2024-10-15 12:07:05

我发现了另一个 stackoverflow 问题,讨论如何使用 JavaScript 选择文本。

您可以设置和/或更改用户在 JavaScript 中选择的文本?

我能够修改他们的脚本,以便能够在 Selenium IDE 的 getEval 调用中工作(如果您以不同的方式使用 Selenium,您可能需要修改它)。我还删除了与 Firefox 不相关的代码:

function selectElementContents(el,start,end) {
    var sel = window.getSelection();
    var range = window.document.createRange();
    range.setStart(el,start);
    range.setEnd(el,end);
    sel.removeAllRanges();
    sel.addRange(range);
}
selectElementContents(window.document.getElementById("container").firstChild,10,20)

这允许您在单个元素中选择一系列文本。 “Container”是包含文本的元素的 ID,10 和 20 是开始和结束字符的索引。

只需将其压缩为一行,将其添加到 Selenium getEval 语句中,它就会为您选择文本。

如果单个元素中的文本对您来说不够灵活,JavaScript 范围的文档应该为您提供所需的额外详细信息。

I found another stackoverflow question that talks about how to select text using JavaScript.

Can you set and/or change the user’s text selection in JavaScript?

I was able to modify their script to be able to work within a getEval call from Selenium IDE (if you're using Selenium in a different way, you may need to modify it). I also stripped out the code not pertinent to Firefox:

function selectElementContents(el,start,end) {
    var sel = window.getSelection();
    var range = window.document.createRange();
    range.setStart(el,start);
    range.setEnd(el,end);
    sel.removeAllRanges();
    sel.addRange(range);
}
selectElementContents(window.document.getElementById("container").firstChild,10,20)

This allows you to select a range of text within a single element. "Container" is the ID of the element containing the text, and 10 and 20 are the indexes of characters to start and stop at.

Just condense this down to one line, add it to a Selenium getEval statement, and it should select the text for you.

If text within a single element isn't flexible enough for you, documentation on JavaScript ranges should provide you the extra detail you need.

你的心境我的脸 2024-10-15 12:07:05

将 Josh 建议的方法绑定到 Capybara 辅助函数中(并为开始/结束元素中的部分选择添加可选位置参数):

module TextSelection
  def select_text(startEl, endEl, startPos = 0, endPos = 0)
    js = <<-JS
      var sel = window.getSelection();
      var range = window.document.createRange();
      range.setStart(arguments[0],#{startPos});
      range.setEnd(arguments[1],#{endPos});
      sel.removeAllRanges();
      sel.addRange(range);
    JS

    page.execute_script(js, startEl.native, endEl.native)
  end
end

World(TextSelection)

然后,在您的测试中:

start_el = first('#first')
end_el = first('#last')
select_text(start_el, end_el)

Tying the approach suggested by Josh into a Capybara helper function (and adding optional position arguments for partial selections within the start/end elements):

module TextSelection
  def select_text(startEl, endEl, startPos = 0, endPos = 0)
    js = <<-JS
      var sel = window.getSelection();
      var range = window.document.createRange();
      range.setStart(arguments[0],#{startPos});
      range.setEnd(arguments[1],#{endPos});
      sel.removeAllRanges();
      sel.addRange(range);
    JS

    page.execute_script(js, startEl.native, endEl.native)
  end
end

World(TextSelection)

Then, in your test:

start_el = first('#first')
end_el = first('#last')
select_text(start_el, end_el)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文