Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable clipboard in automated tests with Protractor and webdriver

I want to test with Protractor an Angular application that accesses the system clipboard using the clipboard api. The problem is that, when executing the test, the Chromium browser asks the user for permission to access the clipboard. I can't close this box from the test, because this is not a DOM element nor an alert window, and it seems not to be any way to access it from Protractor.

clipboard permissions

I have tried to automatically give permission in Chrome, in protractor setup. First, I looked for a command line switch that do it, but it seems not to exist.

Then I found a way that apparently works, but I don't know if its stable: to set an "exception" in Chrome user profile, using the Capabilities of the chrome driver of webdriver. Basically, I add the following lines of my protractor config file:

  capabilities: {
    'browserName': 'chrome',
    'chromeOptions': {
      'prefs': {
        'profile.content_settings.exceptions.clipboard': {
          'http://localhost:8000,*': {'last_modified': Date.now(), 'setting': 1}
        }
      }
    }
  }

I have found the value by looking inside Default/Preferences file inside my Chrome profile.

It now works for me, but I don't want to use undocumented features, since they can change anytime without notice. Do you know a better way of doing this? Thanks.

like image 864
hirunatan Avatar asked Dec 07 '18 12:12

hirunatan


People also ask

How do I find clipboard in selenium?

Launch the Selenium IDE, hover your mouse over the Clipboard Format option from the Options menu, and select the preferred combination format.

How do I copy and paste text in selenium?

Copy & Paste Text: When we need to copy some text from one text box to another, we select the text by pressing "CTRL+A" they copy the text using "CTRL+C" and paste the text in the new text box by simply clicking in the text box and pressing keys "CTRL+V".


1 Answers

If anyone is looking to do something similar with C#, I have adapted the code as such:

var options = new ChromeOptions();
var clipboardException = new Dictionary<string, object> {
  {"[*.]myurl.com,*", 
    new Dictionary<string, object> {
      {"last_modified", DateTimeOffset.Now.ToUnixTimeMilliseconds()}, 
      {"setting", 1}
    }
  }
};          
options.AddUserProfilePreference("profile.content_settings.exceptions.clipboard", clipboardException);
like image 79
Max Eisenhardt Avatar answered Oct 27 '22 23:10

Max Eisenhardt