How can I use copy and paste with protractor on MAC with Chrome?

All we need is an easy explanation of the problem, so here it is.

How can I use copy and paste with protractor on MAC with Chrome?

newInput.sendKeys(protractor.Key.chord(browser.controlKey, "a"));
newInput.sendKeys(protractor.Key.chord(browser.controlKey, "c"));
newInput.sendKeys(protractor.Key.chord(browser.controlKey, "v"));

I have “undefined” when I use this code

I use this code from this post Using cross-platform keyboard shortcuts in end-to-end testing but it doesn’t work:

browser.controlKey = protractor.Key.CONTROL; //browser.controlKey is     a global variable and can be accessed anywhere in the test specs
browser.getCapabilities().then(function(capabilities){
    if(capabilities.caps_.platform === "MAC")
        browser.controlKey = protractor.Key.COMMAND;
});

elm.sendKeys(protractor.Key.chord(browser.controlKey, "c"));

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

This is a known chromedriver problem. Unfortunately, sending keyboard shortcuts from Protractor/WebDriverJS is not going to work on Chrome+Mac.

In our project, we’ve moved all of the tests that involve using keyboard shortcuts to Firefox:

var firefox_only_specs = [
    "../specs/sometest1.spec.js",
    "../specs/sometest2.spec.js"
];

exports.config = {
    multiCapabilities: [
        {
            browserName: "chrome",
            chromeOptions: {
                args: ["incognito", "disable-extensions", "start-maximized"]
            },
            specs: [
                "../specs/*.spec.js"
            ],
            exclude: firefox_only_specs
        },
        {
            browserName: "firefox",
            specs: firefox_only_specs
        }
    ],

    // ...
}

Method 2

protractor.Key.COMMAND has been tagged as a wontfix in Protractor’s github for MAC. Here is a solution I adapted from keyboard commands for left-handed users

Universal copy+paste alternative working for MAC, LINUX, and WINDOWS:

// This does a select all
element1.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, protractor.Key.SHIFT, protractor.Key.HOME));

 // This copies the text
element1.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, protractor.Key.INSERT));

// This pastes it in another element
element2.sendKeys(protractor.Key.chord(protractor.Key.SHIFT, protractor.Key.INSERT));

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply