求助登录界面弹出警告窗口找不到元素,希望各位各位帮个忙看一下
# -*- coding: utf-8 -*-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
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Ets2(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "http://etspad.knoocrc.cn"
self.verificationErrors = []
self.accept_next_alert = True
def test_ets2(self):
driver = self.driver
driver.get(self.base_url)
driver.maximize_window()
driver.find_element_by_link_text(u"参与测评").click()
driver.find_element_by_class_name("btn").click()
driver.switch_to_alert().accept
try: self.assertEqual(u"请输手机号码!", driver.find_element_by_css_selector("div.layui-layer-content").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
driver.find_element_by_name("clientMobile").clear()
driver.find_element_by_name("clientMobile").send_keys("123456")
driver.find_element_by_class_name("btn").click()
driver.switch_to_alert().accept
try: self.assertEqual(u"请输正确的手机号码!", driver.find_element_by_css_selector("div.layui-layer-content").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
driver.find_element_by_name("clientMobile").clear()
driver.find_element_by_name("clientMobile").send_keys("13000000029")
driver.find_element_by_class_name("btn").click()
driver.switch_to_alert().accept
try: self.assertEqual(u"请输入姓名!", driver.find_element_by_xpath("//*[@id='layui-layer10']/div").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
driver.find_element_by_name("clientName").clear()
driver.find_element_by_name("clientName").send_keys("123")
driver.find_element_by_class_name("btn").click()
driver.switch_to_alert().accept
try: self.assertEqual(u"姓名只能为中文!", driver.find_element_by_xpath("//*[@id=1layui-layer11']/div").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
driver.find_element_by_name("clientName").clear()
driver.find_element_by_name("clientName").send_keys("12@#aas")
driver.find_element_by_class_name("btn").click()
driver.switch_to_alert().accept
try: self.assertEqual(u"姓名只能为中文!", driver.find_element_by_xpath("//*[@id=1layui-layer11']/div").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
driver.find_element_by_name("clientName").clear()
driver.find_element_by_name("clientName").send_keys(u"啊")
driver.find_element_by_class_name("btn").click()
driver.switch_to_alert().accept
try: self.assertEqual(u"输入的姓名长度不能小于2!", driver.find_element_by_xpath("//*[@id='layui-layer12']/div").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
driver.find_element_by_name("clientName").clear()
driver.find_element_by_name("clientName").send_keys(u"啊少时诵诗书")
driver.find_element_by_class_name("btn").click()
driver.switch_to_alert().accept
try: self.assertEqual(u"输入的姓名长度不能超过5!", driver.find_element_by_xpath("//*[@id='layui-layer13']/div").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
driver.find_element_by_name("clientName").clear()
driver.find_element_by_name("clientName").send_keys(u"少时诵诗书")
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as 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()
学习 黑盒测试 发表于 2017-1-10 10:16
学习
什么学习 你的脚本没啥问题,主要是网页上有bug。
另外还是建议你的脚本适当加些等待时间和注释。
# -*- coding: utf-8 -*-
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
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Ets2(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://etspad.knoocrc.cn"
self.verificationErrors = []
self.accept_next_alert = True
def test_ets2(self):
#打开测试网页
driver = self.driver
driver.get(self.base_url)
driver.maximize_window()
driver.find_element_by_link_text(u"参与测评").click()
driver.find_element_by_class_name("btn").click()
#测试两者不输入时,提示信息:请输手机号码
try: self.assertEqual(u"请输手机号码!", driver.find_element_by_css_selector("div.layui-layer-content").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
time.sleep(3)
#测试输入非法的手机号,提示信息:请输正确的手机号码
driver.find_element_by_name("clientMobile").clear()
driver.find_element_by_name("clientMobile").send_keys("123456")
driver.find_element_by_class_name("btn").click()
try: self.assertEqual(u"请输正确的手机号码!", driver.find_element_by_css_selector("div.layui-layer-content").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
time.sleep(3)
#测试输入正确的手机号码,不输入姓名,提示信息:请输入姓名!
driver.find_element_by_name("clientMobile").clear()
driver.find_element_by_name("clientMobile").send_keys("13000000029")
driver.find_element_by_class_name("btn").click()
time.sleep(2)
#这里有个bug:需要点击两次才提交
driver.find_element_by_class_name("btn").click()
try: self.assertEqual(u"请输入姓名!", driver.find_element_by_xpath("//*[@class='layui-layer-content']").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
time.sleep(3)
#测试输入非法姓名,场景1,提示信息: 姓名只能为中文!
driver.find_element_by_name("clientName").clear()
driver.find_element_by_name("clientName").send_keys("123")
driver.find_element_by_class_name("btn").click()
time.sleep(2)
driver.find_element_by_class_name("btn").click()
try: self.assertEqual(u"姓名只能为中文!", driver.find_element_by_xpath("//*[@class='layui-layer-content']").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
time.sleep(3)
#测试输入非法姓名,场景2,提示信息: 姓名只能为中文!
driver.find_element_by_name("clientName").clear()
driver.find_element_by_name("clientName").send_keys("12@#aas")
driver.find_element_by_class_name("btn").click()
time.sleep(2)
driver.find_element_by_class_name("btn").click()
try: self.assertEqual(u"姓名只能为中文!", driver.find_element_by_xpath("//*[@class='layui-layer-content']").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
time.sleep(3)
#测试输入姓名的长度,小于2,提示信息:输入的姓名长度不能小于2!
driver.find_element_by_name("clientName").clear()
driver.find_element_by_name("clientName").send_keys(u"啊")
driver.find_element_by_class_name("btn").click()
time.sleep(2)
driver.find_element_by_class_name("btn").click()
try: self.assertEqual(u"输入的姓名长度不能小于2!", driver.find_element_by_xpath("//*[@class='layui-layer-content']").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
time.sleep(3)
#测试输入姓名的长度,大于5,提示信息:输入的姓名长度不能超过5!
driver.find_element_by_name("clientName").clear()
driver.find_element_by_name("clientName").send_keys(u"啊少时诵诗书")
driver.find_element_by_class_name("btn").click()
time.sleep(2)
driver.find_element_by_class_name("btn").click()
try: self.assertEqual(u"输入的姓名长度不能超过5!", driver.find_element_by_xpath("//*[@class='layui-layer-content']").text)
except AssertionError as e: self.verificationErrors.append(e)
driver.find_element_by_class_name("layui-layer-btn0").click()
time.sleep(3)
#测试..........
driver.find_element_by_name("clientName").clear()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as 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()
若尘_51 发表于 2017-1-10 11:32
你的脚本没啥问题,主要是网页上有bug。
另外还是建议你的脚本适当加些等待时间和注释。
谢谢,非常感谢
页:
[1]