Desktop: How to Click on a button after finding it using image comparision
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Desktop: How to Click on a button after finding it using image comparision
I have found whether a button exists or not using 'FindRegion'.
Now how do I click this button ?
Below is the VB script I have used for checking if the button exists or not;
Set Button = Regions.GetPicture("output_preview")
Set currentWindow = Sys.Desktop.ActiveWindow.Picture(0, 0, -1, -1)
If Regions.FindRegion(Button ,currentWindow ) Is Nothing Then
Log.Warning "Not found"
Else
Log.Warning"Found"
End If
Please Help.!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
While the image comparison obviously works for detecting if something shows up... that's not exactly the way I'd go about doing it. However, I'm assuming you have a reason for using a Regions.Find to find a button rather than checking for the existance of the object in your application using something like WaitChild or WaitAliasChild or FindChild or something.
Is there a reason why you aren't using those methods? Basically, FindRegion simply returns another image so you're not actually finding the button, you're just searching one picture for another picture. In order to actually interact with the button, I'd suggest using FindChild. What you could do, then, is use FindChild to search for the object and then, if it's find, click on it and if it's not found, log the error.
Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----
Why automate? I do automated testing because there's only so much a human being can do and remain healthy. Sleep is a requirement. So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.
Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
While Robert's reply is the recommended way to go if for some reason you *really* need to use image search, note that as per https://support.smartbear.com/testcomplete/docs/reference/project-objects/items/stores/regions/findr..., .FindRegion() returns found rectangle if the search succeeds.
So your code should be like this:
Set Button = Regions.GetPicture("output_preview") Set currentWindow = Sys.Desktop.ActiveWindow.Picture(0, 0, -1, -1) Set oSearchResult = Regions.FindRegion(Button ,currentWindow ) If oSearchResult Is Nothing Then Log.Warning "Not found" Else ' Do a .Click() here using coordinates calculated using currentWindow and oSearchResult objects End If
/Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Alex,
But how do i calculate the coordinates using currentWindow and oSearchResult objects ?
Can you help me out here.
Ant the reason I'm using image recognition to click on this button is because 'Object Spy' is unable to identify this button. I don't get a red rectangle over this button( also some other buttons which are on the status bar of my desktop application).
I don't want to use coordinates to click on the button, which is why I chose to do it using image recognition.
Please let me know if I can go ahead with Roberts suggestion in this case.
I have attached the screenshot of the status bar having the red rectangle of 'Object Spy'.
Thanks in advance.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
> Please let me know if I can go ahead with Roberts suggestion in this case.
Robert's suggestion is the recommended way because it is more fast and more reliable.
Assuming that you are testing desktop application, you should go through the https://support.smartbear.com/testcomplete/docs/app-testing/desktop/index.html help section and check that all necessary steps are done for your application. If, according to https://support.smartbear.com/testcomplete/docs/app-testing/desktop/index.html and information from your developers, you think that some control should be recognized by TestComplete's Object Browser while it is not, you may contact Support via the https://support.smartbear.com/message/?prod=TestComplete form and ask them.
> But how do i calculate the coordinates using currentWindow and oSearchResult objects ?
Exact code will depend on whether coordinates of the oSearchResult object are relative to the screen or to the parent currentWindow object.
Assuming screen-related coordinates, the code might be just like this:
Sys.Desktop.Click(oSearchResult.Left + 3, oSearchResult.Top + 3) // 3 is some empirical shift to make click() to happen over the control
Assuming object-related coordinates, the code might be:
Sys.Desktop.Click(currentWindow.ScreenLeft + oSearchResult.Left + 3, currentWindow.ScreenTop + oSearchResult.Top + 3) // 3 is some empirical shift to make click() to happen
/Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@TestCytel wrote:
... 'Object Spy' is unable to identify this button. I don't get a red rectangle over this button( also some other buttons which are on the status bar of my desktop application).
Try enabling MSAA or Text Recognition for the object whose elements you want to identify:
- In the Object Spy, find the WndClass of the object marked by the red frame in your image.
- Add this value to the list in the project properties > Open Applications > MSAA and select the check box next to it. Save the project.
- Try spying on the button again.
- If this did not help, add the WndClass value to the project properties > Open Applications > Text Recognition and try again.
Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you can't find the object with the spy, sometimes it helps to record a script and see how the script is recorded when you click the button. Maybe it records as Object.Button.Click or something. Then you can find the parent object and what the script added to click the button. Or find the parent object and click on the button location.
