Skip to content

fix: use the alert text, not the whole data object, for unexpected alert errors - #8

Draft
MattL1989 wants to merge 1 commit into
masterfrom
fix/unexpected-alert-text
Draft

MattL1989 wants to merge 1 commit into
masterfrom
fix/unexpected-alert-text

Conversation

@MattL1989

Copy link
Copy Markdown
Owner

Problem

When the server returns an unexpected alert open error, MobileErrorHandler.check_response passes the whole W3C data object to UnexpectedAlertPresentException as alert_text. The W3C spec puts the alert's text in data.text:

response = {
    'status': 500,
    'value': json.dumps({'value': {'error': 'unexpected alert open', 'message': 'alert', 'data': {'text': 'Hello'}}}),
}
MobileErrorHandler().check_response(response)
Before Selenium's ErrorHandler
e.alert_text {'text': 'Hello'} 'Hello'
str(e) starts with Alert Text: {'text': 'Hello'} Alert Text: Hello

alert_text is meant to be a string, so code that logs it or searches it gets the wrong result.

Fix

  • Take data['text'] when data is a dict, as Selenium does.
  • Use None when there is no data, or when it is not a dict or has no text.

The change is limited to the UnexpectedAlertPresentException lines at the end of check_response.

Behaviour change: e.alert_text is now the alert's text as a string. Code that worked around the bug with e.alert_text['text'] would need updating.

Tests

  • New test/unit/webdriver/errorhandler_alert_test.py checks that:
    • alert_text is 'Hello' and str(e) contains Alert Text: Hello for data: {'text': 'Hello'};
    • alert_text is None when data is missing or has no text;
    • find_element() raises UnexpectedAlertPresentException with alert_text == 'Hello' when the server answers 500 with that error, checked end to end through httpretty.
  • 3 of the 4 new tests fail without the fix, for example assert {'text': 'Hello'} == 'Hello'.
  • ruff check, ruff format --check, mypy appium and the full unit test suite pass.

🤖 Generated with Claude Code

…ert errors

MobileErrorHandler.check_response passed the whole W3C error `data`
object as `alert_text` when raising UnexpectedAlertPresentException.
For an `unexpected alert open` error with `"data": {"text": "Hello"}`,
`e.alert_text` was `{'text': 'Hello'}` and `str(e)` started with
`Alert Text: {'text': 'Hello'}`. Selenium's own ErrorHandler gives
`'Hello'`.

Take `data['text']` when `data` is a dict, as Selenium does, and use
None otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant