Compare current element screenshot to reference image using variable repo item

Ask general questions here.
jsmith
Posts: 25
Joined: Mon Mar 05, 2018 4:03 pm

Compare current element screenshot to reference image using variable repo item

Post by jsmith » Wed Oct 17, 2018 12:09 pm

During the course of my test execution, I want to take a screenshot of a repository item, which I will then store and use later.
I can achieve this like so:

Code: Select all

public static void Screenshotting_1()
        {
            Image newimg = Ranorex.Imaging.CaptureImage(myrepo.repoItem.Self);
            newimg.Save(filepathandname, System.Drawing.Imaging.ImageFormat.Jpeg);

        }
This works very nicely.
Now I want to take 100 screenshots of 100 different repo items, so I want to pass in the repo item to one method, rather than write 100 methods. So I have tried this, pieced together from various forum posts:

Code: Select all

public static void Screenshotting_2(RepoItemInfo item_in)
        {
          Ranorex.Unknown itemAdapter = item_in.CreateAdapters<Ranorex.Unknown>();
            Image newimg = Ranorex.Imaging.CaptureImage(itemAdapter);
            newimg.Save(filepathandname, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
but I am getting this error:
Cannot implicitly convert type 'System.Collections.Generic.IList<Ranorex.Unknown>' to 'Ranorex.Unknown'. An explicit conversion exists (are you missing a cast?) (CS0266)
I guess I am missing a cast, but am past the limit of my coding ability. Can anyone help?

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

Re: Compare current element screenshot to reference image using variable repo item

Post by odklizec » Wed Oct 17, 2018 12:19 pm

Hi,

I assume the repo element returns multiple items? In this case, you should use something like this:

Code: Select all

public static void Screenshotting_2(RepoItemInfo item_in)
        {
                IList<Ranorex.Unknown> itemAdapters = item_in.CreateAdapters<Ranorex.Unknown>();
                foreach (Ranorex.Unknown itemAdapter  in itemAdapters)
                {
                        Image newimg = Ranorex.Imaging.CaptureImage(itemAdapter);
                        newimg.Save(filepathandname, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
        }
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

jsmith
Posts: 25
Joined: Mon Mar 05, 2018 4:03 pm

Re: Compare current element screenshot to reference image using variable repo item

Post by jsmith » Wed Oct 17, 2018 12:30 pm

:oops:
It needed a list...... doh.

I am pretty sure that your code is going to work. However my first repo item is effectively the root folder, so that generates another problem for me.
If the path for my item is:
Base: /dom[@caption = 'Google']
then I don't want a shot of every element within that path, I just the want the root item. I think I should be able to get the first item of a list easily enough(itemAdapters.First), but will the first item always be the root item?

And thank you, for your quick and helpful answers! Please tell your boss I said you're awesome!

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

Re: Compare current element screenshot to reference image using variable repo item

Post by odklizec » Wed Oct 17, 2018 12:51 pm

Hi,

If you know the repo element returns just one item, then use CreateAdapter instead of CreateAdapters ;) I just thought there will be returned more than just one element?

Could you please show us some screenshots/snapshots and a more detailed description of what exactly you want to achieve? Maybe we can find a better way how to achieve what you want? Generally speaking, I would use image-based validation only if really necessary and if there is really no other way to validate something. I would definitely not use it on large images and too many elements. It's just not so reliable way to validate something.
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

jsmith
Posts: 25
Joined: Mon Mar 05, 2018 4:03 pm

Re: Compare current element screenshot to reference image using variable repo item

Post by jsmith » Wed Oct 17, 2018 2:09 pm

Hi,
Happily, I think I am achieving what I want to - with your help :D
I can't really provide screenshots/snapshots, as the application I'm working on is very much still in development, so I can't post about it.

To give you an idea, we are moving our desktop application to a browser based one. One aspect of our application is to produce charts. So I need to test cross browser and cross device, that the charts our application produces do in fact look correct.
To do this, I will run through the test manually to secure the reference images, and they are stored on the test machine.
I will then use the test I have created in Ranorex to drive the browser, but will run this test through BrowserStack, and programmatically change endpoints to use different browsers. At the required step, I will take an screenshot of the chart (myRepoItem), rename and save it, then use a free api to do pixel to pixel comparison of the acquired image, referencing it to my earlier stored image. Ranorex can report back the pass or fail of this comparison, and then we simply continue on to the next chart, and do the same again.
In this way, we check that something looks correct. Without Ranorex and automation, this would be a very tedious and repetitive job our developers would need to do, every single time they changed something.
To make this a success, we need
Ranorex to drive BrowserStack (done)
Screenshot instructions in Ranorex test to be carried out by Browserstack (done)
Those screenshots to be saved on the local machine(done)
Image comparison(already done in other tests)
Result reported in Ranorex report(already done in other tests)
Browserstack continues test, repeats actions on other required elements ( should be possible)
Change endpoint programmatically and rerun entire test ( fingers crossed!)