Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add suggested selector for UiAutomator strategy #1394

Merged
merged 10 commits into from
Mar 20, 2024

Conversation

eglitise
Copy link
Collaborator

This PR adds generation of suggested selectors for the UiAutomator locator strategy, when using the UiAutomator2 driver.
While it does not cover the full breadth of capabilities for this strategy (e.g. no UiScrollable or child selectors), it can still assemble selectors out of 4 attributes (resource-id, text, content-desc, class), and add indices, which should cover most use cases.

It is also oftentimes faster than the id strategy... 👀
image

⚠️ Important to note that this strategy seems to only find elements in the last direct child of the hierarchy - elements under any other direct child are not visible. I assume this is some sort of limitation (whether intentional or not) from Google's end. This is why the implementation requires recalculating the full DOM Document, the DOM node, and path.

There is another related change in this PR: when reading the suggested locator table, the entry -android uiautomator (docs) was quite lengthy and difficult to read, therefore the presentation of entries in the selected element tables was adjusted. Until now, some columns had a fixed width, and others took up the remaining width, potentially resulting in linebreaks - this could look very unreadable at small window sizes:
image
Now the rows support horizontal scrolling, while still keeping the previously-fixed-width columns in place:
image
This does mean that very long selectors (e.g. complex xpaths/class chains) will require scrolling even for large screen sizes. I think this is still acceptable.

@github-actions github-actions bot added the enhancement New feature or request label Mar 18, 2024
@mykola-mokhnach
Copy link
Contributor

mykola-mokhnach commented Mar 18, 2024

We may also show the full selector properly wrapped in a tooltip upon mouseover cell event

* @returns {Object.<string, string>} mapping of strategies to selectors
*/
export function getComplexSuggestedLocators(path, sourceDoc) {
export function getComplexSuggestedLocators(path, sourceDoc, isNative, automationName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mathods having more than 3 arguments are hard to read. Maybe we could combine some args into an object

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same below

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps it would even make sense to have a common class that takes all common method params as constructor args, so we don't need to repeat them for each separate function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Among all 3 related functions (getSuggestedLocators, getSimpleSuggestedLocators, getComplexSuggestedLocators), there is only one common parameter (isNative). 4 parameters isn't that bad in my opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say there is still a lot space for improvements. Ofc, not necessarily as part of this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh certainly - a lot of the Inspector code is very much in need of better code quality.

@eglitise
Copy link
Collaborator Author

eglitise commented Mar 19, 2024

We may also show the full selector properly wrapped in a tooltip upon mouseover cell event

Unfortunately this does not appear to work in combination with the existing onClick tooltip (plus the text wrapping was quite ugly, although perhaps that can be customized). Another idea I had is to wrap the selector only after a certain width, but this would still make it quite difficult to read it for small window sizes.

@eglitise
Copy link
Collaborator Author

Also, while I've been using UiAutomator selectors for some time now, I'm still not sure whether the new UiSelector(). part is necessary, because resourceId("android:id/content") works just fine.


// BASE CASE #4: Check all attributes and try to find unique ones
let uiSelector, othersWithAttr, mostUniqueSelector;
let othersWithAttrMinCount = 99;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this magic number?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess Number.MAX_SAFE_INTEGER would make the intent more obvious. Another approach would be to skip the initial value here, and assign it in the following comparison if it is undefined.

@eglitise
Copy link
Collaborator Author

@eglitise eglitise merged commit b599bed into appium:main Mar 20, 2024
6 checks passed
@eglitise eglitise deleted the add-suggested-uiautomator-selector branch March 20, 2024 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants