0

I need to copy url and paste it to browser address bar. Unfortunately when I copy url there is no data attribute from where I can getText and paste.

I used actions class as below to paste the url. But doesn't seem to work.

HMTL code:

  <li class="copyLink">
  <span class="link">Copy link</span>
  <input class="input" readonly="">
 </li> 

await browser.executeScript("window.open(arguments[0], '_blank')"); // opens new tab await browser.actions().keyUp(protractor.Key.CONTROL).perform();//to paste in the address bar await browser.sleep(1000);

Any suggestions on whats wrong with the code ?

Thanks

asl
  • 77
  • 1
  • 2
  • 11
  • Can you please elaborate your questions.I don't seem to understand your question properly – Cpt Kitkat Jan 31 '19 at 12:56
  • Are you sure the URL is copied correctly and is the new tab opening as expected? – DublinDev Jan 31 '19 at 13:12
  • I am trying to paste copied URL from the clipboard to browser address bar. Tried using browser.executescript to open new. Later tried actions class to paste the url from clipboard which deosn't seem to work. Yes , I see the tab opening. But the paste doesn't work. Looking for suggestions – asl Jan 31 '19 at 13:34
  • FYI , I have added the HTML code. – asl Jan 31 '19 at 13:40

1 Answers1

0

UPDATED CODE AGAIN:

You were very close in your initial attempt. Using the sendKeys method you can send the current copied code in the clipboard to the current selected element using

await browser.actions().sendKeys(protractor.Key.CONTROL, 'v').perform();

You were missing the 'v' which, as I'm sure your aware, is the button you need on the keyboard after the control button is pressed.

This is where you can read more about sending specific key presses with Protractor

Latest Update:

If you address bar is not focused then the above approach will not work and I have not found a way to manually focus the address bar at this time. Another approach you could try is to use an npm package like clipboardy. This will allow you to copy to clipboard contents to a variable and then use a browser.get() to reach the required URL.

Install clipboardy package

npm i clipboardy

In your test

const clipboardy = require('clipboardy');

//create variable with value from clipboard
let urlFromClipboard= await clipboardy.read();

await browser.get(urlFromClipboard);
DublinDev
  • 2,318
  • 2
  • 8
  • 31
  • Thank you . But I dont want to copy the current URL. When I click on something called "Copy URL" its gets copied to the clipboard. I am unsure on how to paste the clipboard text to the address bar – asl Feb 01 '19 at 08:44
  • Updated the answer, let me know if it does not solve your issue – DublinDev Feb 01 '19 at 12:04
  • Thanks for correcting. Looks like I missed adding 'v'. Well , however even after adding the right code , I am still unable to get it to work. `await browser.executeScript("window.open(arguments[0], '_blank')"); browser.getAllWindowHandles().then(function(handles) { browser.switchTo().window(handles[1]).then(function() { browser.actions().sendKeys(protractor.Key.CONTROL, 'v')).perform(); browser.actions() .sendKeys(protractor.Key.chord(protractor.Key.ENTER)) .perform(); browser.sleep(1000); }); });` – asl Feb 04 '19 at 14:43
  • I had a feeling that the URL may not be focused. Edited my answer, could you attempt the new approach and let me know if it is suitable? – DublinDev Feb 04 '19 at 15:10
  • Ah glad to hear it! Would you mind accepting this answer in case anyone else find this question with a similar issue? – DublinDev Feb 05 '19 at 09:15
  • 1
    Done. Thank you so much for the help – asl Feb 05 '19 at 14:45