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

时间:2018-03-16 14:07:50

标签: python selenium-webdriver appium-ios webdriverwait

我有一个Appium测试类测试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:消息:处理命令时发生未知的服务器端错误。原始错误:当一个未打开时,尝试对模态对话框进行操作。

* test_normal虽然有效。我试图注释掉 test_normal test_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上运行Appium 1.7.2(Appium GUI 1.4.0)。测试iOS在带有OS 11.2的iPhone 7模拟器上运行。

错误堆栈跟踪:

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对话框屏幕图像 image of the dialog of test_normal

test_fail对话框屏幕图像 image of the dialog of test_fail

1 个答案:

答案 0 :(得分:1)

您很可能面临错误https://github.com/appium/appium/issues/10286

错误: 在第一次ping /检查警报本身时,如果不存在警报,Appium会抛出异常而不等待给定的时间。

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

另请参阅https://github.com/facebook/WebDriverAgent/issues/857,其中表示它是Appium问题,但不是WebDriver。

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