Selenium C#: How to check/assert copied text? (Net6) - c#

On my website, I can click on a button, which does copy text. In the screenshot below this is how it looks like:
After clicking on the copy text, it should copy the text Bitte einschalten und ausschalten äüöß 876543212345678 which can be seen on the bottom of the picture.
Now I would like to check, if the copy function worked and if the text on the webpage, is the same as the one which has been copied.
I was already searching for answers, but nothing worked for me.
My idea was to paste the text into the browser's URL field and then compare it, but it seems that selenium is not able to access this URL field.
There is also no other text field where I could paste the text inside.
Would appreciate any help!
EDIT: There was a method in C# which doesnt work in .NET Core anymore.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.clipboard?view=windowsdesktop-6.0
Looking for a other solution like this.

With a bit of JavaScript, you might be able to do this:
Create a text field using JavaScript.
Get the value of the text field.
var js = (IJavaScriptExecutor)driver;
var textareaId = DateTime.Now.Ticks.ToString();
var script = $#"
var textarea = document.createElement('textarea');
textarea.id = '{textareaId}';
document.body.appendChild(textarea);";
js.ExecuteScript(script);
var textarea = driver.FindElement(By.Id(textareaId));
// Paste clipboard into textarea
textarea.SendKeys(Keys.CONTROL + "v");
var clipboardText = textarea.GetProperty("value");
Just be aware of where this text area gets appends to the page. If it is hidden or partially obstructed by another element, you will get an ElementNotInteractableException. If this happens, pick a different element on the page when appending the textarea. Just replace document.body.appendChild(textarea) with a reference to a different element.
For example: document.getElementById('someOtherElementId').appendChild(textarea)

Related

Making a file with full path suitable to act as an href in the HTML

I have a piece of code that is like
string.Format("<a href='{0}' class='hidden'>{0}</a>", Path.Combine(filePathPrefix , string.Join("-", new string[] { fOrgName, fCatName, f.filename })))
and meant to return an a tag that a user can click on to get a file. The only problem is that string I'm building for the href does not work as a link. It is something like
c:\users\me\documents\visual studio 2013\Projects\myproj\myproj\Assets\someorg-somecat-somepic.png
which doesn't work as a clickable link, brings to an about:blank page if you click on it. However, if I paste the link into my browser and make the request then it changes to
file:///C:/users/me/documents/visual%20studio%202013/Projects/mypoj/myproj/Assets/someorg-somecat-somepic.png
and brings up the asset properly. That means I need some way getting the link in that form while its on the page. Is this possible? If so, what C# class(es) and method(s) do I need?
Try changing to this:
string.Format("{0}", ...);

Other text input methods?

There are a bunch of websites with "search" input boxes.
If I input a word manually then it automatically search without need to click any buttons.
Using the following code:
GeckoInputElement input = (GeckoInputElement)geckoHtmlElement;
input.Value = "searchword";
The I see that search input box is filled but nothing happens automatically.
If I manually add space or any character then website works as expected.
Auto searches my wanted word.
I tried using input.Focus(); but still same.
Any ideas how I can input text into search box in a more advanced way or something like that?
I think the solution here is to manually trigger the event that triggers the search - for example the keyup event. I had the same problem which I solved by adding this code:
nsAStringBase eventType = (nsAStringBase)new nsAString("keyup");
var ev = browser.Document.CreateEvent("HTMLEvents");
ev.DomEvent.InitEvent(eventType, false, false);
nameBox.GetEventTarget().DispatchEvent(ev);
You would need to inspect your page and see if there are any javascript events attached to your input.
Good luck!

How to make sure that my <br> tag get read as a html and not as text

