量角器 - 选择文本量角器、文本

2023-09-13 05:04:49 作者:见到本宫还不下跪

林具有使用选择量角器一些文字一些真正的麻烦。

Im having some real trouble with selecting some text using protractor.

一个小范围内;这是一个AngularJS CMS系统写新闻文章。我想强调的文本位于这是大多数网页的文本区域内。一个类似的应用是谷歌文档文件。

A little context; this is for an AngularJS CMS system for writing news articles. The text I want to highlight is located within a text area which is most of the page. A similar application is a Google Docs Document.

使用webdriver的,我相信我可以简单地用东西这种效果:

With webdriver, I believe I can simply use something to this effect:

browser.actions().keyDown(protractor.Key.CTRL).sendKeys('a').perform();

不过,我美元一个MAC C $ c和目前虽然我们的测试是在一个SauceLabs Windows中运行,最终目标是将一个MAC效仿我们的用户。

However, I code on a MAC and although currently our tests are run on a windows box in SauceLabs, the end goal is to move to a MAC to emulate our users.

我试图code,但与命令(或CMD),但它不工作的类似线,根据这个帖子,OSX不支持本机的关键事件。

I tried a similar line of code but with Command (or CMD) but it doesn't work, according to this post, OSX doesn't support native key events.

其他的方法我已经了解:

Other methods I've explored:

在试图元素三重单击以选中所有文字...但我不能得到这个工作(任何帮助吗?)。这是由鼠标光标是在文本为它突出显示所有的文字变得复杂。

Attempting a triple click in the element to select all text...but I couldn't get this to work (any help?). This is complicated by the fact that the mouse cursor has to be over the text for it to highlight all of the text.

而我的本地机器上的管理,以选择在文本区域的最后一个工作,但在SauceLabs,浏览器是小,因此设法选择不同的单词领域内双击。这感觉太脆使用,因为它会在大多数机器不同。

Double clicking inside the field which on my local machine manages to select the last work in the text area, but in SauceLabs, the browser is smaller so manages to select a different word. This feels too brittle to use as it would be different on most machines.

移动文本光标移动到上的Shift键和pressing基于字符的文本区域数左或右方向键的开头或文本区域结束,KEYDOWN。我无法将光标移动到文本字段的开始或结束在此实现。

Moving the text cursor to either the beginning or end of the text area, keydown on Shift and pressing the left or right arrow keys based on the number of character in the text area. I am having trouble moving the cursor to the start or end of the text field in this implementation.

感谢您的阅读,我意识到这是一个有点长个了!如果你能想到的我都没有考虑过的一种方法或一种方法,code中的三击或箭头键的方法,这将是非常有帮助的!

Thanks for reading, I realise this is a bit of a long one! If you can think of a method I haven't thought of yet or a way to code the triple click or the arrow keys method, that would be extremely helpful!

推荐答案

OK,我设法通过选择文本编程使用.executeScript方法来解决这个问题。

OK, I managed to get around this by selecting the text programatically using the .executeScript method.

这是一个有点作弊的,因为它不模仿用户的互动,但我无法找到一个替代,它被认为是一个可以接受的解决方法。

It's a bit of a cheat as it doesn't mimic a user's interaction but I couldn't find an alternative and it was deemed an acceptable work-around.

这里的code。如果你有兴趣,这将选择在文本区第一段:

Here's the code if you're interested, this will select the first paragraph in the text area:

Article.prototype.selectFirstParagraph = function(driver) {
    driver.executeScript(function () {
        var range = document.createRange();
        range.selectNode(document.body.querySelector('.ui-rich-text-editor__input').firstChild);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
        return sel;
    });
}