I am working on a test for a Web Application. I want to simulate the Copy command and verify the value of the Clipboard.
I have two ways to simulate this:
I simulate "Ctrl+C" by using this code:
System.Windows.Forms.SendKeys.SendWait("^{c}");
I use a button on my App which executes Copy on some text and puts it on the Clipboard
These two work and after using one of them, I can do "Ctrl+V" and it pastes the text correctly.
On my test, I am supposed to verify that the Clipboard contains the correct value.
I am using this code to check if the Clipboard is not empty and that it contains the correct string:
Clipboard.ContainsText(); // verify that Clipboard is not empty
Clipboard.GetText(); // verify that string on the Clipboard contains the good string
But after I simulate a Copy (with one of options above), the code just above returns respectively:
false
""
Does anyone have a solution to fill the Clipboard and to see its contents?
Ok I added this on the attribute of my test :
[Apartment(ApartmentState.STA)]
I can access the value of the Clipboard now.
(Source: https://docs.nunit.org/articles/nunit/writing-tests/attributes/apartment.html)
You can use Clipboard.SetText("your text here") to manually set text to the clipboard.
Afterwards, you can use Clipboard.ContainsText() and Clipboard.GetText() to check the clipboard contents as you mentioned above.
See the documentation here.
Related
I need to check pictures extension when user has chosen picture and try to upload it into picture library.
I've found the way to edit master page with js script, but i haven't to edit master page. Then i try to use event receiver Adding, but it can't get the name of file or file path. I used:
var file = properties.ListItem.File.Name; //properties.ListItem - returns null
AfterProperties also returns null.
The another way i see is to edit Adding Picture form with js:
I think is's simplest way but i can't found information about it.
Problem: how to set js script to the form (see picture) or how to do such actions with another way
It can be realized with event receiver ItemAdding and with
properties.AfterUrl
The same answered question with code in:
Customizing upload file functionality in SharePoint picture library
In your case, a simple JS form validation will do.
First I would prevent default action of the submit button.
Then, I would analyze the contents of the filename textbox and check the extention. If it's not one of the listed in the validation script, return false + alert "THis file extention is not supported".
Why would you want to use event receiver? It catches the event on server side.
I'm creating a program that allows you to manage a game server, now the main settings of it exist in a file called server.properties. I'm making a form which is able to change these settings. However, I want to be able to load the current settings into different things.
For example, if a line is spawn-npcs=true in the server.properties file, I want to be able to apply that to a CheckBox and make the CheckBox Checked. Or if something uses numbers then a TextBox would need to contain that so if a line is server-name=Funhuas in the server.properties file a certain TextBox would need to only read Funhaus.
I am using this code to read the lines I want:
string line = File.ReadLines("server.properties").Skip(14).Take(1).First();
And I am using this code to change the value of that line:
var lines = File.ReadAllLines("server.properties");
lines[6] = "spawn-npcs=" + textBox1.Text;
File.WriteAllLines("server.properties", lines);
The code above seemingly works fine however. Also, all this data will be entered when the user presses a Save button. Hope I've provided enough info, thanks!
I have a small program that is listening to clipboard(hook) for images. If there is an image stored or copied through ctrl+c and the like, my program will paste the image automatically to an open word doc.
Code:
if (Clipboard.ContainsImage())
{
IDataObject obj = Clipboard.GetDataObject();
if (obj.GetDataPresent(DataFormats.Bitmap))
{
Microsoft.Office.Interop.Word.Range range = thefile.Bookmarks.get_Item(#"\endofdoc").Range;
range.Paste();
newdoc.Selection.EndKey(WdUnits.wdStory);
}
}
I just don't under stand why there are text from a certain application being copied to doc where in fact I'm only looking for bitmap data formats (based on my code). Is there a way to check if the data being copied is really an image or a text? I have no problem doing printscreen but every time I'll copy a text from a certain application (which I believe after doing ctrl+c is now stored in clipboard), the text is being considered an image?
I think the reason is that Clipboard can contain various format data, such as text and bitmap, at the same time.
So, when you run range.Paste() and Clipboard contains both text and bitmap data, text data will be copied.
To avoid this unexpected behavior, you need to get image data only from Clipboard by calling Clipboard.GetImage() and then paste it to your word doc.
Unfortunately I couldn't find any easy way to do that, but this answer might be helpful (i.e. write Clipboard image data to a file and then call Shapes.AddPicture to show the image in the word doc).
I have a asp.net form with 5 HTML file input controls with runat=server and a submit button. after user selects the files and clicks the submit button, the files are upload in the server.
Problem is the HTML file input controls are editable, and user can edit the path after he has already selected the file from the browse button.
If he enters a invalid file path, the file is not uploaded because it does not exist.
How can I stop users from manually changing the file path? I have tried to make the controls read only, but it disables the browse button also.
You can't. Write your server side logic to cope with missing uploads.
you could try something like having a hidden field and when the user selects the file from browse, it populates to the hidden field as well. Then when they upload, you either read from the hidden field for your upload or using JS replace the values back to the original control and read from that.
Is this what you want to have...
You can do like that hiding the control when user select the file
and showing only the path to the user
in a div
if he disagree, let him click on a link called Choose Another next to the path div, so when user click Choose another toggle the same div with flushing the previous value
see this fiddle : http://jsfiddle.net/T2D3X/
While brian's suggestion works (Thank you brian for this), finally we decided to follow the server side handling approach. since in case of invalid file, the file size would be zero, so the response is instant and we do not need to wait till the file is uploaded because in this case there is not file in the first place.
//check if file exists
if (uploadCtrl.PostedFile.ContentLength > 0)
{
uploadCtrl.PostedFile.SaveAs(fileNameWithPath);
uploadCtrl.Dispose();
}
else
{
fileNameWithPath = "invalid";
}
You can also use these methods:
One before validating: http://jsfiddle.net/T2D3X/1/
One After validating: http://jsfiddle.net/T2D3X/2/
Friend of mine posted the same answer before I have re-modified it.
There are some internal jsfiddle error on second link you can ignore that, i have just added more functionally to check on submit what value exactly you are sending.
Let us know if you find the it helpful ;)
Thank!
i wrote a small application that will monitor the clipboard and paste text directly in a webbrowser component.
...
DocumentWysiwyg = ClipBoardWebBrowser.Document.DomDocument as IHTMLDocument2;
DocumentWysiwyg.designMode = "On";
...
and when the Clipboard changes, i paste the Clipboard content into the webbrowser using:
ClipBoardWebBrowser.Document.Write(Clipboard.GetText(TextDataFormat.Html));
now, when pasting any copied content from web as html, i get inside the html
span class="Apple-converted-space">Â </span
which does not belong to the html i copied from a website.
what are those? and how can i get rid of them?
any help is really appreciated .
here is the html code for google.de as example
http://pastie.org/pastes/3706386/text
how would i make sure that the pasted clipboard is exactly the same as the copied data in the clipboard?
On Mac multiple occurrences of a regular space " " get converted such that they replace every other regular space with a non-breaking space character aka This allows the spaces to still break on every other character, but preserve such ranges of spaces.
The reason for this is because without this trick HTML would compress multiple spaces to a single one. Having only characters would disable line wrapping, because as their name states they would be non-breaking.
In addition to "Apple-converted-space" there was also "Apple-style-span" but that was eliminated from Webkit in 2011: https://www.webkit.org/blog/1737/apple-style-span-is-gone/
So to answer your question: since Webkit is filling the pasteboard you cannot do anything to prevent such behavior.