Skip to content

🧹 Refactor AppiumDriver.Location to use safe type conversions - #244

Open
Dor-bl wants to merge 4 commits into
mainfrom
fix/appium-driver-location-type-conversion-5517764888486651733
Open

🧹 Refactor AppiumDriver.Location to use safe type conversions#244
Dor-bl wants to merge 4 commits into
mainfrom
fix/appium-driver-location-type-conversion-5517764888486651733

Conversation

@Dor-bl

@Dor-bl Dor-bl commented Aug 16, 2026

Copy link
Copy Markdown
Owner

🎯 What: Modified AppiumDriver.cs to defensively extract "altitude", "latitude", and "longitude" from the GetLocation response.

💡 Why: The previous implementation casted the response Value directly to a dictionary via the as operator 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 (NullReferenceException or KeyNotFoundException). The new approach prevents these crashes and gracefully falls back to a 0-value initialized Location object.

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 the TryGetValue behavior.

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

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.
@google-labs-jules

Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI lite review requested due to automatic review settings August 16, 2026 14:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 Location object and conditionally populates its coordinate fields.
  • Replaces direct dictionary indexing with TryGetValue checks to avoid KeyNotFoundException.
  • 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.

Comment on lines +307 to +318
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);
}
Dor-bl added 2 commits August 16, 2026 15:26
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants