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

标签 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: 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 上运行 Appium 1.7.2 (Appium GUI 1.4.0)。测试 iOS 在操作系统 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

最佳答案

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

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

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

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

临时解决方案: 添加 1-3 秒的 sleep 时间,以确保在检查明确条件之前存在警报。

关于python - Appium Python WebDriverWait wait.until(expected_conditions.alert_is_present()) 随机失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49322696/

相关文章:

javascript - Selenium - 自动化javascript按钮点击

c# - 使用 C# 将驱动程序导航到 selenium 中新打开的窗口

python - 如何使用 selenium python 缓慢向下滚动网页?

iOS 自动化测试 - XCTest 与 Appium

python - 无法在 Mac OS X 10.9 上安装 Lxml

python - Numpy:使用矩阵作为另一个矩阵的索引来创建张量?

java - 如何检查 apk 文件是否已安装在我的模拟器中?

appium-ios - 如何在 ios 中查找应用程序的当前事件

python - 如何使用 Python 的 Matplotlib 在条形图上添加显着性水平?

java - 如何在不同的PC上运行jar文件(具有selenium自动化、webdriver)