我的问题:能够检测到警报框已打开,验证警报内的文本,确认文本然后关闭警报。然后,返回并更正无效的电子邮件。
我的测试是用于注册用户的测试。我需要验证何时有人可能输入不正确的数据或不匹配的数据。在这种情况下,我正在输入电子邮件并验证第一次输入的电子邮件。如果它们不匹配,则会出现一个弹出窗口,提醒用户检查他们输入的电子邮件地址是否匹配。到目前为止,我能得到的只是一个错误。
错误:
E UnexpectedAlertPresentException: Message: u'Modal dialog present' ; Stacktrace:方法 nsCommandProcessor.prototype.execute 在 file:///var/folders/1j/x3cxkrqn3sscdyq0t36169hr0000gn/T/tmpTCqMgm/extensions/fxdriver@googlecode.com/components/command_processor.js 中抛出错误
我以为我的代码会处理这个问题,但没有。如果有人能指出我对你来说很明显的错误,我将不胜感激。
我的整个测试代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class ChallengeTests(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(5)
self.base_url = "https://www.testpage.com"
self.verificationErrors = []
self.accept_next_alert = True
# SIGN UP NEW USER
def test_00_sign_up(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("remail").send_keys("foobar@me.com")
driver.find_element_by_id("remail_confirm").send_keys("bar@me.com")
driver.find_element_by_id("next").click()
alert = self.driver.switch_to_alert()
alert = self.assertTrue(self.is_text_present("The email addresses you entered"))
driver.find_element_by_id("remail_confirm").send_keys("foobar@me.com")
driver.find_element_by_id("registration_button").click()
# NON TESTS
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what) # to find page elements
except NoSuchElementException, e: return False
return True
def is_text_present(self, text):
try:
body = self.driver.find_element_by_tag_name("body") # find body tag element
except NoSuchElementException, e: return False
return text in body.text # check if the text is in body's text
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException, e:
return False
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
return True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)