带有量角器 js 的剪贴板中的文本

Text in clipboard with protractor js

如何使用量角器复制特定文本?

我想加载一段文本,然后用这个命令粘贴:

return browser.actions().sendKeys(Keys.CONTROL, 'v').perform();

样本:

加载我的文本 "test",然后使用此命令粘贴 "test"

我想在我的剪贴板中添加一段文字

can I put a value directly in my ng-model, not use sendKeys ?

是的,您可以通过 .evaluate():

直接设置 model
var elm = element(by.model("mymodel.field"));
elm.evaluate("mymodel.field = 'test';");

正在将文本放入剪贴板

想法是使用现有的或动态创建一个 input 元素,您可以将文本发送到该元素,select 输入中的所有文本并使用 CTRL/COMMAND + C快捷方式。

样本:

var textToBeCopied = "my text";

// creating a new input element
browser.executeScript(function () {
    var el = document.createElement('input');
    el.setAttribute('id', 'customInput'); 

    document.getElementsByTagName('body')[0].appendChild(el);
});

// set the input value to a desired text
var newInput = $("#customInput");
newInput.sendKeys(textToBeCopied);

// select all and copy
newInput.sendKeys(protractor.Key.chord(browser.controlKey, "a"));
newInput.sendKeys(protractor.Key.chord(browser.controlKey, "c"));

其中 browser.controlKey 是 cross-platform 处理 CTRL/COMMAND 键的方式:

这是我使用量角器将文本复制到剪贴板的代码片段。我需要制表符才能被接受,因为我的大部分测试都涉及从电子表格中剪切和粘贴,其中制表符是默认的列分隔符。

此外,它更好地适应了 html 正文布局中的细微差别(溢出:隐藏)。

function copyToClipboard(browser, text) {
  var id = 'someCustomIdToAvoidAliasing';
  var newInput = browser.element(by.css("#" + id));
  return browser.executeScript(function () {
    var el = document.createElement('textarea');
    el.setAttribute('id', 'someCustomIdToAvoidAliasing');
    el.setAttribute('style', 'position:fixed;z-index:10000;top:0;left:0');
    el.onkeydown = function(e) {
      if (e.keyCode === 9) {
        this.value = this.value + "\t";
        return false;
      }
    };
    document.getElementsByTagName('body')[0].appendChild(el);
  })
  .then(function() {
    return newInput.sendKeys(text);
  })
  .then(function() {
    return newInput.sendKeys(protractor.Key.CONTROL, "a", protractor.Key.NULL);
  })
  .then(function() {
    return newInput.sendKeys(protractor.Key.CONTROL, "c", protractor.Key.NULL);
  })
  .then(function() {
    return browser.executeScript(function() {
      var el = document.getElementById('someCustomIdToAvoidAliasing');
      el.remove();
    });
  });
}

与此有点相关:我需要测试一个对话框,当用户按下对话框中的 'Copy' 按钮时,它会在剪贴板中放置一些文本。我想测试文本是否真的被复制到剪贴板。我找到了这个 'copy-paste' 库:https://www.npmjs.com/package/copy-paste。 "A command line utility that allows read/write (i.e copy/paste) access to the system clipboard. It does this by wrapping pbcopy/pbpaste (for OSX), xclip (for Linux and OpenBSD), and clip (for Windows)." 我会说它是一个 javascript 模块而不是命令行实用程序。无论如何,我开始使用它,因为对 xclip(Linux)的依赖对我来说不是问题。