‘’’
Alert断言,报错如下:
TypeError: ‘Alert’ object is not callable
原因分析:
‘Alert’ object is not callable 的含义为Alert不能被函数调用,它不是一个函数。
解决方案:将alert后的括号去掉。
正确代码:
browser.switch_to.alert.accept()
‘’’
#-- coding: utf-8 --
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class AlertDemo(unittest.TestCase):
def is_alert_present(self):
#self.driver.switch_to.alert去掉后面括号
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
if name == “main”:
unittest.main()
testsuite,discover,报错如下:
ImportError: Start directory is not importable: ‘…/功能自动化测试/exec318/’
原因分析:同级目录路径不对
解决方案:unittest.defaultTestLoader.discover同级路径直接用"./"
import unittest
import os
from time import strftime
#sys.path.append(“I:\\test\功能自动化测试\exec318\”)
#注意:将支持python3语法格式的HTMLTestRunner.py放在python36路径下的Lib中
from Lib.HTMLTestRunner import HTMLTestRunner
‘’’
注意:如果使用@ddt来装饰测试用例类,那么无法单独批量运行一些测试方法,只能整个类一起运行。
‘’’
if name == “main”:
# 创建测试报告存放目录
try:
os.mkdir("…/report")
except Exception as e:
print(e)
# 创建保存测试结果的文件
html_report_file = open("…/report/register_test_report_%s.html" % strftime("%Y%m%d_%H%M%S"), “wb”)
# 调用HTMLTestRunner类生成html格式测试运行报告
runner = HTMLTestRunner(stream=html_report_file, title=“Test Report”,
description=“The state of the run testcase”)
suite1 = unittest.defaultTestLoader.discover("./", “*testcase.py”)
# suite1 = unittest.TestLoader().loadTestsFromTestCase(ECShop_BG_Login_TestCase)
# suite1 = unittest.defaultTestLoader.discover("…/testcase/", “*testcase.py”)
runner.run(suite1)
# runner.run(allTest())
html_report_file.close()
‘’’
读取CSV文件数据
‘’’
import csv
class CSVUtil():
def init(self, filePath):
self.filePath = filePath
def list_data(self):
# 读取CSV文件
value_rows = []
with open(self.filePath, "r") as f:
f_csv = csv.reader(f)
next(f_csv) # 如果从第2个开始读取数据,则加入该行代码
for r in f_csv:
value_rows.append(r)
return value_rows
if name==“main”:
filePath="…/exec318/添加会员.csv"
data=CSVUtil(filePath)
print(data.list_data())
‘’’
元素定位模块基础类
‘’’
from selenium.webdriver.common.by import By
class ECShop_FP_BasePage_Locator():
def init(self):
self.find_logout_link=(By.LINK_TEXT,“退出”)
self.find_login_img=(By.XPATH,"//font[@id=‘ECS_MEMBERZONE’]/a[1]/img")
self.find_register_img=(By.XPATH, “//font[@id=‘ECS_MEMBERZONE’]/a[2]/img”)
self.find_username_in_head_txt=(By.XPATH,".//*[@id=‘ECS_MEMBERZONE’]/font/font")
‘’’
业务逻辑基础类Base_Module
‘’’
import csv
class ECShop_BG_SubPage_Base_Module():
def init(self,driver):
self.driver=driver
def switch_to_home_page(self):
self.driver.switch_to.default_content()
return self.driver
‘’’
测试用例基础类
‘’’
import unittest
from selenium import webdriver
from time import sleep
from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException
from ecshop.module.ecshop_bg_adduserpage_module import ECShop_BG_AddUserPage_Module
from ecshop.module.ecshop_bg_headerpage_module import ECShop_BG_HeaderPage_Module
from ecshop.module.ecshop_bg_homepage_module import ECShop_BG_HomePage_Module
from ecshop.module.ecshop_bg_menupage_module import ECShop_BG_MenuPage_Module
from ecshop.module.ecshop_bg_userlistpage_module import ECShop_BG_UserListPage_Module
from ecshop.module.ecshop_bg_usermessagepage_module import ECShop_BG_UserMessagePage_Module
class MyTestCaseBase(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox()
# self.driver.implicitly_wait(10)
self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
# 判断页面元素是否出现
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 is_title_ends_with(self, t):
return self.driver.title.endswith(t)
# 判断网页标题是否以指定字符串开头
def is_title_starts_with(self, t):
return self.driver.title.startswith(t)
# 判断网页源代码中是否包含指定字符串
def is_pagesource_contains(self, info):
return info in self.driver.page_source
def to_user_list_page(self):
hp = ECShop_BG_HomePage_Module(self.driver)
self.driver = hp.switch_to_menu()
menup = ECShop_BG_MenuPage_Module(self.driver)
self.driver = menup.to_user_list_page()
hp = ECShop_BG_HomePage_Module(self.driver)
self.driver = hp.switch_to_main()
ulp = ECShop_BG_UserListPage_Module(self.driver)
sleep(3)
return ulp
def to_add_user_page(self):
hp=ECShop_BG_HomePage_Module(self.driver)
self.driver=hp.switch_to_menu()
menup=ECShop_BG_MenuPage_Module(self.driver)
self.driver =menup.to_add_user_page()
hp = ECShop_BG_HomePage_Module(self.driver)
self.driver = hp.switch_to_main()
aup=ECShop_BG_AddUserPage_Module(self.driver)
return aup
def to_user_message_page(self):
hp=ECShop_BG_HomePage_Module(self.driver)
self.driver=hp.switch_to_menu()
menup=ECShop_BG_MenuPage_Module(self.driver)
self.driver =menup.to_user_message_page()
hp = ECShop_BG_HomePage_Module(self.driver)
self.driver = hp.switch_to_main()
ump = ECShop_BG_UserMessagePage_Module(self.driver)
return ump
def logout_bg(self):
self.driver.switch_to.default_content()
# 退出
hp = ECShop_BG_HomePage_Module(self.driver)
self.assertTrue(self.is_element_present(*hp.hp_loc.find_header_frame))
self.driver=hp.switch_to_header()
hdp=ECShop_BG_HeaderPage_Module(self.driver)
self.assertTrue(self.is_element_present(*hdp.hdp_loc.find_logout_link))
self.driver=hdp.logout()
return self.driver
’’’
module逻辑运行举例
‘’’
from exec318.register_locator import RegisterLocator
from exec318.register_read_csv import CSVUtil
from time import sleep
from selenium import webdriver
class RegisterModule():
def init(self,driver):
self.driver = driver
self.base_url = “https://account.shopex.cn/reg?shopex”
self.loc = RegisterLocator()
def open(self):
self.driver.get(self.base_url)
def input_data(self,name,verifycode,mobilecode,pwd,contact):
# 手机号
name_input = self.driver.find_element(*self.loc.loc_name)
name_input.clear()
name_input.send_keys(name)
# 验证码
verifycode_input = self.driver.find_element(*self.loc.loc_verifycode)
verifycode_input.clear()
verifycode_input.send_keys(verifycode)
# 手机号验证码
self.driver.find_element(*self.loc.loc_mobileCodeBtn).click()
mobilecode_input = self.driver.find_element(*self.loc.loc_mobilecode)
mobilecode_input.send_keys(mobilecode)
# 登录密码
pwd_input = self.driver.find_element(*self.loc.loc_pwd)
pwd_input.send_keys(pwd)
# 联系人姓名
contact_input = self.driver.find_element(*self.loc.loc_contact)
contact_input.send_keys(contact)
sleep(5)
def register(self,name,verifycode,mobilecode,pwd,contact):
self.input_data(name,verifycode,mobilecode,pwd,contact)
accept_input = self.driver.find_element(*self.loc.loc_accept)
accept_input.click()
register_button = self.driver.find_element(*self.loc.loc_register)
if register_button.is_enabled():
print('注册按钮可用')
return self.driver
def login(self):
login_a = self.driver.find_element(*self.loc.loc_login)
login_a.click()
’’’
测试用例举例
‘’'
import unittest
import sys
from selenium import webdriver
from ecshop.testcase.ecshop_00_testcase_base import MyTestCaseBase
from ecshop.module.ecshop_bg_headerpage_module import ECShop_BG_HeaderPage_Module
from ecshop.module.ecshop_bg_homepage_module import ECShop_BG_HomePage_Module
from ecshop.module.ecshop_bg_loginpage_module import ECShop_BG_LoginPage_Module
from ecshop.module.ecshop_bg_systeminfopage_module import ECShop_BG_SystemInfoPage_Module
from ecshop.module.ecshop_bg_adduserpage_module import ECShop_BG_AddUserPage_Module
from ecshop.module.ecshop_bg_menupage_module import ECShop_BG_MenuPage_Module
from ecshop.module.ecshop_bg_userlistpage_module import ECShop_BG_UserListPage_Module
from ecshop.module.ecshop_bg_edituserpage_module import ECShop_BG_EditUserPage_Module
from ecshop.module.ecshop_fp_loginpage_module import ECShop_FP_LoginPage_Module
from ecshop.module.ecshop_fp_systeminfopage_module import ECShop_FP_SystemInfoPage_Module
import csv
from ddt import ddt, data, unpack
from time import sleep
from ecshop.locator.ecshop_bg_loginpage_locator import ECShop_BG_LoginPage_Locator
from ecshop.util.read_csv import CSVUtil
#testdata=getCsvData()
#testdata=CSVUtil(“D:\PycharmProjects\myproject1\ecshop\data\数据_ECShop_后台添加会员.csv”).list_data()
testdata=CSVUtil("…\data\数据_ECShop_后台添加会员.csv").list_data()
#testdata=ExcelUtil(“D:\PycharmProjects\myproject1\ecshop\data\数据_ECShop_后台添加会员.xlsx”,“后台添加会员”).list_data()
#testdata=ExcelUtil("…\data\数据_ECShop_后台添加会员.xlsx",“后台添加会员”).list_data()
#print(testdata)
@ddt
class ECShop_BG_AddUser_TestCase(MyTestCaseBase):
@data(*testdata)
@unpack
def test_a_bg_adduser_submit(self,testdata_id,testcase_id,username,email,password,confirm_password,
user_rank,sex,birthdayYear,birthdayMonth,birthdayDay,
credit_line,msn,qq,phone1,phone2,phone3,expected_result_id):
print("=")
print(testdata_id)
print(testcase_id)
print(username)
print(email)
print(password)
print(confirm_password)
print(user_rank)
print(sex)
print(birthdayYear)
print(birthdayMonth)
print(birthdayDay)
print(msn)
print(qq)
print(phone1)
print(phone2)
print(phone3)
print(expected_result_id)
print("=")
lp=ECShop_BG_LoginPage_Module(self.driver)
lp.open()
self.driver=lp.login(“admin”,“admin123”,“0”)
sleep(2)
if expected_result_id==“1” and username not in [“text”,“vip”,“ecshop”]:
ulp=self.to_user_list_page()
self.driver = ulp.search(username)
ulp = ECShop_BG_UserListPage_Module(self.driver)
sleep(2)
if ulp.is_user_in_result(username):
ulp.delete_user(username)
self.assertTrue(self.is_alert_present())
self.close_alert_and_get_its_text()
aup=self.to_add_user_page()
self.driver=aup.add_user_submit(username,email,password,confirm_password,
user_rank,sex,birthdayYear,birthdayMonth,birthdayDay,
credit_line,msn,qq,phone1,phone2,phone3)
if expected_result_id==“1”:
sip=ECShop_BG_SystemInfoPage_Module(self.driver)
self.assertTrue(self.is_element_present(*sip.sip_loc.find_previous_page_link))
self.assertTrue(self.is_pagesource_contains(“会员账号 %s 已经添加成功。”%username))
sleep(4)
ulp=ECShop_BG_UserListPage_Module(self.driver)
self.assertTrue(self.is_element_present(*ulp.ulp_loc.find_search_button))
self.driver=ulp.search(username)
ulp = ECShop_BG_UserListPage_Module(self.driver)
sleep(2)
self.assertTrue(ulp.is_user_in_result(username))
self.driver=ulp.edit_target_user(username)
sleep(2)
eup=ECShop_BG_EditUserPage_Module(self.driver)
self.assertEqual(username,eup.get_user_name())
self.assertEqual(email,eup.get_email())
self.assertEqual(user_rank,eup.get_user_rank())
self.assertEqual(sex,eup.get_sex())
self.assertEqual(birthdayYear,eup.get_birthdayYear())
self.assertEqual(birthdayMonth,eup.get_birthdayMonth())
self.assertEqual(birthdayDay,eup.get_birthdayDay())
self.assertAlmostEqual(float(credit_line),float(eup.get_credit_line()))
self.assertEqual(msn,eup.get_msn())
self.assertEqual(qq,eup.get_qq())
self.assertEqual(phone1,eup.get_phone1())
self.assertEqual(phone2,eup.get_phone2())
self.assertEqual(phone3,eup.get_phone3())
eup.switch_to_home_page()
self.driver=self.logout_bg()
# 到前台测试该账号
flp=ECShop_FP_LoginPage_Module(self.driver)
flp.open()
self.driver=flp.login(username,password)
fsip=ECShop_FP_SystemInfoPage_Module(self.driver)
self.assertTrue(self.is_element_present(*fsip.sip_loc.find_logout_link))
msg1=fsip.get_red_info()
self.assertEqual(“登录成功”,msg1)
fsip.logout_fp()
elif expected_result_id=="2":
self.assertTrue(self.is_alert_present())
msg2=self.close_alert_and_get_its_text()
self.assertEqual("- 没有输入用户名。\n",msg2)
self.assertFalse(self.is_alert_present())
aup.switch_to_home_page()
self.logout_bg()
elif expected_result_id=="3":
sip = ECShop_BG_SystemInfoPage_Module(self.driver)
self.assertTrue(self.is_element_present(*sip.sip_loc.find_previous_page_link))
self.assertTrue(self.is_pagesource_contains("已经存在一个相同的用户名。"))
sleep(4)
aup = ECShop_BG_AddUserPage_Module(self.driver)
self.assertTrue(self.is_element_present(*aup.aup_loc.find_username_input))
self.assertTrue(aup.is_pw_and_confirm_pw_empty())
aup.switch_to_home_page()
self.logout_bg()
elif expected_result_id=="4":
self.assertTrue(self.is_alert_present())
msg4=self.close_alert_and_get_its_text()
self.assertEqual("- 输入的密码和确认密码不一致。\n",msg4)
self.assertFalse(self.is_alert_present())
aup.switch_to_home_page()
self.logout_bg()
elif expected_result_id=="5":
self.assertTrue(self.is_alert_present())
msg5=self.close_alert_and_get_its_text()
self.assertEqual("- 没有输入邮件地址或者输入了一个无效的邮件地址。\n",msg5)
self.assertFalse(self.is_alert_present())
aup.switch_to_home_page()
self.logout_bg()
elif expected_result_id=="6":
self.assertTrue(self.is_alert_present())
msg6=self.close_alert_and_get_its_text()
self.assertEqual("- 没有输入密码。\n- 没有输入确认密码。\n- 输入的密码和确认密码不一致。\n- 输入的密码不能少于六位。\n",msg6)
self.assertFalse(self.is_alert_present())
aup.switch_to_home_page()
self.logout_bg()
elif expected_result_id=="7":
self.assertTrue(self.is_alert_present())
msg7=self.close_alert_and_get_its_text()
self.assertEqual("- 没有输入确认密码。\n- 输入的密码和确认密码不一致。\n",msg7)
self.assertFalse(self.is_alert_present())
aup.switch_to_home_page()
self.logout_bg()
elif expected_result_id=="8":
self.assertTrue(self.is_alert_present())
msg8=self.close_alert_and_get_its_text()
self.assertEqual("- 输入的密码不能少于六位。\n",msg8)
self.assertFalse(self.is_alert_present())
aup.switch_to_home_page()
self.logout_bg()
elif expected_result_id=="9":
self.assertTrue(self.is_alert_present())
msg9=self.close_alert_and_get_its_text()
self.assertEqual("- 没有输入邮件地址或者输入了一个无效的邮件地址。\n- 没有输入用户名。\n- 没有输入密码。\n- 没有输入确认密码。\n- 输入的密码和确认密码不一致。\n- 输入的密码不能少于六位。\n",msg9)
self.assertFalse(self.is_alert_present())
aup.switch_to_home_page()
self.logout_bg()
@data(*testdata)
@unpack
def test_b_bg_adduser_reset(self,testdata_id,testcase_id,username,email,password,confirm_password,
user_rank,sex,birthdayYear,birthdayMonth,birthdayDay,
credit_line,msn,qq,phone1,phone2,phone3,expected_result_id):
print("=============")
print(testdata_id)
print(testcase_id)
print(username)
print(email)
print(password)
print(confirm_password)
print(user_rank)
print(sex)
print(birthdayYear)
print(birthdayMonth)
print(birthdayDay)
print(msn)
print(qq)
print(phone1)
print(phone2)
print(phone3)
print(expected_result_id)
print("=============")
lp=ECShop_BG_LoginPage_Module(self.driver)
lp.open()
self.driver=lp.login("admin","admin123","0")
sleep(2)
aup = self.to_add_user_page()
self.driver = aup.add_user_reset(username, email, password, confirm_password,
user_rank, sex, birthdayYear, birthdayMonth, birthdayDay,
credit_line, msn, qq, phone1, phone2, phone3)
aup=ECShop_BG_AddUserPage_Module(self.driver)
self.assertTrue(aup.is_all_reset_to_default())
#
# def to_user_list_page(self):
# hp = ECShop_BG_HomePage_Module(self.driver)
# self.driver = hp.switch_to_menu()
# menup = ECShop_BG_MenuPage_Module(self.driver)
# self.driver = menup.to_user_list_page()
# hp = ECShop_BG_HomePage_Module(self.driver)
# self.driver = hp.switch_to_main()
# ulp = ECShop_BG_UserListPage_Module(self.driver)
# sleep(3)
# return ulp
#
# def to_add_user_page(self):
# hp=ECShop_BG_HomePage_Module(self.driver)
# self.driver=hp.switch_to_menu()
# menup=ECShop_BG_MenuPage_Module(self.driver)
# self.driver =menup.to_add_user_page()
# hp = ECShop_BG_HomePage_Module(self.driver)
# self.driver = hp.switch_to_main()
# aup=ECShop_BG_AddUserPage_Module(self.driver)
# return aup
#
# def logout_bg(self):
# # 退出
# hp = ECShop_BG_HomePage_Module(self.driver)
# self.assertTrue(self.is_element_present(*hp.hp_loc.find_header_frame))
# self.driver=hp.switch_to_header()
# hdp=ECShop_BG_HeaderPage_Module(self.driver)
# self.assertTrue(self.is_element_present(*hdp.hdp_loc.find_logout_link))
# self.driver=hdp.logout()
# return self.driver
if name == ‘main’:
unittest.main()