selenium + python 完整自动化测试脚本及讲解(四、unitTestCase包介绍)
编写不易,求关注!!!
简介
文件名 | 介绍 |
---|---|
ACase_0_test_login | 继承unittest.TestCase的类 |
备注: 其实都是继承unittest.TestCase的类。详情参见:后续添加
第一种编写方式
第一种编写方式就是,每一个用例中值写测哪里,不写怎么测。
先看一下中编写方式 ↓ ↓ ↓ ↓
# -*- coding: utf-8 -*-
import unittest
from object.DRIVER import getDriver
from Action.Action_Test_Login import test_login
from assert_option.assert_test_login import *
class Test_Login(unittest.TestCase):
# 用例运行前执行的函数
def setUp(self):
self.driver = getDriver()
# 测试用例
def test_1_base_test_login(self):
# 方法的第一个参数 self 代表类本身,相当于java 中的this
driver = self.driver
# 调用了 Action.Action_Test_Login 的 test_login 函数
test_login(driver)
# 调用了 assert_option.assert_test_login 的 assert_login 函数
assert_login(driver, self.assertNotEqual)
# 用例运行后执行的函数
def tearDown(self):
# 在 组测试用例运行时 tearDown 不要使用 self.quit() 关闭浏览器
pass
if __name__ == "__main__":
unittest.main()
在看 test_login,与 assert_login 函数
test_login 函数内容
# -*- coding: utf-8 -*-
from element_control.element_Test_Login import *
from selenium import webdriver
def test_login(driver: webdriver, username: str, password: str):
"""
使用 username,password登录到后台服务器
:param driver: webdriver对象
:param username: 用户登录名
:param password: 用户登录密码
:return: 无 返回值
"""
driver.get("http://*********.com/***/***")
send_keys(driver, By.NAME, "phone", username)
send_keys(driver, By.NAME, "pwd", password)
click(driver, By.LINK_TEXT, u"登录")
# driver.find_element_by_link_text(u"登录").click()
PS: send_keys和 click 是另外封装的函数,等写全了 一起给你们!
assert_login 函数内容
# -*- coding: utf-8 -*-
from selenium.webdriver.remote.webdriver import WebDriver
def assert_login(driver: WebDriver) -> bool:
"""
判断是否登录成功,
:param driver:WebDriver
:return: True = 成功 ; False = 失败
"""
# TODO 断言内容
return len(driver.get_cookies()) != 0
第二种编写方式
第一种编写方式就是,一个用例中什么都写,看代码 ↓ ↓ ↓ ↓
# -*- coding: utf-8 -*-
import time
import unittest
from object.DRIVER import getDriver
from utils.DEPENDON_ import dependon
from utils.UPLOAD_FILE_TOOLS import UpLoad_File_win32api
class publish_article(unittest.TestCase):
def setUp(self):
self.driver = getDriver()
# 依赖 另一个 CASE 参考网址 : https://blog.csdn.net/weixin_43664254/article/details/90512419
@dependon("test_1_base_test_login")
def test_1_publish_article(self):
driver = self.driver
isins = True
time.sleep(2)
driver.get("http://zw.******.com/****/main/?siteInfo=1")
driver.find_element_by_link_text(u"文章管理").click()
driver.find_element_by_xpath(u"(//a[contains(text(),'文章管理')])[2]").click()
driver.find_element_by_xpath(u"(//a[contains(text(),'文章管理')])[3]").click()
time.sleep(2)
driver.find_element_by_xpath("(//button[@type='button'])[4]").click()
time.sleep(2)
driver.find_element_by_xpath("(//input[@name='title'])[2]").click()
driver.find_element_by_xpath("(//input[@name='title'])[2]").clear()
driver.find_element_by_xpath("(//input[@name='title'])[2]").send_keys("32131312")
# 第一个上传图片
f1 = driver.find_element_by_xpath(
u"(.//*[normalize-space(text()) and normalize-space(.)='文章标题图:'])[1]/following::i[1]")
i1 = driver.find_element_by_xpath(u"//input[@name='articleTitlePic']")
UpLoad_File_win32api(f1, "D:\\timg (1).jpg", i1)
# 第二个上传图片
f2 = driver.find_element_by_xpath(
u"(.//*[normalize-space(text()) and normalize-space(.)='文章来源图:'])[1]/following::i[1]")
i2 = driver.find_element_by_xpath(u"//input[@name='originImg']")
UpLoad_File_win32api(f2, "D:\\timg (1).jpg", i2)
driver.find_element_by_xpath("(//button[@type='button'])[8]").click()
driver.find_element_by_link_text(u"政策法规").click()
driver.find_element_by_xpath("(//button[@type='button'])[9]").click()
driver.find_element_by_link_text(u"法律条文").click()
driver.find_element_by_id("j_custom_title").click()
driver.find_element_by_id("j_custom_title").clear()
driver.find_element_by_id("j_custom_title").send_keys(u"开心一下")
driver.find_element_by_id("j_custom_source").click()
driver.find_element_by_id("j_custom_source").clear()
driver.find_element_by_id("j_custom_source").send_keys(u"转载")
driver.find_element_by_xpath("(//input[@id='j_custom_title'])[2]").click()
driver.find_element_by_xpath("(//input[@id='j_custom_title'])[2]").clear()
driver.find_element_by_xpath("(//input[@id='j_custom_title'])[2]").send_keys(u"a,b")
driver.find_element_by_xpath("//td/span/a/i").click()
driver.find_element_by_xpath("//dd[3]").click()
driver.find_element_by_name("artSummary").click()
driver.find_element_by_name("artSummary").clear()
driver.find_element_by_name("artSummary").send_keys(u"测试测试")
driver.find_element_by_xpath(
u"(.//*[normalize-space(text()) and normalize-space(.)='所属地区:'])[1]/following::span[1]").click()
driver.find_element_by_link_text(u"北京").click()
# 文章是否推荐
if isins:
# 推荐
driver.find_element_by_xpath("//div[1]/ins").click()
driver.find_element_by_id("j_custom_author").click()
driver.find_element_by_id("j_custom_author").clear()
driver.find_element_by_id("j_custom_author").send_keys(u"1")
driver.find_element_by_xpath("//tr[@id='ids2']/td/div/button/span").click()
driver.find_element_by_link_text(u"排行榜").click()
else:
# 不推荐
driver.find_element_by_xpath("//div[2]/ins").click()
driver.switch_to.frame(0)
driver.find_element_by_xpath("//body").send_keys(u"121231273jkh")
driver.switch_to.parent_frame()
# driver.find_element_by_xpath("(//button[@type='submit'])[2]").click()
def tearDown(self):
pass
if __name__ == "__main__":
unittest.main()
PS:可以看到,一个case中写了所有的操作,定位,等等!
总结
对比 | 方法一 | 方法2 |
---|---|---|
代码编写难度 | 中等,需要多点编程基础 | 简单,录制完的脚本直接就能使用 |
维护性 | 高,组件都是拆分开的,哪里出错改哪里 | 简单的代码维护简单,越复杂维护起来越难 |
可用性 | 高,组件拆分,可随意调用 | 只能在当前CASE下面运行,其他位置不能调用 |
维护性 | 高,优化代码,不用牵一发动全身 | 优化一个细节,可能影响整个脚本 |
稳定性 | 高,代码经过封装后添加很对异常处理 | 添加异常时让代码更乱 |
PS:
其实还有很对,方法2 除了简单其他的一点用都没有
方法2: 使用使用起来虽然麻烦,但是后期维护的时候方便很多。
我在稍微举个例子:
需求
用户注册成功后,可使用用户名与密码登录还系统,
- 用户名不能包含特殊字符,可使用 “_” 下滑线
- 用户名长度在 8 到 16 个字符
- 用户名不能使用数字开头
- 密码不能包含特殊字符,可使用 “_” 下滑线
- 密码长度在 8 到 18 个字符
- 密码必须包含大写字符,小写字母,数字三种组合使用
用例
至少要 20 条以上的用例,等价类,边界值等等!
下面代码:11 行,要是使用方法1 要 20 * 11 = 220 行代码可以搞定。这样沉郁的代码太多!我还没写断言的代码!
def test_untitled_test_case(self):
driver = self.driver
username = "32131312"
password = "sdfsdfsdf"
driver.get("http://zw.*******.com/ZWCMS/main")
# 输入用户名
driver.find_element_by_xpath("(//input[@name='username'])[2]").click()
driver.find_element_by_xpath("(//input[@name='username'])[2]").clear()
driver.find_element_by_xpath("(//input[@name='username'])[2]").send_keys(username)
# 输入密码
driver.find_element_by_xpath("(//input[@name='password'])[2]").click()
driver.find_element_by_xpath("(//input[@name='password'])[2]").clear()
driver.find_element_by_xpath("(//input[@name='password'])[2]").send_keys(password)
# 点击登录按钮
driver.find_element_by_xpath("(//input[@name='submit'])").send_keys(password)
下面代码:9 行,没加一条用例 增加两条代码 9 + (20 * 2 )49 行。逻辑变了修改test_login这个一个地方就行了!
# 测试用例
def test_1_base_test_login(self):
test_login(self.driver, "superAdmin", "123456")
def test_2_base_test_login(self):
test_login(self.driver, "superAdmin", "123456")
def test_login(driver: webdriver, username: str, password: str):
driver.get("http://zw.*******.com/ZWCMS/main")
# 输入用户名
driver.find_element_by_xpath("(//input[@name='username'])[2]").click()
driver.find_element_by_xpath("(//input[@name='username'])[2]").clear()
driver.find_element_by_xpath("(//input[@name='username'])[2]").send_keys(username)
# 输入密码
driver.find_element_by_xpath("(//input[@name='password'])[2]").click()
driver.find_element_by_xpath("(//input[@name='password'])[2]").clear()
driver.find_element_by_xpath("(//input[@name='password'])[2]").send_keys(password)
# 点击登录按钮
driver.find_element_by_xpath("(//input[@name='submit'])").send_keys(password)
上面代码是伪代码,仅供参考不能直接运行。
编写不易,求关注!!!