🧹 Refactor AppiumDriver.Location to use safe type conversions - #244
🧹 Refactor AppiumDriver.Location to use safe type conversions#244Dor-bl wants to merge 4 commits into
Conversation
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
Refactors AppiumDriver.Location to make reading GPS coordinates from the GetLocation command response more defensive, returning a default Location instance when the payload is missing expected keys or not in the expected shape.
Changes:
- Initializes a default
Locationobject and conditionally populates its coordinate fields. - Replaces direct dictionary indexing with
TryGetValuechecks to avoidKeyNotFoundException. - Adds a type check before treating the response value as a dictionary to avoid
NullReferenceException.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Replaced unsafe type casting and unhandled KeyNotFoundException in the `Location` property getter of `AppiumDriver.cs`. Added type validation via pattern matching (`is Dictionary<string, object>`) and switched to safe dictionary access via `TryGetValue()` to ensure that the code gracefully returns default zero coordinates in cases where location properties are missing from the command response.
Fixed a `StaleElementReferenceException` occurring in `test/integration/Android/ActionsChainsTest.cs` during the `TouchByCoordinatesTestCase` test. The failure happened because the `Rect` property was accessed multiple times dynamically while constructing an action sequence, triggering multiple underlying cache evaluation checks. Extracted the `elementToTouch.Rect` property into a local variable (`rect`) immediately after finding the element to prevent multiple driver checks that lead to stale references when building the action sequence.
06ee548 to
c365343
Compare
- Use TryConvertToDouble with InvariantCulture to safely parse coordinate values from command response without throwing exceptions. - Default invalid, non-numeric, or null coordinates to 0.0. - Add unit tests in AppiumDriverLocationTests covering valid numbers, strings, integers, nulls, missing keys, and malformed payloads.
…and fix CI action chaining tests - Replaced unsafe `as Dictionary` cast with a defensive `is Dictionary<string, object>` pattern matching check in `AppiumDriver.Location` property getter. - Switched direct coordinate dictionary indexers with `TryGetValue` to prevent `KeyNotFoundException` exceptions when payloads are incomplete. - Introduced `TryConvertToDouble` helper utilizing `CultureInfo.InvariantCulture` for safe parsing of boxed objects to double. Non-numeric or null payload values gracefully fallback to `0.0`. - In `ActionsChainsTest.cs`, prevented `StaleElementReferenceException`s during action creation by using `CoordinateOrigin.Viewport` and eagerly fetched location coordinate offsets instead of indirectly prompting Appium bounds caching logic.
🎯 What: Modified
AppiumDriver.csto defensively extract "altitude", "latitude", and "longitude" from theGetLocationresponse.💡 Why: The previous implementation casted the response
Valuedirectly to a dictionary via theasoperator without a null check and then indexed the keys directly. If the server returned an unrecognized response format (not a dictionary) or omitted specific coordinate keys, it resulted in unhandled runtime exceptions (NullReferenceExceptionorKeyNotFoundException). The new approach prevents these crashes and gracefully falls back to a 0-value initializedLocationobject.✅ Verification: Ran
dotnet test test/integration/Appium.Net.Integration.Tests.csproj. Verified that tests compiled and ran successfully and that there were no regressions introduced. Also simulated a scenario offline reproducing theTryGetValuebehavior.✨ Result: Improved codebase stability and resilience in scenarios where external responses do not match the strictly expected JSON structure or property subsets.
PR created automatically by Jules for task 5517764888486651733 started by @Dor-bl