Appium Python WebDriverWait wait.until(expected_conditions.alert_is_present()) 随机失败

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

我有一个 Appium 测试 class 测试一个 iOS 应用程序,里面有两个几乎相同的测试:

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()

当我运行测试时,test_fail(它运行先于test_normal ),它总是无法捕捉到错误的警告对话框:

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.

*test_normal 虽然有效。我试图注释掉 test_normaltest_fail 会失败并显示相同的消息。

然后我尝试注释掉 test_fail,但这次 test_normal 会起作用。所以出于一些奇怪的原因,test_fail 不能与 self.wait.until(expected_conditions.alert_is_present())

一起使用

但是,如果我替换 test_fail 测试 wait.until 行:

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

与:

self.wait_for('OK')

然后一切正常。

self.wait 在 def setUp(self) self.wait = WebDriverWait(self.driver, 120)

中声明

我在 Mac OS X 上 运行ning Appium 1.7.2 (Appium GUI 1.4.0)。测试 iOS 是 运行 iPhone 7 模拟器 OS 11.2.

错误堆栈跟踪:

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.

谁能帮我弄清楚这是怎么回事?

test_normal 对话框屏幕图像

test_fail 对话框屏幕图像

很可能您遇到了错误 https://github.com/appium/appium/issues/10286

错误: 在 alert 本身的第一个 ping/check 中,如果不存在 alert,Appium 将抛出异常而不等待给定时间。

最近记录了此错误(15 天前)。试试最新版本。我认为它已在最新版本 beta 中修复。

另请参阅 https://github.com/facebook/WebDriverAgent/issues/857,它说这是 Appium 问题而不是 WebDriver。

临时解决方案: 添加 1-3 秒睡眠以确保在检查显式条件之前存在警报。