This is a migrated thread and some comments may be shown as answers.

Validating clipboard content

4 Answers 55 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 10 Aug 2012, 07:26 PM
Hey guys,

Anyone know how to validate that the clipboard content is equal to a particular string?

Thanks
Jon

4 Answers, 1 is accepted

Sort by
0
Anthony
Telerik team
answered on 13 Aug 2012, 03:18 PM
Hello Jon,

Use the Clipboard Class. This is basic .NET code and not specific to Test Studio.

Kind regards,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Jon
Top achievements
Rank 1
answered on 13 Aug 2012, 09:30 PM
Hi Anthony

The clipboard class doesn't work. I can only assume this is because the content is running in a MTU state, the clipboard function must be run in a STA thread. 

I did some research and found that the following code snippet should work, but doesn't in Test Studio...

//COM Voodoo
            IDataObject idat = null;
            Exception threadEx = null;
            Thread staThread = new Thread(new ParameterizedThreadStart(
                delegate
                {
                    Console.WriteLine("Thread Started");
                    try
                    {
                        Console.WriteLine("Grabbing IdataObj");
                        idat = Clipboard.GetDataObject();
                    }
 
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception caught");
                        threadEx = ex;
                    }
                }));
            staThread.SetApartmentState(ApartmentState.STA);
            Console.WriteLine("Starting Thread");
            Console.WriteLine("Apartment State: {0}", staThread.GetApartmentState().ToString());
            staThread.Start();
            staThread.Join();
 
 
            var content = idat.GetData(DataFormats.Text);
            Console.WriteLine("Clipboard contents: {0}", content);
            Console.WriteLine("Press Any Key...");
            Console.ReadKey(false);

If you could take another look at this I would greatly appreciate it!

Thanks Jon
0
Accepted
Anthony
Telerik team
answered on 14 Aug 2012, 02:58 PM
Hello Jon,

The following code works in our testing:

public string ClipboardMethod()
{
    string content = Clipboard.GetText();
    return content;
}
     
[CodedStep(@"New Coded Step")]
public void ClipboardTest_CodedStep()
{
    string content = string.Empty;
     
    var thread = new Thread(obj =>
    {
        content = this.ClipboardMethod();
    });
     
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
     
    Log.WriteLine(content);
}


Kind regards,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Jon
Top achievements
Rank 1
answered on 14 Aug 2012, 04:01 PM
Thanks Anthony that worked.

Cheers
Jon
Tags
General Discussions
Asked by
Jon
Top achievements
Rank 1
Answers by
Anthony
Telerik team
Jon
Top achievements
Rank 1
Share this question
or