I have a asp.net page. On one particular page i can add comments and when i add these comments they get saved to a db. When i want new lines the "\n" tagg is automaticly displayed when i look at it in my backend code.
So now when i get back a response i want to get my comments back but i want them to have breaking spaces on those particular places where the "\n" is displayed.
I tried this in my backend code(cs):
Text = text.replace("\n", "<br/>")
the result i get:
my test<br/> test text new line<br/>
My <br> tags get displayed as text.
To clarify i basicly wan to achieve this:
Why are my "<br />" tags getting converted to "<br />"?
But from the backend code in C#.
Ive been googling and haven't been able to find a answer to this.
If you are using ASP.NET WebForms and you are putting the text in a Label control, this is normal behaviour. The Label control html-encodes its text before rendering it.
The Literal control does not use html-encoding, so if you change your Label to a Literal, it should work.
If you are using ASP.NET MVC, try using #Html.Raw(Model.Text).
You need to create an HtmlString with your content before printing it. So your code would be like
var foo = new HtmlString(text.replace("\n", "<br/>"));
and you would print foo.

C# jumping some textfields like searchboxes

I'm just using watin to fill some textfields but sometimes it writes in the wrong textfield because the textfields name is not clear here's my code
IE browser = new IE(site);
browser.TextField(Find.By("type","text")).TypeTextQuickly(username.ToString());
browser.TextField(Find.By("type", //"password")).TypeTextQuickly(pass.ToString());
browser.Button(Find.By("type", "submit")).Click();
Edit your HTML code and setup ID´s for your input elements. Then use Find.ById
One option could be to see if there is an outer element (such as a Div) you can find first, and afterwards get the text fields from that element instead of from the browser variable. That could for instance look like this:
Div div = browser.Div(Find.ById("divId"));
//Div div = browser.Div(Find.ByClass("divClass")); // or like this for instance...
TextField text = div.TextField(Find.By("type", "text"));
TextField password = div.TextField(Find.By("type", "password"));
Button submit = div.Button(Find.By("type", "submit"));
I am unclear as to your knowledge of WatiN and testing so I will start from the begginging. First you need to go on the webpage you want to test and (in IE) go to tools -> Developer tools. Click the white arrow in the menu then proceed to click the box you wish to utilize. Once you do this the developer tools will give you the code for the textbox including many ways you can reference it in your code. For example, using dev tools in IE I can automate logging into my gmail like this:
IE browser = new IE("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=http://mail.google.com/mail/&scc=1&ltmpl=default&ltmplcache=2");
browser.TextField(Find.ByID("Email")).TypeText("myemail#email.com");
browser.TextField(Find.ByID("Passwd")).TypeText("mypassword");
browser.Button(Find.ByID("signIn")).Click();
There are many Find.By commands so there is no reason you can't acess ANY textfield you wish. You just need the html and to be specific to which one you want to write into. I hope this helps :)

ASP.Net Hyperlink navigation URL not including server name

This might be a ridiculously easy question, but it has me stumped. I have a web form where I'm trying to create a hyperlink in the code behind to a file server share, e.g. file://myServer/Shared/, but when the page is rendered, the link doesn't include the server name, i.e. file:///Shared/. I don't know why this happens. Any help or insight is appreciated.
UPDATE:
Sure, here is the snippet where the link is being set.
//The link is embedded in a table
HyperLink link = (HyperLink)e.Row.Cells[1].Controls[0];
link.NavigateUrl = #"file://myServer/Shared/";
As a test, I assigned the link to a string value and the link prints the expected url.
string foo = link.NavigateUrl;
//Displays this
"file://myServer/Shared/"
I don't know why this doesn't appear when the link is rendered in the final page.
UPDATE 2:
Ok, so I know I have to set the absolute path in the code-behind, I thought that's what I was doing, but it still won't render correctly.
UPDATE 3:
I followed pjacobs suggestion about setting the test property and it was actually a step in the right direction. I have the following:
link.Text = "link text";
Now the link gets rendered as follows: file:///myServer/Shared. I'm almost there except it gives the extra '/' in front of the server name. I'll keep playing with it, this seems like it should be so simple, I don't understand why ASP.Net renders the URL differently.
You have to set the Text property of the HyperLink... link.Text = "whatever"
Are the resources inside the project? If so:
you need to use ResolveUrl to resolve the "web location" of the resource.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx
if you're using an asp.net control you shouldn't need to use the resolve url, but you need to refer to the location of the file relative to the path of the project.
If not:
Did you give the proper read account to ASP.NET process?
Use a virtual directory?
http://www.dotnetspider.com/tutorials/AspNet-Tutorial-86.aspx

Categories