4

How can I copy a specific text with protractor ?

I would like to load a text to paste after with this command :

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

Sample :

Load my text "test" and after with this command, paste "test"

I would like put a text in my clipboard

Jérémie Chazelle
  • 1,721
  • 4
  • 32
  • 70
  • 1
    When you say specific text, is that text in a separate element or is it in one element and you are trying to extract it from that? Can you also update your question with the html element code that you are trying to perform operations on? Thanks – giri-sh Jan 19 '16 at 11:55
  • A text in my clipboard – Jérémie Chazelle Jan 19 '16 at 13:04

3 Answers3

7

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:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thx ! Also, can I put a value directly in my ng-model, not use sendKeys ? – Jérémie Chazelle Jan 19 '16 at 13:30
  • Thx ! Just last question : can I push my text with sendKeys instantly, I don't have the tapping effect, it's possible ? – Jérémie Chazelle Jan 19 '16 at 15:12
  • 1
    @JérémieChazelle well, if you are pasting from clipboard with command/ctrl + v, then yes..but with sendKeys I think it would always be one by one. Thanks. – alecxe Jan 19 '16 at 15:15
  • 1
    @alecxe Performing the paste operation pastes text as "mytext" rather than "my text", I need text with spaces/tab whatever specified in the string. Also sendKeys(protractor.Key.chord(browser.controlKey, "a")); wasn't working for me used newInput.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "a")); – Milan Kumar Nov 17 '16 at 10:50
  • In chrome it removes the spaces/tabs i.e "mytext" rather than "my text" and firefox it only pastes the first part of the string "my" – Milan Kumar Nov 17 '16 at 13:29
  • It seems that the code does not work on macOS. Does anyone have the same issue? – A.Chao Nov 12 '20 at 11:22
1

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.

Attila123
  • 932
  • 8
  • 8
0

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();
    });
  });
}
schwerwolf
  • 250
  • 1
  • 7
  • 20
  • Using ** "Copy-paste" ** 'var text = "Copied Text" require("copy-paste").copy(text); element(by.id('csv')).sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "v"));' – Milan Kumar Nov 18 '16 at 07:27