Search code examples
pythonselenium-webdriverappium-ioswebdriverwait

Appium Python WebDriverWait wait.until(expected_conditions.alert_is_present()) random failure


I have a Appium test class testing an iOS app with two nearly identical tests inside:

def test_fail(self):
    self.log_in('invalid_user_1')
    self.wait.until(expected_conditions.alert_is_present())
    alert = self.driver.switch_to.alert
    assert "Your mobile number is not registered with us" in alert.text
    alert.accept()

def test_normal(self):
    self.log_in('empty')
    self.wait.until(expected_conditions.alert_is_present())
    alert = self.driver.switch_to.alert
    assert 'Please enter mobile number' in alert.text
    alert.accept()

When I run the test, test_fail (it runs first before test_normal), it would always fail to catch the warning dialog with error:

WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: An attempt was made to operate on a modal dialog when one was not open.

The *test_normal works though. I tried to comment out test_normal, the test_fail would fail with the same message.

I then try to comment out test_fail, but this time test_normal would work. So for some strange reason, test_fail just won't work with self.wait.until(expected_conditions.alert_is_present())

However, if I replace the test_fail test wait.until line:

self.wait.until(expected_conditions.alert_is_present())

with:

self.wait_for('OK')

Then everything would work.

The self.wait is declared in the the def setUp(self) self.wait = WebDriverWait(self.driver, 120)

I'm running Appium 1.7.2 (Appium GUI 1.4.0) on Mac OS X. The test iOS is run on iPhone 7 simulator with OS 11.2.

The error stack trace:

Error
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 331, in run
    testMethod()
  File "/Users/steve/Desktop/Temp/Appium/Ding_ios_aws/ios_aws/tests/test_invalid_login.py", line 16, in test_invalid_user_login
    self.wait.until(expected_conditions.alert_is_present())
  File "/Users/steve/venv/Appium/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 71, in until
    value = method(self._driver)
  File "/Users/steve/venv/Appium/lib/python2.7/site-packages/selenium/webdriver/support/expected_conditions.py", line 387, in __call__
    alert = driver.switch_to.alert
  File "/Users/steve/venv/Appium/lib/python2.7/site-packages/selenium/webdriver/remote/switch_to.py", line 55, in alert
    alert.text
  File "/Users/steve/venv/Appium/lib/python2.7/site-packages/selenium/webdriver/common/alert.py", line 69, in text
    return self.driver.execute(Command.GET_ALERT_TEXT)["value"]
  File "/Users/steve/venv/Appium/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/Users/steve/venv/Appium/lib/python2.7/site-packages/appium/webdriver/errorhandler.py", line 29, in check_response
    raise wde
WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: An attempt was made to operate on a modal dialog when one was not open.

Can anyone help me figure out what's going on?

test_normal dialog screen image image of the dialog of test_normal

test_fail dialog screen image image of the dialog of test_fail


Solution

  • Most probably you are facing the bug https://github.com/appium/appium/issues/10286.

    Bug: In the first ping/check of alert itself, if alert is not present, Appium throws the exception without waiting for the given time.

    This bug is recently logged (15 days before). try latest version. I think it is fixed in latest version beta.

    Also refer https://github.com/facebook/WebDriverAgent/issues/857, which says it is Appium issue but not WebDriver.

    Temporary Solution: Add 1-3 seconds sleep to make sure alert is present before checking for the explicit condition.