Exception when trying to capture an image

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

Exception when trying to capture an image

Post by jsmith » Wed Nov 14, 2018 9:58 am

I have code which should capture a screenshot, either via a repo item, or via screen co-ordinates. I f I pass the method a repository item it will use it.
I am regularly getting an exception "Object Reference Not Set to An Instance of an Object".
I am not a natural coder. I get that something isn't set, but can't figure out what it is, nor how to fix it. Can anyone recreate the error, or better still, offer a solution?
I realise it's a long shot....

Code: Select all

if(repo_used == false)
            					{
            						Bitmap newimg = Imaging.CaptureDesktopImage(new Rectangle(start_x, start_y, width, height));
            						//save screenshot
            						newimg.Save(ref_combo, System.Drawing.Imaging.ImageFormat.Jpeg);
            						
            						
            					}
            					else
            					{
            						
            						
            						Ranorex.Unknown itemAdapter = item_in.CreateAdapter<Ranorex.Unknown>(false);
            						Bitmap newimg2 = Ranorex.Imaging.CaptureImage(itemAdapter);
            						
            						//save screenshot
            						newimg2.Save(ref_combo, System.Drawing.Imaging.ImageFormat.Jpeg);//<<<<<<<<<<<<<<<<<Object Reference not set to an instance of an object
            						
								}

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

Re: Exception when trying to capture an image

Post by odklizec » Wed Nov 14, 2018 2:26 pm

Hi,

Does it happen always or it's a random issue? Have you tried to debug the code? My guess is, that there is already something wrong with the itemAdapter or newimage2 definition. Are you sure the itemAdapter and newimg2 are not null/empty?
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: 36
Joined: Mon Mar 05, 2018 4:03 pm

Re: Exception when trying to capture an image

Post by jsmith » Wed Nov 14, 2018 2:59 pm

Hello again odklizec,
I have spent a lot of time trying to debug it. The error always occurs at that line, indicating newimg2 is null. Tracing that back, I fill newimg2 with an Image.Capture
And tracing that back brings us to the itemAdapter.
The itemAdapter is where I think it goes wrong. Sometimes, even when the repo item is found, we do not seem to end up with a functioning itemAdapter.
I have debugged to check that the repo item is present. When, for whatever reason Ranorex does not find it, then this error seems occur and that would make sense - it can't take a picture if it can't find the repository item. However, the test regularly finds the repository item ( and posts a report line to tell me it has done so). Then throws the exception at the indicated line.

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

Re: Exception when trying to capture an image

Post by odklizec » Wed Nov 14, 2018 3:08 pm

In this case, I would suggest to add Exists() check before creating the adapter. However, it's still weird, because if the element is not available, CreateAdapter should fail too? So my guess is, that the element exists at a time of creating the adapter, just not visible? Eventually, the repo element, from which you are trying to create an adapter, points to more than just one element and when the code fails, it most probably uses wrong one?

Are you pretty sure that the xpath behind item_in element points to just one item? Also, try to add @visible='True' to the item_in xpath. This should assure that the element is found and used only if visible ;)
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: 36
Joined: Mon Mar 05, 2018 4:03 pm

Re: Exception when trying to capture an image

Post by jsmith » Wed Nov 14, 2018 3:25 pm

I have just added the visible = true steps to the one of my two repo items that this is affecting.

However the other item is a 'Base' item. That is, it has a lot of items within it.
Its xpath is:

Code: Select all

/dom[@visible='True' and @domain='mydomain.co.uk']
But it normally displays with Base: preceding it.

I use this item as I often want to capture everything in the browser window that isn't the toolbar itself. Spy gave me this item and it seems to work - most of the time. I pass this repo item to the code and it is assigned to the variable item_in
I check that item_in exists near the start of the method. 99% of the time it does, but then that doesn't really mean anything, as it should exist if the browser exists. Still, sometimes, if I remove the domain ( leaving just /dom[] as the xpath) it will find it, and sometimes it won't.

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

Re: Exception when trying to capture an image

Post by odklizec » Wed Nov 14, 2018 3:34 pm

Hi,

For DOM element, use rather @state='complete' attribute, which should make sure the test continues only if the page loading is completed. But I'm not quite sure this will solve your problem? Unfortunately, without seeing the web page, your solution and exact code you are using, it's hard to tell what could be the cause of the problem. BTW, are you sure using image-based operations is the best way to handle your scenario? I would personally use images only if there is absolutely no other way to do what I want and only for very small elements/images.
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: 36
Joined: Mon Mar 05, 2018 4:03 pm

Re: Exception when trying to capture an image

Post by jsmith » Thu Nov 15, 2018 12:14 pm

Hello,
An update:
I seem to have fixed the problem (fingers crossed).
This line:

Code: Select all

Bitmap newimg2 = Ranorex.Imaging.CaptureImage(itemAdapter);
is the line that was failing to do as designed. Quite often, the line would pass but newimg2 would still be null. I now test to see if it is null after this line and use a while loop to keep trying until it isn't null.

Additionally, this code is used with Browserstack. I would often get elements not found, seemingly at random. I now use 30s waitfor exists to try and make sure that my element will be loaded. I then use loops on the itemAdapter line to keep trying that until it is not null, then move on to the newimg2 line and loop that one too. So far, this seems to be working!

In response to whether or not image validation is the way to go, I'm afraid there is no choice. We absolutely have to check that the charts we create look correct in the browser, and therefore we must grab screenshots - or rather elementshots. I honestly don't know of a better way to do visual regression testing at the moment, especially cross-browser, than using Ranorex and BrowserStack. Perhaps someone else can suggest an alternative?

When looking to screenshot everything below the toolbar, I use an element with the xpath /dom . That's it, no added parameter. At this time, this is the most fragile part of the test, which I view as quite a result! Despite there quite obviously being something in the browser, sometimes I get an element not found. To combat this I have used WaitFor as described above.