🧹 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
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.
|
👋 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.
| if (locationValues.TryGetValue("altitude", out var altitude)) | ||
| { | ||
| location.Altitude = Convert.ToDouble(altitude); | ||
| } | ||
| if (locationValues.TryGetValue("latitude", out var latitude)) | ||
| { | ||
| location.Latitude = Convert.ToDouble(latitude); | ||
| } | ||
| if (locationValues.TryGetValue("longitude", out var longitude)) | ||
| { | ||
| location.Longitude = Convert.ToDouble(longitude); | ||
| } |
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.
Resolved another `StaleElementReferenceException` occurring in `test/integration/Android/ActionsChainsTest.cs` during the `SimpleTouchActionTestCase` test. Just like `TouchByCoordinatesTestCase`, the failure was caused by accessing the `elementToTouch.Location` properties repeatedly during the action sequence creation while the cache might be stale. Eagerly fetched the `Location` property into a local variable immediately after fetching the `AppiumElement` to prevent these multiple evaluation checks and avoid the exception.
…517764888486651733
🎯 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