Search code examples
c#seleniumxpathreturn-valuereturn-type

Copy and paste the link from clipboard in selenium C#


I'm having the following issue:
I'm saving the locators as

public static By CopyUrl =  By.XPath("//a[@data-key='UrlLink']");

In the test, I cannot GetText(), or store the whole CopyUrl in a var, since it's void. Driver.Click(XPath.MainMenu.Tabs.CopyUrl);

How can I store the link which is saved in clipboard after I click on it, and paste it in a new tab.

I tried:

var elem =Driver.SwitchTo().NewWindow(WindowType.Tab).Navigate().GoToUrl("myCopiedUrl").ToString();

Driver.SwitchTo().NewWindow(WindowType.Tab).Navigate().GoToUrl(elem);

Solution

  •  [Test, Order(01234)]
        public void t_Document_CopyUrl_Downloaded()
        {
            NavigateToYourPage();
            UploadNewDocument();
            NavigateToTheSpecificTab();
            
            //Click on "Copy URL"
            string fileName = Driver.FindElement(XPath.Input).GetValue();
            IWebElement copyUrl = Driver.FindElement(XPath.CopyUrl);
            string currentUrl = copyUrl.GetAttribute("textContent");
            Driver.Click(XPath.CopyUrl);
            Driver.WaitForNoSpinner();
    
            // Opens a new tab and switches to new tab
            Driver.SwitchTo().NewWindow(WindowType.Tab);
            Driver.Navigate().GoToUrl(currentUrl);
    
    
            Driver.Navigate().GoToUrl("chrome://downloads/");
            string downloadedFileName = Driver.ExecuteJavaScript<string>("return document.querySelector('downloads-manager').shadowRoot.querySelector('downloads-item').shadowRoot.querySelector('#name').textContent;");
            Driver.SwitchTo().Window(Driver.WindowHandles[0]);
    
            Assert.IsTrue(downloadedFileName.StartsWith(fileName));
    
            
        }