northwoodspd / uia

A ruby gem for interacting with automation elements in Windows
MIT License
17 stars 4 forks source link

How to Identify an element with Regex #10

Closed Pramanth closed 7 years ago

Pramanth commented 7 years ago

Is there a way in UIA to find an element with regular expression. I know we can use regex to find Window like "/window_name/i"

leviwilson commented 7 years ago

The uia_spec has some examples of finding things by regex.

Note that when you are looking for things by regex that have to go down into UI Automation that they won't be as fast as if you use locators that use win32 calls, like window title, etc.

Pramanth commented 7 years ago

Thanks @leviwilson for the information.

Tried different ways like Uia.children.find { |e| e.name =~ /MainFormWindow/ } etc., but could not able to find a way to identify the children with partial match. For now we are getting all the children under a element and returning the element name based on the regex match .

leviwilson commented 7 years ago

It'd likely be beneficial to know the structure of your app (as seen by UI Spy) to have a better recommendation.

In general, however...you want to refrain from doing a lot of regex searches within a .children (top-level children only) and especially with a .descendants since that can be CPU intensive as it is not a fast operation to hit that may UIA calls in your entire window structure.

You really want to be leveraging the automation IDs where possible since those are the fastest lookups.

Pramanth commented 7 years ago

@leviwilson Sorry for the delay. Below is the way the application is structured in UI Spy,

image

We want to interact with attachments and we have file name to interact with the element not the size of the file.

Got your valuable points wrt drawback of regex search. Thank you.

leviwilson commented 7 years ago

Gotcha; I have another gem that is a higher-level wrapper around uia. It's called mohawk. It looks like you have a root for your list in "Attachments" up there; my guess is that it has an AutomationId for it? If so, you could define a page object like so:

class SomeScreen
  include Mohawk
  window(title: /RegEx for some top-level window/)

  select_list(:attachment, id: 'runtimeIdForAttachments')
end

Then you can interact with it like so:

on(SomeScreen) do |screen|
  screen.attachment = 'sk2.jpg.html'
end

I believe you can pass a regex in as well, so on(SomeScreen).attachment = /^sk2/.

Pramanth commented 7 years ago

Thanks @leviwilson for all the information.