Screenshots from User Code

Ask general questions here.
tallahassee101
Posts: 169
Joined: Thu Jan 13, 2011 2:06 pm

Screenshots from User Code

Post by tallahassee101 » Mon Jun 11, 2012 4:32 pm

Hi,

I saw in the release notes for 3.3 the following item:

"Repository images/item screenshots are now stored in a separate file and are available through code "

Could you explain how to do this or point me to the documentation for it? We have a bunch of screenshot validations in a test that I would like to make into a simpler test that we just pass a dataset table into with the list of screenshot names.

When I convert the image validation we have to code I get this:

Code: Select all

Validate.ContainsImage(repo.FormZoom.APP_ZoomInfo, APP_Zoom_Site_0102, APP_Zoom_Site_0102_Options, Validate.DefaultMessage, false);
What I would like to do in user code is something like this:

Code: Select all

                //Turn this string into a variable
        	Bitmap myImg = new Bitmap("APP_Zoom_Site_0102");

               //Turn the options variable into a generic set of options
        	Validate.ContainsImage(repo.FormZoom.APP_ZoomInfo, myImg, APP_Zoom_Site_0102_Options);
Thanks!
-Nick

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Screenshots from User Code

Post by Support Team » Tue Jun 12, 2012 10:06 am

Hi,
tallahassee101 wrote:I saw in the release notes for 3.3 the following item:
"Repository images/item screenshots are now stored in a separate file and are available through code "
Could you explain how to do this or point me to the documentation for it?
the documenation regarding this new feature can be found at
Lesson 7: Code Modules - Using Repository within Code Module.

Regards,
Tobias
Ranorex Team

tallahassee101
Posts: 169
Joined: Thu Jan 13, 2011 2:06 pm

Re: Screenshots from User Code

Post by tallahassee101 » Tue Jun 12, 2012 9:39 pm

So the documentation gives the following example of retrieving the bitmap:

Code: Select all

    
   // get the screenshot from the repoistory  
    Bitmap MyScreenshot = MyRepo.IconPicker.LI_IconInfo.GetScreenshot_Icon(); 
This won't work for pulling the bitmap based on the file name. I want to do a table of all the screenshot file names and then iterate through them as variables.

Code: Select all

    //strScreenshotName is a variable that is bound to a data table of bitmap repo item names.
    Bitmap MyScreenshot = MyRepo.IconPicker.LI_IconInfo.GetScreenshot(strScreenshotName); 
Is there a way to implement the above pseudo code?

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Screenshots from User Code

Post by Support Team » Wed Jun 13, 2012 10:31 am

Hi,

what you can do is to use if statements and call the given methods based on the string stored in the variable strScreenshotName:

E.g.:
...
if (strScreenshotName == "Screenshot_Icon")
  MyScreenshot = MyRepo.IconPicker.LI_IconInfo.GetScreenshot_Icon(); 
else
  MyScreenshot = MyRepo.IconPicker.LI_IconInfo.GetScreenshot_Icon2();
...
Or, you can use refelction to dynmaically call the methods.

Regards,
Tobias
Ranorex Team

tallahassee101
Posts: 169
Joined: Thu Jan 13, 2011 2:06 pm

Re: Screenshots from User Code

Post by tallahassee101 » Wed Jun 13, 2012 5:43 pm

Tobias,

Thanks I haven't used reflection before, but after spending some time with it I think I understand how to get this working. However I am running into a problem getting my generated cs file to compile as it cannot find Ranorex in order to do a "using Ranorex;" in the generated code.

I am running the following from the commandline to debug my issue with generating a dll:
csc /target:library BitmapImageFinder.cs

BitmapImageFinder.cs ( generated ):

Code: Select all

using System.Drawing;
using Ranorex.Core.Repository;
	
class BitmapImageFinder
{
    
	public Bitmap FindSite(  )
	{
	// Image search method
       TacViewLib.TacViewLibRepository repo = TacViewLib.TacViewLibRepository.Instance;
	Bitmap myImg = repo.FormZoom.APP_ZoomInfo.GetSite0103(new Rectangle(108, 255, 153, 125));	return myImg;	}
}
The error i get is CS0246: The type or namespace name 'Ranorex' could not be found. Any help would be appreciated!

Do you guys have an example anywhere of how you use reflection in user code to do something similar?

I'm doing the following which doesn't work ( I'm assuming because the code can't compile ), right now it just hangs on WaitForExit:

