Passing repository item to user code as a variable

Class library usage, coding and language questions.
aaroncz
Posts: 5
Joined: Tue Jun 28, 2016 3:22 pm

Passing repository item to user code as a variable

Post by aaroncz » Tue Jun 28, 2016 3:49 pm

Hi,

I'm trying to write user code which takes 2 variables, 'TimeToWait' and 'ImageToValidate'. 'TimeToWait' is an amount of time that the validation waits to pick up the image, and 'ImageToValidate' is meant to be the name of a snapshot which is in the repository. The problem I am trying to solve is taking the second variable, 'ImageToValidate', and use it in the user code to validate snapshot images from my repository.

Currently I have a repository item called 'Canvas' which has 2 snapshots under it, 'chart1' and 'chart2'. I'd like to be able to pass these in and validate them through variables in my user code.

I am using the 'Validate.ContainsImage' and passing my repository item 'Canvas' to it as the first argument, but the second argument is where I'm getting stuck, the Bitmap argument. What I'm hoping to do is pass the name of repository item for the chart into a function and make a call to it directly but have had no luck on this so far.

Any ideas on how to do this?

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

Re: Passing repository item to user code as a variable

Post by Support Team » Thu Jun 30, 2016 12:31 pm

Hello aaroncz,

Unfortunately, it's not possible to pass those image objects to a user code action directly because this object type is not supported as an argument.

One way to implement such a scenario would be passing the name of the image as a string and initialize the corresponding image object accordingly. This could be done with the following user code. Please note that you will need to modify the code according to your repository item.

Code: Select all

public void Validate_RxSwtDemoJar(string ValidationImage)
{
        //Create image object
        CompressedImage image = null;
        	
        //Check which image should be used
        switch(ValidationImage)
        {
        	case "chart1": 
        		image=repo.Explorer.CanvasInfo.Getchart1();
        		break;
        	case "chart2":
        		image=repo.Explorer.CanvasInfo.Getchart2();
        		break;
        	default:
        		Report.Info("Name of image is not supported. Chart1 will be used.");
        		image=repo.Explorer.CanvasInfo.Getchart1();
        		break;        		
        }
        //Perform validation        	
        Validate.ContainsImage(repo.Explorer.CanvasInfo, image, Canvas_chart1_Options);
}
I hope this information will help you. If you need further assistance, please do let me know.

Sincerely,
Johannes

aaroncz
Posts: 5
Joined: Tue Jun 28, 2016 3:22 pm

Re: Passing repository item to user code as a variable

Post by aaroncz » Tue Jul 05, 2016 3:59 pm

Hi Johannes,

That does work and is a useful way to do it though it will require updating which i was trying to avoid. I was hoping I might be able to simply pass the name of the chart in and change the "Getchart#()" on demand as more charts are added but I guess this just isn't possible.

Thanks for the solution.
Aaron

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

Re: Passing repository item to user code as a variable

Post by Support Team » Wed Jul 06, 2016 12:16 pm

Hi Aaron,

There is indeed a more dynamic approach to implement such a scenario. Please have a look at the sample code below. In this case, only the name of the validation image needs to be passed as an argument (for example "Chart2").

Code: Select all

	public void Validate_RxSwtDemoJar(string ValidationImage)
		{
			//Create image object
			CompressedImage image = null;
			
			//Run the method according to the parameter ValidationImage
			Ranorex.Core.Repository.RepoItemInfo repoItem = repo.Explorer.CanvasInfo;
			var method = repoItem.GetType().GetMethod("Get"+ValidationImage,Type.EmptyTypes);
			image = (CompressedImage)method.Invoke(repoItem,null);
						
			//Perform validation
			Validate.ContainsImage(repoItem, image, Canvas_Chart1_Options);
		}
More detailed information can be found at the corresponding stackoverflow discussion.

Sincerely,
Johannes

aaroncz
Posts: 5
Joined: Tue Jun 28, 2016 3:22 pm

Re: Passing repository item to user code as a variable

Post by aaroncz » Wed Jul 06, 2016 3:37 pm

Hi Johannes,

Thank you very much for that. That is more like what I am looking for to save on having to continuously edit the user code for new screenshots. Just tested and it works very well.

Thanks!