Hi Team,
We met one issue, for click copy button and get clip text, with telerik 2024, chrome could work well, but chrome headless couldn't work well, the text is always empty.
The code we use is like this::
Clipboard.Clear();
copybutton.click();
Clipboard.GetText();
Can you give some suggestion about this?'
Thanks very much,
Jun
Hi Jun,
Please, prepare a sample test to demonstrate the misbehavior you see in headless execution and share it along.
Regards,
Elena
HI Elena,
Below is a sample test, which could run pass directly in chrome but fail in chromeheadless. Please help to check.
using ArtOfTest.WebAii.Controls.HtmlControls;
using ArtOfTest.WebAii.Core;
using System.Windows.Forms;
// Initialize the settings
Settings mySettings mySettings = new Settings();
// Set the default browser
mySettings.Web.DefaultBrowser = BrowserType.EdgeChromiumHeadless;
// Create the manager object
private static Manager myManager = new Manager(mySettings);
// Start the manager
myManager.Start();
// Launch a new browser instance. [This will launch an IE instance given the setting above]
myManager.LaunchNewBrowser();
// Navigate to a certain web page
myManager.ActiveBrowser.NavigateTo("http://www.google.com");
// Perform your automation actions.
HtmlTextArea searchText = myManager.ActiveBrowser.Find.ByTagIndex<HtmlTextArea>("textarea", 0);
searchText.Text = "hello";
Clipboard.Clear();
searchText.MouseClick();
myManager.ActiveBrowser.Desktop.KeyBoard.KeyPress(Keys.Control | Keys.A);
myManager.ActiveBrowser.Desktop.KeyBoard.KeyPress(Keys.Control | Keys.C);
var clipboardText = Clipboard.GetText(); //shoud not be empty
Thanks,
Jun
Hi Jun,
Thank you for the provided sample test. In this particular case you should take into consideration the fact that Chrome headless uses its own user data folder. Therefore the initial prompt regarding browser cookie use could cover the application and should be handled first. Additionally, System.Windows.Forms.Clipboard API could not work as expected during headless execution and I would recommend using keyboard combinations like Ctrl+C and Ctrl+V.
Here is a modified version of your sample which demonstrates this approach.
using ArtOfTest.Common.UnitTesting; using ArtOfTest.WebAii.Core; using ArtOfTest.WebAii.Controls.HtmlControls; using System.Windows.Forms; using System.Linq; namespace TestProject { public class TestClass { public void TestMethod() { // Initialize the settings Settings mySettings = new Settings(); // Set the default browser mySettings.Web.DefaultBrowser = BrowserType.EdgeChromiumHeadless; // Create the manager object var myManager = new Manager(mySettings); // Start the manager myManager.Start(); // Launch a new browser instance. myManager.LaunchNewBrowser(); // Clear browser cookies myManager.ActiveBrowser.ClearCache(BrowserCacheType.Cookies); // Navigate to a certain web page myManager.ActiveBrowser.NavigateTo("http://www.google.com"); // Wait for the prompt to appear System.Threading.Thread.Sleep(5000); // Close cookies prompt var closeCookies = myManager.ActiveBrowser.Find.AllByContent<HtmlDiv>("Reject all").FirstOrDefault(); if (closeCookies != null) { closeCookies.MouseClick(); } // Perform your automation actions. HtmlTextArea searchText = myManager.ActiveBrowser.Find.ByTagIndex<HtmlTextArea>("textarea", 0); searchText.Text = "hello"; searchText.MouseClick(); myManager.ActiveBrowser.Desktop.KeyBoard.KeyPress(Keys.Control | Keys.A); myManager.ActiveBrowser.Desktop.KeyBoard.KeyPress(Keys.Control | Keys.C); System.Threading.Thread.Sleep(1000); myManager.ActiveBrowser.Desktop.KeyBoard.KeyPress(Keys.Delete); System.Threading.Thread.Sleep(1000); Assert.AreEqual("", searchText.Text); myManager.ActiveBrowser.Desktop.KeyBoard.KeyPress(Keys.Control | Keys.V); Assert.AreEqual("hello", searchText.Text); } } }
Regards,
Konstantin
Hi Konstantin,
Thank you for the feedback.
Actually if we just want to copy and paste, we can use Control + C and Control +V, but some scenarios. we need to click the copy button of our application, and then check if the clipboard text is expected.
I will try if I can use Control + V to workaround and feedback you later.
Still want to know, can you please help to confirm if there's any other function I can use or if telerik will try to support System.Windows.Forms.Clipboard in the future?
Thanks,
Jun
Hello Jun,
Thank you for your cooperation. I hope that the workaround with keyboard combination works for you. You can use it to paste the contents of the clipboard to a text box and verify its value.
Additionally, in case your scenario and environment allow it, you can use normal Chrome without extension just for the tests which require access to the System.Windows.Forms.Clipboard.
Please let me know if you need any further assistance.
Regards,
Konstantin
Hi Konstantin,
Yes, after I use control + v to verify clipboard value, my case could work well for chromeheadless, and for chrome, we still useSystem.Windows.Forms.Clipboard. Thanks for your suggestion, now our cases will not block by this.
Thanks,
Jun
Hi Jun,
It is nice to hear that my suggestion was helpful and you can continue your work.
Feel free to reach out if you need any additional help.
Regards,
Konstantin