Code: Select all


	         ProcessStartInfo psi = new ProcessStartInfo(  );
	         psi.FileName = "cmd.exe";
	 
	         string compileString = "csc /optimize+ /target:library BitmapImageFinder.cs > compile.out";       
	 
	         psi.Arguments = String.Format(compileString);
	         psi.WindowStyle = ProcessWindowStyle.Minimized;
	 
	         Process proc = Process.Start(psi);
	         proc.WaitForExit(  );    // wait at most 2 seconds
	 
	         // Open the file, and get a 
	         // pointer to the method info
	         Assembly a =  Assembly.LoadFrom(fileName + ".dll");
Thanks
-Nick

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Screenshots from User Code

Post by Support Team » Thu Jun 14, 2012 3:37 pm

Hi Nick,

I would suggest using the first way which Tobias mentioned, as it is much easier:
switch (strScreenshotName)
      {
        case "ElementOne":
          repo.Start.StartInfo.GetScreenshot1();
          break;
        case "ElementTwo":
          repo.TryitEditorV15.SomePTagInfo.GetScreenshot1();
          break;
        default:
          Console.WriteLine("Default case");
          break;
      }
We unfortunately do not have a code sample about how to use reflection, but the following link describes it very well: Reflection

Regards,
Markus
Ranorex Support Team

tallahassee101
Posts: 169
Joined: Thu Jan 13, 2011 2:06 pm

Re: Screenshots from User Code

Post by tallahassee101 » Thu Jun 14, 2012 4:36 pm

Markus,

That solution with a switch statement will not work, we have 8 sets of 40 cases right now and want to add more. We need to use this reflection. What I am not understanding is how to get the "csc" compile to find the Ranorex library. I have looked through the forums here and several times "Reflection" has been given as the solution to solving a similar problem. If that is a solution then I think as a user of the product I should be provided with a simple example that shows how to get the compile step to work with Ranorex using the ranorex repositories.

Thank you,
-Nick

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Screenshots from User Code

Post by Support Team » Fri Jun 15, 2012 10:29 am

Hi Nick,

Here is a sample project about how you can invoke the specific method which you are getting with reflection, but as I said in my view reflection is not really needed in this case.
I hope this will help you solve the problem.

Regards,
Markus
Ranorex Support Team
You do not have the required permissions to view the files attached to this post.

tallahassee101
Posts: 169
Joined: Thu Jan 13, 2011 2:06 pm

Re: Screenshots from User Code

Post by tallahassee101 » Tue Jun 26, 2012 2:01 pm

Markus,

Thank you, that test works and explains how to solve part of the problem with Reflection. I'd still like to know why my reflection code can't find the Ranorex libraries or link against the DLL, but for now I'll just focus on using yours since it is much simpler.
The problem I'm still facing is that the find options do not seem to be translating properly to user code, or the validation is failing for some other reason.

Here is my version of the code example from what you gave me:

Code: Select all


        public void Validate_APP_Zoom()
        {
            TacViewLib.TacViewLibRepository myRepo = TacViewLib.TacViewLibRepository.Instance;	
            string methodName = "GetSite_" + picturetracknum;            
            MethodInfo myMethidInfo = myRepo.FormZoom.APP_ZoomInfo.GetType().GetMethod(methodName, new Type[0]);
            CompressedImage myImg = (CompressedImage) myMethidInfo.Invoke(myRepo.FormZoom.APP_ZoomInfo, null);
            Imaging.FindOptions myOptions = Imaging.FindOptions.Parse("1;None;127,262,115,102;True;10000000;0ms");
            Validate.ContainsImage(repo.FormZoom.APP_ZoomInfo, myImg, myOptions, Validate.DefaultMessage, false);
        }
When I convert one of the validation cases that I am trying to mimic to user code I get the following in my user code:

Code: Select all


        public void Validate_APP_Zoom2()
        {
            Report.Log(ReportLevel.Info, "Validation", "(Optional Action)\r\nValidating CompareImage (Screenshot: 'Site_0103' with region {X=127,Y=262,Width=115,Height=102}) on item 'FormZoom.APP_Zoom'.", repo.FormZoom.APP_ZoomInfo);
            Validate.CompareImage(repo.FormZoom.APP_ZoomInfo, APP_Zoom_Site_0103, APP_Zoom_Site_0103_Options, Validate.DefaultMessage, new Validate.Options(false, Validate.CreateScreenshot.Always));
        }


And these helper methods get generated in the .cs file:

