'' '
Alert assertion, given as follows:
TypeError:' Alert 'object is not callable
Analysis:
' Alert 'IS not a Callable Object Alert can not be the meaning of a function call, it is not a function.
Solution: remove the parentheses after the alert.
Correct codes:
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, being given as follows:
ImportError: Start Directory IS not importable: '... / functional test automation / exec318 /'
Cause Analysis: same directory path right
solution: unittest.defaultTestLoader.discover parallel path directly with "./"
unittest Import
Import os
from Time Import strftime
# sys.path.append ( "the I: \\ the Test \ functional test automation \ exec318 \")
# Note: The syntax support HTMLTestRunner.py python3 Lib placed under python36 path
from Lib.HTMLTestRunner import HTMLTestRunner
'' '
Note: If you use @ddt to decorate a test case class, not a single batch run some tests, it can only run with the whole class.
'' '
IF name == " main ":
# create a test report storage directory
the try:
os.mkdir ( "... / Report")
the except Exception AS E:
Print (E)
# create a saved test results file
html_report_file = open ( "... / report /register_test_report_%s.html "the strftime% ("% m% D_ the Y% H% m% S% ")," WB ")
# call HTMLTestRunner class generation html format report test run
runner = HTMLTestRunner (stream = html_report_file, title = "the Report the Test",
Description = "TestCase of The State of The RUN")
Suite1 = unittest.defaultTestLoader.discover ( "./", "* testcase.py")
# = Suite1 unittest.TestLoader (). loadTestsFromTestCase (ECShop_BG_Login_TestCase)
# = unittest.defaultTestLoader.discover Suite1 ( "... / TestCase /", "* testcase.py")
Runner.run(suite1)
# runner.run(allTest())
html_report_file.close()
'' '
To read the data file 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())
'' '
Element positioning module base class
' ''
from Import By selenium.webdriver.common.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
MyTestCaseBase class (of unittest.TestCase):
DEF the setUp (Self):
self.driver webdriver.Firefox = ()
# self.driver.implicitly_wait (10)
self.accept_next_alert = True
DEF the tearDown (Self):
self.driver.quit ()
# page elements to determine whether there
DEF is_element_present (Self, How, the What):
the try:
self.driver.find_element (by = How, value = the What)
the except NoSuchElementException AS E:
return False
return True
# judge whether the pop-up box appears
def is_alert_present ( Self):
the try:
self.driver.switch_to.alert
the except NoAlertPresentException AS E:
return False
return True
# close the pop-up box (the default click "OK"), and to obtain the text
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()
'' '
Test example
' ''
Import the unittest
Import SYS
A wording:
sys.path.append(“D:\PycharmProjects\myproject1\”)
Written two:
sys.path.append("…")
sys.path.append("…/…")
Written three:
import os
curPath = os.path.abspath(os.path.dirname(file))
print(“curPath=”,curPath)
rootPath = os.path.split(curPath)[0]
print(“rootPath=”,rootPath)
sys.path.append(rootPath)
rootPath1 = os.path.split(rootPath)[0]
print(“rootPath1=”,rootPath1)
sys.path.append(rootPath1)
print(sys.path)
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
from ecshop.util.read_excel import ExcelUtil
def getCsvData():
# Read CSV file
value_rows = []
with open ( "D: \ PycharmProjects \ myproject1 \ ecshop \ data \ data _ECShop_ background to add profile .csv", "r") as f:
f_csv = csv.reader(f)
next (f_csv) # If start reading data from the first two, is added to the line
for r in f_csv:
value_rows.append®
return value_rows
getCsvData the Testdata = # ()
# = CSVUtil the Testdata ( "D: \ PycharmProjects \ myproject1 \ ecshop \ the Data \ data _ECShop_ background to add profile .csv") list_data ().
the Testdata = CSVUtil ( "... \ the Data \ data _ECShop _ Add membership backstage .csv ") list_data ().
# = ExcelUtil the Testdata (" D: \ PycharmProjects \ myproject1 \ ecshop \ the data \ data _ECShop_ background to add profile .xlsx "," background to add profile ") .list_data ()
# testdata = ExcelUtil ( "... \ data \ data _ECShop_ background to add profile .xlsx", "background to add profile") .list_data ()
#Print (the 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()
# Foreground to test the account
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 ( "Login successful", 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()