软件测试肖sir__python之ui自动化测试框架作业案例
一、作业:编写5条自动化用例
案例1:
import unittest
import time
from dcs.HTMLTestRunner3_New import HTMLTestRunner
from selenium.webdriver.support.select import Select
from selenium import webdriver
from time import sleep
class zy2(unittest.TestCase):
def setUp(self) -> None:
self.dx = webdriver.Chrome()
self.dx.maximize_window()
self.dx.get('http://cms.duoceshi.cn/cms/manage/login.do')
self.dx.find_element_by_id('userAccount').send_keys('admin')
self.dx.find_element_by_id('loginPwd').send_keys('123456')
self.dx.find_element_by_name('online').click()
self.dx.find_element_by_id('loginBtn').click()
sleep(3)
def tearDown(self) -> None:
time.sleep(3)
self.dx.close()
def test1(self):#添加用户
self.dx.find_element_by_class_name('icon-user').click()
self.dx.find_element_by_link_text('用户管理').click()
kj = self.dx.find_element_by_name('/cms/manage/user-list.html')
self.dx.switch_to.frame(kj)
self.dx.find_element_by_link_text('添加用户').click()
sleep(2)
kj1 = self.dx.find_element_by_class_name("xubox_iframe")
sleep(2)
self.dx.switch_to.frame(kj1)
sleep(2)
self.dx.find_element_by_id('userName').send_keys('lu')
sleep(2)
self.dx.find_element_by_id("userSex").click()
sleep(2)
self.dx.find_element_by_id('user-tel').send_keys('13216158890')
sleep(2)
self.dx.find_element_by_id('user-email').send_keys('2345678715@qq.com')
sleep(2)
self.dx.find_element_by_id('userAccount').send_keys('13216158890')
sleep(2)
self.dx.find_element_by_id('loginPwd').send_keys('123456')
sleep(2)
self.dx.find_element_by_id('confirmPwd').send_keys('123456')
sleep(2)
self.dx.find_element_by_id('submitBtn').click()
sleep(2)
self.dx.switch_to.default_content()
sleep(2)
# self.dx.find_element_by_link_text('退出').click()
def test2(self):#删除用户
self.dx.find_element_by_class_name('icon-user').click()
sleep(3)
self.dx.find_element_by_link_text('用户管理').click()
sleep(3)
kj=self.dx.find_element_by_name('/cms/manage/user-list.html')
self.dx.switch_to.frame(kj)
self.dx.find_element_by_name('sysUserChk').click()
sleep(1)
self.dx.find_element_by_class_name('icon-trash').click()
sleep(1)
self.dx.find_element_by_link_text("确定").click()
def test3(self):#修改密码
self.dx.find_element_by_class_name('icon-user').click()
sleep(3)
self.dx.find_element_by_link_text('用户管理').click()
sleep(3)
kj = self.dx.find_element_by_name('/cms/manage/user-list.html')
self.dx.switch_to.frame(kj)
self.dx.find_element_by_xpath('//*[@id="sysUserTab"]/tr[2]/td[9]/a[3]').click()
kjx=self.dx.find_element_by_class_name('xubox_iframe')
self.dx.switch_to.frame(kjx)
self.dx.find_element_by_id('loginPwd').send_keys("1234567")
sleep(2)
self.dx.find_element_by_id('loginPwd2').send_keys("1234567")
sleep(1)
self.dx.find_element_by_id("#submitBtn").click()
self.dx.switch_to.default_content()
def test4(self):#资讯
self.dx.find_element_by_id('menu-system').click()
sleep(1)
self.dx.find_element_by_link_text('栏目设置').click()
sleep(1)
kj3 = self.dx.find_element_by_name('/cms/manage/article-class.html')
self.dx.switch_to.frame(kj3)
self.dx.find_element_by_class_name('icon-plus').click()
sleep(1)
kj4 = self.dx.find_element_by_class_name('xubox_iframe')
self.dx.switch_to.frame(kj4)
self.dx.find_element_by_name('categoryName').send_keys('测试1')
sleep(1)
wz = self.dx.find_element_by_id('parentId')
Select(wz).select_by_value('34')
sleep(1)
self.dx.find_element_by_id('categoryCode').send_keys('测啊')
sleep(1)
self.dx.find_element_by_name('categoryDesc').send_keys('sdfgh')
sleep(1)
self.dx.find_element_by_id('submitBtn').click()
sleep(1)
self.dx.switch_to.default_content()
def test5(self):
self.dx.find_element_by_id('menu-system').click()
sleep(1)
self.dx.find_element_by_link_text('文章管理').click()
sleep(1)
kj7 = self.dx.find_element_by_name('/cms/manage/article-list.html')
self.dx.switch_to.frame(kj7)
self.dx.find_element_by_class_name('icon-hand-down').click()
sleep(1)
if __name__ == '__main__':
lj=r'C:\Users\Administrator\PycharmProjects\untitled39\dcs'
d=unittest.TestLoader().discover(start_dir=lj,pattern="zy02*")
bglj=r"C:\Users\Administrator\PycharmProjects\untitled39\dcs"
new=time.strftime("%y-%m-%d-%H-%M-%S")
file=bglj+"/"+str(new)+"_ui.html"
f=open(file,'wb')
r=HTMLTestRunner(stream=f,title="ui自动化测试报告",description="用例执行情况" ,tester="lu35")
r.run(d)
案例2:
from selenium import webdriver
from time import sleep
import os
import time
from UI.HTMLTestRunner3_New import HTMLTestRunner
import unittest
from parameterized import parameterized
class Cms(unittest.TestCase): # 创建一个类,并且集成unittest.TestCase
# test_data1 = [('admin', '123456')]
#
# @parameterized.expand(test_data1)
def setUp(self) -> None: # 方法的开始
print('方法开始') # 运行前准备工作
self.dx = webdriver.Chrome()
self.dx.get("http://cms.duoceshi.cn/cms/manage/login.do")
self.dx.maximize_window()
self.dx.find_element_by_id('userAccount').send_keys("admin")
sleep(1)
self.dx.find_element_by_id('loginPwd').send_keys("123456")
sleep(1)
self.dx.find_element_by_name('online').click()
sleep(1)
self.dx.find_element_by_id('loginBtn').click()
sleep(2)
def tearDown(self) -> None:
print('方法结束') # 运行后的工作关闭窗口,时间等待
sleep(2)
self.dx.close()
test_data2 = [('张de', '1', '13783245678', '123456789@163.com', 'test9', '123456', '123456')]
@parameterized.expand(test_data2)
def test_1_add(self, username, sex, phone, email, account, password, password1):
self.dx.find_element_by_class_name("icon-user").click()
sleep(2)
self.dx.find_element_by_link_text("用户管理").click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
self.dx.find_element_by_class_name("icon-plus").click()
ifm2 = self.dx.find_element_by_class_name("xubox_iframe")
self.dx.switch_to.frame(ifm2)
self.dx.find_element_by_id("userName").send_keys(username)
sleep(1)
self.dx.find_element_by_id("userSex").send_keys(sex)
sleep(1)
self.dx.find_element_by_id("user-tel").send_keys(phone)
sleep(1)
self.dx.find_element_by_id("user-email").send_keys(email)
sleep(1)
self.dx.find_element_by_id("userAccount").send_keys(account)
sleep(1)
self.dx.find_element_by_id("loginPwd").send_keys(password)
sleep(1)
self.dx.find_element_by_id("confirmPwd").send_keys(password1)
sleep(1)
self.dx.find_element_by_id("submitBtn").click()
sleep(2)
def test_2_update1(self):
self.dx.find_element_by_class_name("icon-user").click()
sleep(2)
self.dx.find_element_by_link_text("用户管理").click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
self.dx.find_element_by_xpath('//*[@id="sysUserTab"]/tr[1]/td[9]/a[2]').click()
sleep(1)
ifm2 = self.dx.find_element_by_class_name("xubox_iframe")
self.dx.switch_to.frame(ifm2)
self.dx.find_element_by_id("userName").clear()
sleep(1)
self.dx.find_element_by_id("userName").send_keys("哇你个头")
sleep(2)
self.dx.find_element_by_id("submitBtn").click()
sleep(2)
def test_3_update2(self):
self.dx.find_element_by_class_name("icon-user").click()
sleep(2)
self.dx.find_element_by_link_text("用户管理").click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
self.dx.find_element_by_xpath('//*[@id="sysUserTab"]/tr[1]/td[9]/a[3]').click()
ifm3 = self.dx.find_element_by_class_name("xubox_iframe")
self.dx.switch_to.frame(ifm3)
sleep(1)
self.dx.find_element_by_id("loginPwd").send_keys("123456789")
self.dx.find_element_by_id("loginPwd2").send_keys("123456789")
sleep(2)
self.dx.find_element_by_id("#submitBtn").click()
sleep(2)
test_data1 = [('csv647491821')]
@parameterized.expand(test_data1)
def test_4_search(self, username):
self.dx.find_element_by_class_name("icon-user").click()
sleep(2)
self.dx.find_element_by_link_text("用户管理").click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
# self.dx.find_element_by_id("searchValue").send_keys(username)
# sleep(2)
# self.dx.find_element_by_id("searchBtn").click()
# sleep(2)
# self.dx.find_element_by_xpath('//*[@id="sysUserTab"]/tr[1]/td[9]/a[4]').click()
table = self.dx.find_element_by_xpath('/html/body/div/table') # 定位网页表格位置
a_tags = table.find_elements_by_tag_name('td') # 获取所有a标签
list1 = []
for i in a_tags:
list1.append(i.text)
a = list1.index(username)
b = list1[a - 1]
self.dx.find_element_by_xpath('//*[@id="sysUserTab"]/tr[{}]/td[9]/a[4]'.format(b)).click()
sleep(1)
self.dx.find_element_by_link_text("确定").click()
sleep(3)
def test_5_stop(self):
self.dx.find_element_by_class_name("icon-user").click()
sleep(2)
self.dx.find_element_by_link_text("用户管理").click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
self.dx.find_element_by_xpath('//*[@id="sysUserTab"]/tr[1]/td[9]/a[1]').click()
sleep(2)
self.dx.find_element_by_link_text("确定").click()
sleep(2)
if __name__ == '__main__':
# 获取当前脚本文件的绝对路径
script_path = os.path.abspath(__file__)
# 获取程序所在目录
dir_path = os.path.dirname(script_path)
d = unittest.TestLoader().discover(start_dir=dir_path, pattern="ui3*")
new = time.strftime("%y-%m-%d-%H-%M-%S")
file = dir_path + "/" + str(new) + "_ui1.html"
f = open(file, 'wb')
r = HTMLTestRunner(
stream=f, title="ui自动化测试报告", description="用例执行情况", tester="zjy"
)
r.run(d)
案例3:
import unittest
from selenium import webdriver
from time import sleep,strftime
from webdriver_manager.chrome import ChromeDriverManager
from UI.HTMLTestRunner3_New import HTMLTestRunner
# dx = webdriver.Chrome
# dx.get("https://cms.duoceshi.cn/cms/manage/index.do")
# sleep(2)
# dx.maximize_window()
# sleep(2)
# dx.find_element_by_id("userAccount").send_keys("admin")
# sleep(2)
# dx.find_element_by_id("loginPwd").send_keys("123456")
# sleep(2)
# dx.find_element_by_id("loginBtn").click()
# sleep(2)
class Cms(unittest.TestCase):
# dx = webdriver.Chrome(ChromeDriverManager().install())
# dx.get("http://cms.duoceshi.cn/cms/manage/index.do")
# sleep(2)
# dx.maximize_window()
# sleep(2)
# dx.find_element_by_id('userAccount').send_keys("admin")
# sleep(2)
# dx.find_element_by_id('loginPwd').send_keys("123456")
# sleep(2)
# dx.find_element_by_id('loginBtn').click()
# sleep(2)
@classmethod
def setUpClass(cls) -> None:
print("测试开始")
@classmethod
def tearDownClass(cls) -> None:
print("测试结束")
def setUp(self) -> None:
self.dx = webdriver.Chrome(ChromeDriverManager().install())
self.dx.get("http://cms.duoceshi.cn/cms/manage/index.do")
sleep(2)
self.dx.maximize_window()
sleep(2)
self.dx.find_element_by_id('userAccount').send_keys("admin")
sleep(2)
self.dx.find_element_by_id('loginPwd').send_keys("123456")
sleep(2)
self.dx.find_element_by_id('loginBtn').click()
sleep(2)
def tearDown(self) -> None:
self.dx.quit()
def test1(self):
self.dx.find_element_by_class_name('icon-user').click()
sleep(2)
self.dx.find_element_by_link_text('用户管理').click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
sleep(2)
self.dx.find_element_by_link_text('添加用户').click()
sleep(2)
ifm2=self.dx.find_element_by_name('xubox_iframe1')
self.dx.switch_to.frame(ifm2)
sleep(1)
self.dx.find_element_by_id('userName').send_keys("张三")
sleep(2)
self.dx.find_element_by_id('user-tel').send_keys(17367189223)
sleep(2)
self.dx.find_element_by_id('user-email').send_keys("757622965@qq.com")
sleep(2)
self.dx.find_element_by_id('userAccount').send_keys('admin2')
sleep(2)
self.dx.find_element_by_id('loginPwd').send_keys('123456')
sleep(2)
self.dx.find_element_by_id('confirmPwd').send_keys('123456')
sleep(2)
self.dx.find_element_by_id('submitBtn').click()
print("添加用户成功")
def test2(self):
self.dx.find_element_by_class_name('icon-user').click()
sleep(2)
self.dx.find_element_by_link_text('用户管理').click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
sleep(2)
self.dx.find_element_by_id('searchValue').send_keys('csv979996669')
sleep(2)
self.dx.find_element_by_class_name('icon-search').click()
sleep(1)
self.dx.find_element_by_class_name('icon-refresh').click()
sleep(5)
def test3(self):
self.dx.find_element_by_class_name('icon-user').click()
sleep(2)
self.dx.find_element_by_link_text('用户管理').click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
sleep(2)
self.dx.find_element_by_id('searchValue').send_keys('csv')
sleep(2)
self.dx.find_element_by_class_name('icon-search').click()
sleep(5)
def test4(self):
self.dx.find_element_by_class_name('icon-user').click()
sleep(2)
self.dx.find_element_by_link_text('用户管理').click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
sleep(2)
self.dx.find_element_by_xpath('/html/body/div/table/tbody/tr[3]/td[9]/a[4]/i').click()
sleep(3)
self.dx.find_element_by_link_text('确定').click()
def test5(self):
self.dx.find_element_by_class_name('icon-user').click()
sleep(2)
self.dx.find_element_by_link_text('用户管理').click()
sleep(2)
ifm1 = self.dx.find_element_by_name("/cms/manage/user-list.html")
self.dx.switch_to.frame(ifm1)
sleep(2)
self.dx.find_element_by_xpath('/html/body/div/table/tbody/tr[4]/td[9]/a[1]/i').click()
sleep(1)
self.dx.find_element_by_link_text('确定').click()
sleep(3)
if __name__ == '__main__':
lj=r"D:\py\UI"
d=unittest.TestLoader().discover(start_dir=lj,pattern="zzz*")
bglj=r"D:\py\UI"
new=strftime("%y-%m-%d-%H-%M-%S")
file=bglj+'/'+new+'unittest.html'
f=open(file,'wb')
r=HTMLTestRunner(
stream=f, title='ui自动化报告', description='多测师运行用例',
tester='zhw'
)
r.run(d)
# from webdriver_manager.chrome import ChromeDriverManager
# from selenium import webdriver
# from time import sleep
# import os
# import time
# dx = webdriver.Chrome(ChromeDriverManager().install())
# dx.get("http://cms.duoceshi.cn/cms/manage/login.do")
# dx.maximize_window()
# dx.find_element_by_id('userAccount').send_keys("admin")
# sleep(1)
# dx.find_element_by_id('loginPwd').send_keys("123456")
# sleep(1)
# dx.find_element_by_name('online').click()
# sleep(1)
# dx.find_element_by_id('loginBtn').click()
# sleep(2)
# dx.find_element_by_class_name("icon-user").click()
# sleep(2)
# dx.find_element_by_link_text("用户管理").click()
# sleep(2)
# ifm1 = dx.find_element_by_name("/cms/manage/user-list.html")
# dx.switch_to.frame(ifm1)
# table = dx.find_element_by_xpath('html/body/div/table') # 定位网页表格位置
# a_tags = table.find_elements_by_tag_name('td') # 获取所有a标签
# list1 = []
# for i in a_tags:
# list1.append(i.text)
# a=list1.index(input("想删除的名字:"))
# b=list1[a-1]
# dx.find_element_by_xpath("/html/body/div/table/tbody/tr[b]/td[9]/a[4]/i").click()