Code: Select all


        CompressedImage APP_Zoom_Site_0103
        { get { return repo.FormZoom.APP_ZoomInfo.GetSite_0103(new Rectangle(127, 262, 115, 102)); } }

        Imaging.FindOptions APP_Zoom_Site_0103_Options
        { get { return Imaging.FindOptions.Default; } }
The problem I have is all of these numbers for the area to compare the image are different for each image. I want to just do a compare of the entire image since each image is a single icon graphic on a plain gray background. The rectangles shouldn't matter, but they seem to as the user code fails each time. I'd like to remove the "127, 262, 115, 102" from the find options and just use the default find options.
The images getting compared seem to be identical, but the user code validation will fail. Any ideas?

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Screenshots from User Code

Post by Support Team » Wed Jun 27, 2012 1:03 pm

Hi,
Support Team wrote: I'd like to remove the "127, 262, 115, 102" from the find options and just use the default find options.
to get the defauult find options you can use the following code snippet:
Imaging.FindOptions myOptions = Imaging.FindOptions.Default;
tallahassee101 wrote:The images getting compared seem to be identical, but the user code validation will fail. Any ideas?
I would recommend to set the similarity to a value less than 100%.
This can be done using following code snippet:
myOptions.Similarity = 0.9;
Regards,
Tobias
Ranorex Team

tallahassee101
Posts: 169
Joined: Thu Jan 13, 2011 2:06 pm

Re: Screenshots from User Code

Post by tallahassee101 » Thu Jul 05, 2012 3:57 pm

Tobias,

Sorry, I didn't word that correctly. I want to do an image comparison of the subset "127, 262, 115, 102" or some variation, however it doesn't seem to be working properly. When I do the following:

Code: Select all

            Imaging.FindOptions myOptions = Imaging.FindOptions.Default;
            myOptions.Clipping = new Rectangle(127,262,115,102);
            Validate.ContainsImage(repo.FormZoom.APP_ZoomInfo, myImg, myOptions,Validate.DefaultMessage, false);
I get a failure on the comparison, but the failure screenshots show the entire image, rather than the subset image ( the image is about 380x700 in size ). Any idea why this isn't working?

Thanks,
Nick

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Screenshots from User Code

Post by Support Team » Fri Jul 06, 2012 5:12 pm

Hi Nick,

I think you want to make the following:
Imaging.FindOptions myOptions = Imaging.FindOptions.Default;
Validate.ContainsImage(repo.FormZoom.APP_ZoomInfo, FormZoom.APP_ZoomInfo.GetScreenshot1(new Rectangle(127,262,115,102)), myOptions);
am i right?
The clipping just defines the region where the search takes place and not the "Selection Rectangle".

Regards,
Markus
Ranorex Support Team

tallahassee101
Posts: 169
Joined: Thu Jan 13, 2011 2:06 pm

Re: Screenshots from User Code

Post by tallahassee101 » Mon Jul 09, 2012 2:21 pm

Markus,

How do I specify the new Rectangle parameter using the invoke code though? I'm not sure if you saw it but that's how this post started. The code I'm using right now is:

Code: Select all

            string methodName = "GetSite_" + picturetracknum;            
            MethodInfo myMethidInfo = myRepo.FormZoom.APP_ZoomInfo.GetType().GetMethod(methodName, new Type[0]);
            CompressedImage myImg = (CompressedImage) myMethidInfo.Invoke(myRepo.FormZoom.APP_ZoomInfo, null);
Where picturetracknum is a variable passed in ( one example: "0103"). Thanks for any help!

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Screenshots from User Code

Post by Support Team » Tue Jul 10, 2012 2:55 pm

Hi Nik,

Sorry, I thought you have problems with the mentioned code.
Did you try to add the Rectangle as parameter to the invoke method, instead of the null?

Regards,
Markus
Ranorex Support Team

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Screenshots from User Code

Post by Support Team » Tue Jul 10, 2012 3:07 pm

Hi Nik,

Here is the code:
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(10,10,10,5);
			Object[] obj = new Object[1];
			obj[0]= rect;
			
			String methodName = "GetScreenshot1";
			MethodInfo myMethidInfo = repo.Calculator.ContainerHash32770.Button131Info.GetType().GetMethod(methodName, new Type[1] {typeof(Rectangle)});
			CompressedImage image = (CompressedImage) myMethidInfo.Invoke(repo.Calculator.ContainerHash32770.Button131Info, obj);//null
			image.Store(@"C:\ScreenshotFromInvoke.png");
Regards,
Markus
Ranorex Support Team