前置测试类+后置测试类 setup_class+teardown_class(整个流程只运行一遍)
前置测试方法+后置测试方法setup_method+teardown_method
1.有序
@pytest.mark.run(order=xx)
注意:导入插件库,pip install pytest-ordering
2.跳过
普通跳过 @pytest.mark.skip("说明")
满足条件跳过 @pytest.mark.skipif(condition=='冒烟',reason='smoketest')
查看跳过方法原因说明 pytest xxx.py -vs
@pytest.fixture
1.同一个脚本内部参数传递
- @pytest fixture()
- def test_case01():
- num1=60
- return num1
- def test_case02(test_case01):
- num2=test_case01
- print("从方法1传递的值",num2)
2.不同脚本之间参数传递
2.1引入其他脚本对应的测试类或测试方法
from 路径.文件名 import 测试类/测试方法
2.2在其他脚本的相关方法前,需要打@pytest.fixture()标签
- @pytest.fixture()
- def test_case1():
- num1=666
- nlist[1,2,3]
- return nlist
2.3在当前脚本中直接使用
- def test_case2(test_case1)
- num=test_case1
- print("从其他文件通过fixture传入的数据",num)
2.parametrize方法
2.1引入其他脚本对应的测试类或测试方法
2.2只能是普通方法,不能加fixture标签
2.3在当前脚本中是参数标签
- @pytest.mark.parametrize('nun',testcase1())
- def testcase3(num):
- num6=num
- print("从其他文件中通过parametrize传入的数据",num6)
3.使用全局配置文件
- import pytest
- @pytest.fixture()
- def test_data():
- datalist=[1,2,5,8,10]
- return datalist
4.全局变量
- import pytest
- global_list=[1,2,3]
5.小结
5.1在当前脚本需要传递参数,就使用fixture
5.2偶发性的需要使用其他脚本的参数,如果已经设置fixture,引入该文件,用fixture方法进行参数传递,如果没有设置fixture,可以使用普通方法进行传输,结合parametrize标签
5.3大批量的脚本需要使用相同的测试数据,数据放入conftest.py文件中 ,所有当前脚本的项目都可以直接使用,不需要引入文件
5.4当前脚本需要传递某些公共的参数,不想定义fixture,使用全局变量就可以 ,放在脚本最开始进行定义
- # 导入相关需要用到的类库
- import pytest
- from selenium import webdriver
-
- # 创建业务流程类,独立+无序,新建小组+新建用户
- # @pytest.mark.smoke1
- class Test_add_group_user():
- # 前置类方法,在整个流程中只走一遍
- def setup_class(self):
- # 定义url
- self.url="http://testplt.share.atstudy.com/admin/login/?next=/admin/"
- # 打开谷歌浏览器
- self.driver=webdriver.Chrome()
- # 发送请求
- self.driver.get(self.url)
- # 输入用户名
- self.driver.find_element_by_name("username").send_keys('atstudy')
- # 输入密码
- self.driver.find_element_by_name("password").send_keys('51testing')
- # 点击登录按钮
- self.driver.find_element_by_xpath('//*[@id="login-form"]/div[3]/input').click()
- # 测试添加组
- # 执行顺序设置为2
- @pytest.mark.run(order=2)
- def test_add_group(self):
- # 打开添加用户组页面,这里的url只是添加用户方法自己使用,所以不需要加self
- url="http://testplt.share.atstudy.com/admin/auth/group/add/"
- # 发送请求
- self.driver.get(url)
- # 输入用户组名称
- self.driver.find_element_by_id('id_name').send_keys("group1")
- # 选择全部权限
- self.driver.find_element_by_id('id_permissions_add_all_link').click()
- # 点击确定按钮
- self.driver.find_element_by_xpath('//*[@id="group_form"]/div/div/input[1]').click()
-
- # 测试添加用户
- # 执行顺序设置为1
- @pytest.mark.run(order=1)
- def test_add_user(self):
- # 打开添加用户页面
- url='http://testplt.share.atstudy.com/admin/auth/user/add/'
- # 发送页面请求
- self.driver.get(url)
- # 输入用户名
- self.driver.find_element_by_id('id_username').send_keys("user")
- # 输入密码
- self.driver.find_element_by_id('id_password1').send_keys("123456Pwd")
- # 再一次输入密码
- self.driver.find_element_by_id('id_password2').send_keys("123456Pwd")
- # 点击确定按钮
- self.driver.find_element_by_xpath('//*[@id="user_form"]/div/div/input[1]').click()
- # 执行后置类方法,整个流程中只运行一次
- def teardown_class(self):
- self.driver.close()
五、删除添加的用户-联调参数-先用面向对象先调通先
- from selenium import webdriver
- import time
- # 面向对象,先把函数先跑通先,再封装在pytest里面,因为用pytest驱动去跑封装后的代码时,如果报错,控制台的日志不好定位
-
-
-
- # 定义类
- class Test_delete_user():
- # 类前置方法(一个流程里面只走一次)
- # 登录
- def test_login(self):
- # 定义url
- self.url='http://testplt.share.atstudy.com/admin/login/?next=/admin/'
- # 打开谷歌浏览器
- self.driver=webdriver.Chrome()
- # 发送请求
- self.driver.get(self.url)
- # 输入用户名
- self.driver.find_element_by_name('username').send_keys('atstudy')
- # 输入密码
- self.driver.find_element_by_name('password').send_keys('51testing')
- # 点击登录按钮
- self.driver.find_element_by_xpath('//*[@id="login-form"]/div[3]/input').click()
-
- # 添加用户
- def test_add_user(self):
- # 定义添加用户页面
- url='http://testplt.share.atstudy.com/admin/auth/user/add/'
- # 发送请求
- self.driver.get(url)
- # 定义usename
- username='user3'
- # 输入用户名
- self.driver.find_element_by_id('id_username').send_keys(username)
- # 输入密码
- self.driver.find_element_by_id('id_password1').send_keys('123456Pwd')
- # 再次确认密码
- self.driver.find_element_by_id('id_password2').send_keys('123456Pwd')
- # 点击确定按钮
- self.driver.find_element_by_xpath('//*[@id="user_form"]/div/div/input[1]').click()
- # 返回用户名到其他方法中
- return(username)
-
- # 删除用户
- def test_delete_user(self,username):
- url='http://testplt.share.atstudy.com/admin/auth/user/'
- self.driver.get(url)
- num=len(self.driver.find_elements_by_class_name('field-username'))
- print("统计下有多少个用户",num)
- for i in range(1,num+1):
- uname=self.driver.find_element_by_xpath('//*[@id="result_list"]/tbody/tr['+str(i)+']/th/a').text
- print(uname)
- if uname==username:
- time.sleep(3)
- # 点击用户名信息
- self.driver.find_element_by_xpath('//*[@id="result_list"]/tbody/tr['+str(i)+']/th/a').click()
- # 点击删除用户的按钮
- self.driver.find_element_by_xpath('//*[@id="user_form"]/div/div/p/a').click()
- # 获取到新页面上的句柄,然后切换到新窗口
- self.driver.switch_to.window(self.driver.window_handles[-1])
- # 然后点击确认删除按钮
- self.driver.find_element_by_xpath('//*[@id="content"]/form/div/input[2]').click()
- # 如果break跟if对齐的话,在遍历完第一个元素,就会跳出循环,后面的判断也都就执行不了
- break
-
- if __name__ == '__main__':
- # 实例化对象
- obj=Test_delete_user()
- # 调用方法
- obj.test_login()
- username=obj.test_add_user()
- obj.test_delete_user(username)
-
-
五、删除添加的用户-联调参数-python已封装
- # 删除刚添加的用户
- # 需要导入的类库
- from selenium import webdriver
- import pytest
- import time
- # 定义类
- class Test_delete_user():
- # 类前置方法(一个流程里面只走一次)
- def setup_class(self):
- # 定义url
- self.url='http://testplt.share.atstudy.com/admin/login/?next=/admin/'
- # 打开谷歌浏览器
- self.driver=webdriver.Chrome()
- # 发送请求
- self.driver.get(self.url)
- # 输入用户名
- self.driver.find_element_by_name('username').send_keys('atstudy')
- # 输入密码
- self.driver.find_element_by_name('password').send_keys('51testing')
- # 点击登录按钮
- self.driver.find_element_by_xpath('//*[@id="login-form"]/div[3]/input').click()
-
-
- # 添加用户
- # 添加传参标签
- @pytest.fixture()
- def test_add_user(self):
- # 定义添加用户页面
- url='http://testplt.share.atstudy.com/admin/auth/user/add/'
- # 发送请求
- self.driver.get(url)
- # 定义usename
- username='user3'
- # 输入用户名
- self.driver.find_element_by_id('id_username').send_keys(username)
- # 输入密码
- self.driver.find_element_by_id('id_password1').send_keys('123456Pwd')
- # 再次确认密码
- self.driver.find_element_by_id('id_password2').send_keys('123456Pwd')
- # 点击确定按钮
- self.driver.find_element_by_xpath('//*[@id="user_form"]/div/div/input[1]').click()
- # 返回用户名到其他方法中
- return(username)
-
-
- # 删除用户
- def test_delete_user(self,test_add_user):
- # 定义用户信息页
- url='http://testplt.share.atstudy.com/admin/auth/user/'
- # 发送请求
- self.driver.get(url)
- # 统计用户元素的个数
- num=len(self.driver.find_elements_by_class_name('field-username'))
- print("统计下有多少个用户",num)
- # 依次遍历这几个用户数,找到遍历的值等于添加的用户信息,跳出循环
- for i in range(1,num+1):
- uname=self.driver.find_element_by_xpath('//*[@id="result_list"]/tbody/tr['+str(i)+']/th/a').text
- print(uname)
- if uname==test_add_user:
- time.sleep(3)
- # 点击用户名信息
- self.driver.find_element_by_xpath('//*[@id="result_list"]/tbody/tr['+str(i)+']/th/a').click()
- # 点击删除用户的按钮
- self.driver.find_element_by_xpath('//*[@id="user_form"]/div/div/p/a').click()
- # 获取到新页面上的句柄,然后切换到新窗口
- self.driver.switch_to.window(self.driver.window_handles[-1])
- # 然后点击确认删除按钮
- self.driver.find_element_by_xpath('//*[@id="content"]/form/div/input[2]').click()
- # 如果break跟if对齐的话,在遍历完第一个元素,就会跳出循环,后面的判断也都就执行不了
- break
-
- if __name__ == '__main__':
- pytest.main(["-s","test_8.py","-v"])
-
- # 选择权限,添加用户组
- import pytest
- from selenium import webdriver
- from selenium.webdriver.support.select import Select
- import random
- num=6
-
- # 定义类
- class Test_selectautho_addgroup():
- # 类前置方法,流程中只运行一次
- def setup_class(self):
- # 定义登录url
- self.url="http://testplt.share.atstudy.com/admin/login/?next=/admin/"
- # 打开谷歌浏览器
- self.driver=webdriver.Chrome()
- # 发起登录请求
- self.driver.get(self.url)
- # 输入用户名
- self.driver.find_element_by_name("username").send_keys("atstudy")
- # 输入密码
- self.driver.find_element_by_name("password").send_keys("51testing")
- # 点击登录按钮
- self.driver.find_element_by_xpath('//*[@id="login-form"]/div[3]/input').click()
-
- # 选择权限
- @pytest.fixture()
- def test_select_autho(self):
- # 打开选择权限的页面,这个页面是这个方法自己的
- # 定义autho列表
- autholist=[]
- url='http://testplt.share.atstudy.com/admin/auth/permission/'
- # 发送请求,它只是一个发送请求,并没有接收请求结果
- self.driver.get(url)
- # 取多少个权限
- for j in range (0,num):
- # 生成随机数
- i=random.randint(1,15)
- print(i)
- # 取权限页面上的第几个权限值
- autho=self.driver.find_element_by_xpath('//*[@id="result_list"]/tbody/tr['+str(i)+']/th/a').text
- # 取出来之后,打印到autho列表里面
- autholist.append(autho)
- print(autholist)
- # 返回autholist,可以到其他方法中
- return autholist
-
- # 添加用户组
- def test_group(self,test_select_autho):
- # 定义url,只是本方法内部使用
- url = 'http://testplt.share.atstudy.com/admin/auth/group/add/'
- # 发送请求
- self.driver.get(url)
- # 输入组名
- self.driver.find_element_by_id('id_name').send_keys('group1')
- # 在autho列表里面去遍历autho
- for autho in test_select_autho:
- # 找到autho列表框
- select_autho=self.driver.find_element_by_id('id_permissions_from')
- # 在列表框里面去放入autho,然后转换成文本类型
- Select(select_autho).select_by_visible_text(autho)
- # 点击添加按钮
- self.driver.find_element_by_xpath('//*[@id="id_permissions_add_link"]').click()
-
-
- # 类后置方法,流程中只走一遍
- def teardown_class(self):
- self.driver.close()