0
*我正在使用Python中的Selenium Webdriver進行WebPage的測試用例自動化。
*我需要驗證在網頁上生成的警報消息。
*使用Webdriver當我嘗試處理警報時,它表示沒有發現警報。即使我沒有處理警報,它也不會給出任何錯誤消息(如生成的未接警報)。
*請建議,爲什麼Webdriver沒有檢測到警報。Python Selenium webdriver:未檢測到生成的警報,也沒有提供錯誤
Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class Tcgr1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://colama5863.co5863domain.net/infracc/app/Workspace#VMs"
self.accept_next_alert = True
def test_tcgr1(self):
driver = self.driver
driver.find_element_by_link_text("Settings").click()
driver.find_element_by_link_text("Group Mgmt").click()
driver.find_element_by_xpath("//button[contains(text(), \"Create\")]").click()
driver.find_element_by_name("groupname").clear()
driver.find_element_by_name("groupname").send_keys("tcgr1_group")
driver.find_element_by_xpath("//span[2]/span/button[contains(text(), \"Create\")]").click()
for i in range(60):
try:
if self.is_alert_present(): break
except: pass
time.sleep(1)
else: self.fail("time out")
self.assertEqual("Group created successfully", self.close_alert_and_get_its_text())
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
你可以發佈你正在使用的代碼。您是使用隱式等待函數還是使用更可靠的預期條件等待函數? – duhaime
請給出一些建議...我已經添加了測試用例代碼。 – John