怎么做“没有pytest.raises”?

时间:2018-12-18 10:21:36

标签: python selenium pytest

我想取消对话框并检查是否未显示javascript警报。我该怎么做? “ with not pytest.raises”不起作用。谢谢

# try to cancel the dialog and check that JavaScript alert is displayed
with pytest.raises(UnexpectedAlertPresentException):
    page.cancel()

1 个答案:

答案 0 :(得分:1)

UnexpectedAlertPresentException在您不希望收到警报的情况下发出,但实际上确实存在警报,而您需要NoAlertPresentException在您要处理警报的情况下发出通知from selenium.common.exceptions import NoAlertPresentException with pytest.raises(NoAlertPresentException): driver.switch_to.alert() ,但没有警报:

NoAlertPresentException

该测试应通过,因为我们正试图切换到不存在的警报,并且{{1}}达到了我们的预期