improve "near" relative locator behaviour#11290
Conversation
b5f8b66 to
d8bb2cd
Compare
diemol
left a comment
There was a problem hiding this comment.
Thank you for this, @alpatron.
I believe we would need a blog post to describe the changes, which is basically the description of the PR you are sending.
Also, have you checked the relative locators tests? Don't mind the unrelated failing ones. This should probably break most of them across bindings.
0476cb5 to
bb15e39
Compare
|
Hi, @diemol. I checked the relative-locator tests (at least the ones that I was able to run—Python, JavaScript, and Java), and they all pass. Well, they pass now: I had modified the This brings me to another point: Since this is a change to the Selenium atoms, I assume that it's needed to write tests that verify the fix for all language bindings. Given I can get only the Python, JavaScript, and Java tests to run on the provided gitpod environment, this could be difficult. Or are there atom-specific tests? Then I have a question about some code in the atoms: The Selenium documentation says that, by default, the "near" relative locator finds elements that are "at most 50px away from the provided locator". However, it seems that the code is using a default distance of 100 px. See the code excerpt from the unmodified selenium/javascript/atoms/locators/relative.js Lines 142 to 153 in 6d94706 It seems that the code is setting the default distance to 100 px, not 50 px. And I don't think this has to do with weird geometry rules either. The selenium/javascript/atoms/locators/relative.js Lines 166 to 211 in 6d94706 I believe that the And finally, speaking of the current way of the "near" locator, I also created a simple illustration to illustrate how the current way works. It'd be nice to get a second pair of eyes to look at it so I could check whether I understand the current way of how the "near" relative locator works. The red R2 rectangles are examples of "near" elements that the current way deems as not near, but my changed code would deem as near. |
|
I was doing a quick check and it does seem a couple of languages do pass the distance as I was not able to understand what breaks and what does not from your comment. I will let our tests run and see what breaks. I guess we need to modify |
|
After checking the CI, I believe we need to make changes in the |
Changes the behaviour of the "near" relative locator to use the distance between the two rectangles' boundaries for determining closeness. The relative_locators.html file is changed so that the center cell and cell number two's centers are far away but their boundaries are near each other. Fixes SeleniumHQ#11272
d53bc62 to
7663053
Compare
7663053 to
603591f
Compare
|
Hi, @diemol. I added tests for the Python bindings. I changed the I also changed the default search distance in |
603591f to
dc53e48
Compare
Codecov ReportPatch and project coverage have no change.
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. Additional details and impacted files@@ Coverage Diff @@
## trunk #11290 +/- ##
=======================================
Coverage 55.69% 55.69%
=======================================
Files 86 86
Lines 5421 5421
Branches 223 223
=======================================
Hits 3019 3019
Misses 2179 2179
Partials 223 223 ☔ View full report in Codecov by Sentry. |

Description
I changed the implementation and behaviour of the "near" relative locator.
The documentation states that the near locator finds elements that "are at most 50px away from the provided locator".1
Currently, the code accomplishes this by first checking some kinds of edge-to-edge distances to determine one way of closeness. Then, if this check fails, it checks to see if the centres of the bounding rectangles are within the specified distance. If this check fails as well, the elements are deemed not close. This approach breaks in certain cases as described in issue #11272.
I replaced and simplified this algorithm like so:
The intersection checking is performed by the built-in
ìntersectsmethod, since the code usesgoog.math.Rectobjects that have this feature.2This diagram might better illustrate how this approach works:
It should be noted that this thus changes the behaviour of the near locator, and existing code that expects precisely the old behaviour of the near locator might break, but since the behaviour of the near locator was always just specified as finding elements "at most 50px away from the provided locator", I don't believe that much code exists that utilises the at-times-odd behaviour of the current near locator. On the other hand, this new implementation locates elements that seemingly should be located by this locator but aren't, as described in issue #11272.
Another noteworthy mention is that the way I implemented the locator doesn't use Euclidean distance to determine "nearness", but rather Chebyshev distance. A diagram probably best describes the difference. Shown is what a proper Euclidean near area would look like.
If proper Euclidean distance is deemed crucial to be used in Selenium, it's possible to implement it by using the algorithm for example on this StackOverflow post (https://stackoverflow.com/a/26178015). But this would require more complex and longer code, and I think that the rectangular Chebyshev approach is sufficient for use and probably more natural to use than the proper Euclidean way, as I'd believe most developers think in regular rectangles rather than rectangles with rounded corners.
Technically, the documentation need not change, since the code still looks for elements that "are at most 50px away from the provided locator". What has changed is how that definition is interpreted. But given this definition is not unambiguous, the documentation probably ought to be updated in any case—whether this PR is accepted or not. I suppose if this PR is deemed acceptable, I could update the relevant documentation.
I modified the
relative_locators.htmlfile to trigger the edge case behaviour in issue #11272.I had some trouble with tests: I developed this PR using gitpod, as was recommended in the README, but some tests were failing even on a fresh repo with the latest commit from the trunk branch with no changes made. I don't know if this a problem with gitpod or whether this is expected behaviour. But after performing the fix, no extra failures were recorded. All the relative-locator tests are passing.
Motivation and Context
Fixes issue #11272. The near relative behaviour sometimes doesn't find elements that seem like they should be within 50px of the referenced element.
Types of changes
Checklist
Footnotes
https://www.selenium.dev/documentation/webdriver/elements/locators/#near ↩
https://google.github.io/closure-library/api/goog.math.Rect.html ↩