簡體   English   中英

如何使用Python在Selenium中測試Alert / Popup

[英]How to test Alert/Popup in Selenium using Python

我對自動化感到困惑,當我輸入錯誤的用戶名和密碼時,它會彈出,提示“無法登錄。請嘗試使用其他用戶名”

def test_logonWrongUserName(self):
    self.setUpClass() # Initialize the driver 
    self.sh.navigateToUrl("http://test.com")
    self.sh.member_Login("Foo", "asd") 

現在需要測試具有文本“無法登錄。嘗試使用其他用戶名”的警報或彈出窗口(不確定是警報還是彈出窗口)。 如果找到文本,則測試通過。 如果不是,則測試必須失敗。

注意:只需添加,在關閉警報/彈出窗口之前,我們無法訪問html dom

您需要等待警報出現 ,斷言您內部有預期的文本 ,然后關閉/ 接受警報:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

wait = WebDriverWait(driver, 10)
wait.until(EC.alert_is_present())

alert = driver.switch_to.alert
assert "Unable to login. Try different username" in alert.text
alert.accept()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM