Jérémie Chazelle Jérémie Chazelle - 2 years ago 1023
Javascript Question

Text in clipboard with protractor js

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

Answer Source

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.


var textToBeCopied = "my text";

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


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

// 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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download