Text in clipboard with protractor js

大兔子大兔子 提交于 2019-12-05 15:19:22
alecxe

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

Yes, you can directly set the model value via .evaluate():

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

Putting a text into clipboard

The idea is to use an existing or dynamically create an input element where you would send the text to, select all the text in the input and copy it with a CTRL/COMMAND + C shortcut.

Sample:

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"));

where browser.controlKey is a cross-platform way to handle CTRL/COMMAND keys:

Somewhat related to this: I needed to test a dialog, which placed some text in the clipboard when the user pressed the 'Copy' button in the dialog. I wanted to test that the text really got copied to the clipboard. I found this 'copy-paste' library: 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)." I would say it is a javascript module rather than a command line utility. Anyway, I started using it, as the depenency on xclip (for Linux) was not a problem for me.

Here's the snippet I use with protractor to copy text to the clipboard. I need tabs to be accepted because most of my testing involves cut-and-paste from spreadsheets, where tab is the default column delimiter.

Further, it accommodates nuances in the html body's layout better (overflow: hidden).

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