Loading Image from a project in user code

Ask general questions here.
jenkinsjunior
Posts: 3
Joined: Tue Aug 25, 2015 12:35 pm

Loading Image from a project in user code

Post by jenkinsjunior » Thu Aug 27, 2015 12:14 pm

I have added an existing item (png) into my project. I would like to know the correct way to use Imaging.Load() to be able to do validations based on this.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3937
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Loading Image from a project in user code

Post by odklizec » Fri Aug 28, 2015 8:11 am

Hi and welcome here,

If you want to compare your png file against a repo item, you can use code like this:

Code: Select all

image = Imaging.Load ("c:\path\to\file.png");    
Validate.ContainsImage(RepoItemInfo, image, Imaging.FindOptions.Default, "Image validation", true);
Or like this, if you want to override Ranorex defaut FindOptions...

Code: Select all

Imaging.FindOptions MyFindOptions = new Imaging.FindOptions(0.95);
Validate.ContainsImage(RepoItemInfo, image, MyFindOptions, "Image validation", true);
You can find some more examples here:
http://www.ranorex.com/support/user-gui ... html#c3326
or by searching this forum ;)
Last edited by odklizec on Tue Feb 07, 2017 9:00 am, edited 1 time in total.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

jenkinsjunior
Posts: 3
Joined: Tue Aug 25, 2015 12:35 pm

Re: Loading Image from a project in user code

Post by jenkinsjunior » Fri Aug 28, 2015 3:34 pm

Thanks for your response,

For now I will be making do with using the default relative ranorex working directory path (bin\debug) and then getting it's grandparent, which is where Ranorex copied the files when they were imported. It works, but I suspect it will not once it is compiled.

This is because I cannot guarantee where the files will be on various test pcs. I need a way to perhaps reference these assets in a relative fashion, as if they were in the repo.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3937
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: App get crashed while tap on element in iOS Instrumented app

Post by odklizec » Fri Aug 28, 2015 4:43 pm

If you already added the image file to your project, then you can now set it to always copy to output directory, which means the file will be copied (during build) to bin/debug or bin/release folder. In Projects view select the file and then in Properties set Copy to output dir. to Always.
image.jpg
image.jpg (95.61 KiB) Viewed 1832 times
Then you can use this code to get the path of debug/release dir...
string path = Ranorex.Core.Testing.TestSuite.WorkingDirectory;
Hope this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

jenkinsjunior
Posts: 3
Joined: Tue Aug 25, 2015 12:35 pm

Re: Loading Image from a project in user code

Post by jenkinsjunior » Wed Sep 02, 2015 3:49 pm

Thank you so much, I think this will do it.

EDIT: Working, thank you.

sanoyag
Posts: 7
Joined: Fri May 12, 2017 9:40 am

Re: Loading Image from a project in user code

Post by sanoyag » Mon Jun 05, 2017 9:01 am

Hi,

I came across this thread and have found out that this was what I needed for my current test case. However, I am stuck after the code for getting the path of the debug/release dir. How do I go through the path of where my Image is? Currently my image is stored on a folder called Images and it has been copied on the release dir since I specified the copy to output directory to always. I tried something like image = Imaging.Load ("c:\path\Images\specific_image.png"); yet I have encountered an error on unrecognized escape sequence. Am I doing it wrong? It's my first time actually trying the image validation and I'm still exploring..Thank you very much. Currently this is my code:

string path = Ranorex.Core.Testing.TestSuite.WorkingDirectory;
image = Imaging.Load ("c:\path\Images\specific_image.png");
Imaging.FindOptions MyFindOptions = new Imaging.FindOptions(0.95);

Validate.ContainsImage(repo.App.Text1478Info,(System.Drawing.Bitmap) image, MyFindOptions, "Image validation", true);

Please advise..
Thank you in advance

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3937
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Loading Image from a project in user code

Post by odklizec » Mon Jun 05, 2017 9:14 am

You must use double backslash...

Code: Select all

image = Imaging.Load("c:\\path\\Images\\specific_image.png");
Or @ character in front of the path:

Code: Select all

image = Imaging.Load (@"c:\path\Images\specific_image.png");
Second solution is better, because you will most probably not use hardcoded path but variable? So it's better to go this way.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration