美文网首页python3+selenium3自动化测试软件测试
python3.5+selenium3.4自动化测试10_sel

python3.5+selenium3.4自动化测试10_sel

作者: qingche46 | 来源:发表于2017-10-11 21:57 被阅读25次

    框架2.0(下)

    停了好长一段时间没有更新,最近比较忙,以后坚持至少一个月内输出1篇,继续上次需要掌握的技能,这次主要的函数调用和测试用例集的批量执行.

    8.函数调用其实在前面几篇也有讲到,主要用于登陆的用户名和密码组合测试,可以参考

    python3.5+selenium3.4自动化测试2_参数化
    python3.5+selenium3.4自动化测试4_随机组合的用户名和密码登陆
    下面在讲一个登陆和退出模块化,一般的系统都会有涉及到登陆和退出,当验证完登陆和退出的用例后,一般我们会有一组正常的用户名和密码进行登陆然后测试其他内容,这个时候登陆就可以独立到一个文件进行调用即可这边上一下登陆的代码和调用

    创建一个mod_login_on.py

    # _*_ coding: utf-8 _*_
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import Select
    from selenium.common.exceptions import NoSuchAttributeException
    import unittest,time,re
    from time import strftime, localtime
    from datetime import datetime
    #登陆函数
    def login(self):
        driver=self.driver
        try:
            driver.find_element_by_id("username").send_keys("admin")
            driver.find_element_by_id("secret").send_keys("password1")
            Select(driver.find_element_by_id('language')).select_by_value("en")
            driver.find_element_by_id("login_button").click()
            
        except:
            driver.get_screenshot_as_file(u"D:/python/selenium_use_case/error_png/2.ext/2.1_%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3])
    

    调用登陆模块

    # _*_ coding: utf-8 _*_
    __author__ = 'leo'
    __date__ = '2017/7/6 10:29'
    
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import Select
    from selenium.common.exceptions import NoSuchAttributeException
    import unittest,time,re
    from time import strftime, localtime
    from datetime import datetime
    
    import HTMLTestRunner
    #import mod_login_on函数
    from Pubilic_mod import mod_login_on
    
    class Delext(unittest.TestCase):
        def setUp(self):
            self.driver=webdriver.Chrome()
            self.driver.implicitly_wait(30)
            self.base_url="http://192.168.1.131"
            self.verificationErrors=[]
            self.accept_next_alert=True
    
        def test_Delext(self):
            u"""2.2.1 删除所有"""    
            driver=self.driver
            driver.get(self.base_url+"/")
    
    
            #调用login模块
            mod_login_on.login(self)
    
    
            try:
                time.sleep(3)
                # driver.switch_to_alert().accept()
                driver.implicitly_wait(3) #智能等待30
                # driver.find_element_by_id("current").click()
                driver.find_element_by_xpath(".//*[@id='nav']/ul/li[4]/a").click()
    
                #切换到frame中再找到对应的元素
                driver.switch_to.frame("mainscreen")
                driver.find_element_by_xpath(".//*[@id='btn_new_bulkexten']/span/span").click()
                driver.find_element_by_xpath(".//*[@id='btn_save_bulk']/span/span").click()
    
                driver.find_element_by_id("CHECKALL").click()
                driver.find_element_by_xpath(".//*[@id='btn_delete_selected']/span/span").click()
                driver.switch_to_alert().accept()
    
                #从frame中切回主文档
                driver.switch_to.default_content()
                driver.find_element_by_xpath(".//*[@id='applyChanges_Button']/font").click()
                time.sleep(10)#等待10
    
            except:
                driver.get_screenshot_as_file(u"D:/python/selenium/error_png/2.ext/2.2/%s.png" % datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3])
    
        def tearDown(self):
            time.sleep(3)
            self.driver.quit()
            self.assertEqual([],self.verificationErrors)
    if __name__=="__main__":
        suite=unittest.TestSuite()
        suite.addTest(Delext("test_Delext"))
    
        unittest.TextTestRunner().run(suite)
    

    9.测试用例集批量执行,先讲一下框架,这边先主要讲分为三个大模块error_png,report,test_case三个一级目录,主要存放错误图片,测试报告,测试用例集

    ├─error_png
    │  ├─1.login
    │  ├─2.ext
    ├─report
    └─test_case
        └─case
            ├─A_login
            │  └─case_a_logininterface.py
            │  └─case_b_reset.py
            ├─B_ext
            │  └─case_a_creat.py
            │  └─case_b_reset.py
            ├─Pubilic_mod
            │  └─mod_login_on.py
            ├─all_test.py
    

    通过上面的图片可以看出主要的用例集合放在case文件夹下,按大用例集分不同的文件夹,下面包含对应的.py测试用例,和批量执行文件all_test.py

    #coding=utf-8
    import sys
    from time import strftime, localtime
    from datetime import datetime
    
    from case.A_login import *
    from case.B_ext import *
    
    
    import unittest,doctest
    import HTMLTestRunner
    
    suite=unittest.TestSuite()
    #login
    suite.addTest(unittest.makeSuite(case_a_logininterface.LoginInterface))
    suite.addTest(unittest.makeSuite(case_b_reset.LoginReset))
    
    #ext
    suite.addTest(unittest.makeSuite(case_a_creatext.Createext))
    suite.addTest(unittest.makeSuite(case_b_delext.Delext))
    
    
    
    filename='D:\\python\\selenium\\report\\result20_%s.html'% datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3]
    fp=open(filename,'wb')
    runner=HTMLTestRunner.HTMLTestRunner(
        stream=fp,
        title='Report_title',
        description='Report_descrition')
    runner.run(suite)
    
    

    a.需要说明下,这个在创建A_login的时候需要选择新建python package,会自动带一个init.py文件,有什么作用可以自行百度下,这边主要添加对应的py文件,如下图:

    __init__.py

    在批量运行的文件中要import这些内容

    from case.A_login import *
    from case.B_ext import *
    

    b.这边批量运行集合,主要跟单个运行添加测试到suite容器一样

    if __name__=="__main__":
        suite=unittest.TestSuite()
        suite.addTest(Delext("test_Delext"))
    
        unittest.TextTestRunner().run(suite)
    

    在每个用例末尾使用unitest.TextTestRunner()来运行单个脚本中的多个case,这边的TextTestRunner在上篇文章讲过和HTMLTestRunner一样是继承了TestResult类,TextTestRunner这个主要以文本形式展示,HTMLTestRunner这个将统一展现在html上,这边通过统一的脚本添加所有的用例到suite容器中,run这些用例

    suite=unittest.TestSuite()
    #login
    suite.addTest(unittest.makeSuite(case_a_logininterface.LoginInterface))
    suite.addTest(unittest.makeSuite(case_b_reset.LoginReset))
    

    最后运行输出的报告


    测试报告

    通过这样的框架和需要理解的技巧,通过不断练习就会在2.0的的基础上不断巩固和对Python的深入了解,也基本上满足了一般公司的测试要求

    -----------------------------------------------------分割线-----------------------------------------------------

    用实际用例代码长期更新,介绍selenium使用中的知识小点,希望大家关注,给予鼓励赞赏

    关注微信公众号:无敌轻车

    相关文章

      网友评论

        本文标题:python3.5+selenium3.4自动化测试10_sel

        本文链接:https://www.haomeiwen.com/subject/tcibyxtx.html