• 综合练习:web端Python自动化测试


    一、项目:针对百度首页搜索功能的测试:python+selenium+unittest

    1、page object 封装页面元素。

    from poium import Page, NewPageElement
    
    class BaiduPage(Page):
        """封装百度Page层"""
        search_input = NewPageElement(id_="kw",describe="搜索框")
        search_button = NewPageElement(id_="su",describe="搜索按钮")
        setting = NewPageElement(id_="s-usersetting-top",describe="设置按钮")
        search_setting = NewPageElement(link_text="搜索设置")
        high_level_setting = NewPageElement(link_text="高级设置")
        save_setting = NewPageElement(link_text="保存设置")
        language_setting = NewPageElement(id_ = "SL_1",describe="简体中文")

    2、封装谷歌浏览器启动操作

    import unittest
    from selenium import webdriver
    
    class Launch_Brower(unittest.TestCase):
        """开启/关闭 谷歌浏览器"""
    
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome()
            cls.baidu_url = "https://www.baidu.com/"
    
    
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
    if __name__=="__main__":
        unittest.main()

    3、test case:用parameterized.expand()或ddt.file_data实现参数化。

    import unittest,csv,codecs, os, sys
    base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.insert(0, base_path)
    from common.launch_brower import Launch_Brower
    from page.baidu_page import BaiduPage
    from time import sleep
    from parameterized import parameterized
    from itertools import islice
    from ddt import ddt,file_data,unpack
    
    
    @ddt
    class TestSearchKeyword(Launch_Brower):
    
        def search_keyword(self,keyword):
            page = BaiduPage(self.driver)
            page.get(self.baidu_url)
            page.search_input.send_keys(keyword)
            page.search_button.click()
            sleep(2)
    
        def get_data(file_path):
            
            """读取参数化文件baidu_data.csv"""
            data = []
            with codecs.open(file_path , "r") as f:
                temp = csv.reader(f)
                for i in islice(temp,1,None):
                    data.append(tuple(i))    
            return data
    
       
        @parameterized.expand(
    
            get_data(base_path +
            "/test_case/baidu_data/data_file.csv"))
                              
        def test_01_search_keyword(self,name,keyword):
            
            self.search_keyword(keyword)
            title = self.driver.title
            self.assertEqual(title, keyword + "_百度搜索")
          
       
        
        @file_data("ddt.json")
        @unpack
        def test_02_search_keyword(self,keyword):
            self.search_keyword(keyword)
            title = self.driver.title
            self.assertEqual(title, keyword + "_百度搜索")
        
                   
    if __name__=="__main__":
        unittest.main(verbosity=2)

    4、调用HTMLTestRunner运行测试用例生成报告。

    import time,unittest
    from HTMLTestRunner import HTMLTestRunner
    import time,sys
    sys.path.append("./test_case")
    
    
    if __name__=="__main__":
    
        test_dir = './test_case'
        suit = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')
        now_time = time.strftime("%Y-%m-%d %H_%M_%S")
        filename = './report/'+now_time+'_result.html'
        with open(filename, "wb") as fp:
            runner = HTMLTestRunner(stream=fp,
                                title='百度',
                                description="win 10",
                                 verbosity=2)
                                   
            runner.run(suit,rerun=0,save_last_run=False)
            fp.close()
                                

     

  • 相关阅读:
    哈希表(hash)
    并查集
    trie树(字典树)
    单调队列(滑动窗口)
    单调栈
    用数组实现栈与队列
    数组实现双链表
    数组实现单链表
    区间合并
    离散化
  • 原文地址:https://www.cnblogs.com/yijierui/p/13886769.html
Copyright © 2020-2023  润新知