我的问题:能够检测到警报框已打开,请验证警报中的文本,确认文本,然后关闭警报。然后,返回并更正无效的电子邮件。
我的测试是用于注册用户的测试。我需要验证何时有人可能输入了错误的数据或不匹配的数据。在这种情况下,我要输入电子邮件并验证首次输入的电子邮件。如果它们不匹配,则会出现一个弹出窗口,警告用户检查他们输入的和匹配的电子邮件地址。到目前为止,我只能得到一个错误。
错误:
E UnexpectedAlertPresentException:消息:存在“模态对话框”;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)
Run Code Online (Sandbox Code Playgroud)
在较新版本的 Python(3.4) 上
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException:
return False
Run Code Online (Sandbox Code Playgroud)
尝试等待警报出现,然后再直接切换到它。
如果这不起作用,我有一种感觉,弹出窗口实际上是一个 webment,而不是 JS 警报。如果是这种情况,请尝试使用浏览器开发人员工具查找选择器并直接与其交互。
| 归档时间: |
|
| 查看次数: |
21038 次 |
| 最近记录: |