web自动化全套(三,test***_***.py用例文件)

单元测试用例

因为项目已经是一个将近成熟的项目,所以我就将web端每一个模块分支写成了测试用例

话不多说,上代码!

from selenium import webdriver
#webdriver,不多说了哈
import unittest
#unittest测试框架,也不多说了
import time
import os,sys
from selenium.webdriver.common.action_chains import ActionChains
#如果同学你是从上一篇看过来的,应该都清楚以上的模块作用,我就不重复写了哈

path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(path)
#这里设置环境变量

from public.opens import open_web
#这行代码就是要写环境变量的原因了,因为有些模块是我们自己写的(放在我们的项目文件根目录中),这里我们想要调用自己写的方法的话就需要设置环境变量,来让这个py文件执行的时候找得到我们所写的方法。public是目录名,opens是文件名,open_web是类名,加上环境变量的话我们就可以导入项目文件中的方法了


#****模块
class Co_001(unittest.TestCase):
#定义测试类,固定的格式,类名Co_001可以更改
    '''全局的一个前提,打开***首页'''
    @classmethod
    def setUpClass(self):
        self.mm = open_web()
        #将open_web()方法封装为mm方法,这样mm比较短,而我比较懒哈哈,之后在这个文件中需要调用open_web()方法的地方直接调用mm()就可以了
        self.driver = webdriver.common()
        self.mm.home(self.driver)
        #调用mm方法中的home方法
        self.driver.maximize_window()
        #将我们的脚本控制的浏览器全屏
	#我在文章下方把unittest框架给大家做个介绍,这里我就只介绍我的方法了


    def setUp(self):
        print("-------------------------------------------------------")
        
    #登录
    def test_co_001_001(self):
        '''断言登录是否成功'''
        print('登陆首页页面跳转测试')
        #获取当前网页的url进行断言
        url01 = self.driver.current_url
        time.sleep(1)
        if url01 == "http://testco.qikevip.com/#/public/page":
            print("企业后台登录成功")
        else:
            print("企业后台登录失败")
        
        

    #首页跳转管理权限页
    def test_co_001_002(self):
        '''首页跳转管理权限页'''
        print('登陆首页页面跳转测试')
        time.sleep(1)                      
        self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[2]/div/ul/li[1]/img').click()
        url002 = self.driver.current_url
        time.sleep(1)
        if url002 == "http://testco.qikevip.com/#/public/permissions-list":
            print("跳转权限管理页面成功")
        else:
            print("跳转权限管理页面失败")
        
    
    #首页跳转课库管理
    def test_co_001_003(self):
        '''首页跳转课库管理'''
        print('登陆首页页面跳转测试')
        time.sleep(1)
        self.driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div/div[2]/div/ul/li[2]").click()
        url003 = self.driver.current_url
        time.sleep(1)
        if url003 == "http://testco.qikevip.com/#/public/curr-quest":
            print("跳转企业课库成功")
        else:
            print("跳转企业刻苦失败")
        
        
    #首页跳转企业风采(无此功能权限)(获取不到弹窗)
    """ def test_co_004(self):
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[2]/div/ul/li[3]').click()
        
        time.sleep(1) 
    """
        
    #首页跳转企业机构
    def test_co_001_005(self):
        '''首页跳转企业机构'''
        print('登陆首页页面跳转测试')
        time.sleep(1)
        #元素聚焦,页面跳到元素的位置
        targer = self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[2]/div/ul/li[4]')  
        self.driver.execute_script("arguments[0].scrollIntoView();",targer)

        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[2]/div/ul/li[4]').click()
        url005 = self.driver.current_url
        if url005 == "http://testco.qikevip.com/#/public/organ-enter":
            print("跳转企业机构成功")
        else:
            print("跳转企业机构失败")

    #首页跳转企业视频
    def test_co_001_006(self):
        '''首页跳转企业视频'''
        print('登陆首页页面跳转测试')
        time.sleep(1)
        #元素聚焦,页面跳到元素所在位置
        targer = self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[2]/div/ul/li[5]/img')
        self.driver.execute_script("arguments[0].scrollIntoView();",targer)
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[2]/div/ul/li[5]/img').click()
        url006 = self.driver.current_url
        if url006 == "http://testco.qikevip.com/#/public/video-enterprise":
            print("跳转企业视频页面成功")
        else:
            print("跳转企业视频页面失败")

    #首页跳转任务管理
    def test_co_001_007(self):
        '''首页跳转任务管理'''
        print('登陆首页页面跳转测试')
        time.sleep(1)
        #元素聚焦,页面跳到元素所在位置
        targer = self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[2]/div/ul/li[6]/img')
        self.driver.execute_script("arguments[0].scrollIntoView();",targer)
        self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[2]/div/ul/li[6]/img').click()
        url007 = self.driver.current_url
        if url007 == "http://testco.qikevip.com/#/public/task-list":
            print("首页跳转任务管理页面成功")
        else:
            print("首页跳转任务管理页面失败")

    #清除缓存
    def test_co_001_008(self):
        '''清除缓存功能'''
        print('登陆首页页面跳转测试')
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[1]/div/div/div[3]/button/span').click()
        time.sleep(1)
        #获取弹窗信息
        div_text = self.driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[1]/div[2]/p').text
        print('提示语:{}'.format(div_text))
        div_text1 = self.driver.find_element_by_xpath('/html/body/div[2]/div/div[3]/button[1]/span').text
        div_text2 = self.driver.find_element_by_xpath('/html/body/div[2]/div/div[3]/button[2]/span').text
        print('按钮:{}{}'.format(div_text1,div_text2)+"\n点击取消")
        #点击取消弹窗信息
        self.driver.find_element_by_xpath('/html/body/div[2]/div/div[3]/button[1]/span').click()
        time.sleep(2)
        huancun = self.driver.find_element_by_xpath('/html/body/div[3]').text
        print(huancun)
        
        
    #退出登录
    def test_co_001_009(self):
        '''首页退出按钮功能测试'''
        time.sleep(1)
        #self.driver.find_element_by_xpath("test_co_qyxx_001").click()
        self.driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div/div/div[2]/button").click()
        time.sleep(1)
        #获取弹窗信息
        tc = self.driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[1]/div/p").text
        div_text1 = self.driver.find_element_by_xpath("/html/body/div[2]/div/div[3]/button[1]/span").text
        div_text2 = self.driver.find_element_by_xpath("/html/body/div[2]/div/div[3]/button[2]/span").text
        print('提示内容:{}'.format(tc))
        print('按钮:{},{}'.format(div_text1,div_text2)+"\n点击{}".format(div_text1))
        self.driver.find_element_by_xpath("/html/body/div[2]/div/div[3]/button[1]/span").click()
        time.sleep(1)
        
        url02 = self.driver.current_url  
        time.sleep(1)
        
        if url02 == "http://testco.qikevip.com/#/public/page":
            print("企业后台取消退出成功")
        else:
            print("企业后台取消退出失败")
        time.sleep(1)
        



    #添加管理员
    """ def test_co_qyxx_003_001(self):
        self.driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div/div[1]/div[2]/div[1]/button/span").click()
        url003_001 = self.driver.current_url
        if url003_001 == "http://testco.qikevip.com/#/public/add-permissionst":
            print("跳转添加管理员页面成功")
        else:
            print("跳转添加管理员页面失败") 
    """
        


    def tearDown(self):
        #返回首页
        self.mm.returns(self.driver)
        

    @classmethod
    def tearDownClass(self):
        
        self.mm = open_web()
        self.mm.close(self.driver)
        
if __name__ == "__main__":
    """ suite = unittest.TestSuite()
    
    loader = unittest.TestLoader()
    suite = loader.loadTestsFromModule("text_qyxx.Co_001.test_co_008")
    runner = unittest.TestResult()
    suite.run(runner)

    print(runner.__dict__) """
    unittest.main()

脚本整体简介
1,导包
2,定义测试类class 类名(unittest.TestCase):
2.1,定义全局的环境(在所有用例执行前执行这个方法,每个测试用例都再这个环境下运行,我的习惯是一般登录首页这样的会放在这个方法中)
@classmethod
def setUpClass(self):
2.2,定义每个测试用例的环境(每条用例执行前会执行这个方法,添加每条测试用例的环境)
def setUp(self):
2.3,回收每条测试用例执行后的资源,有始有终(每条测试用例执行后会执行这个方法,回收每条测试用例的资源)
def tearDown(self):
2.4,回收全局的环境(在所有的测试用例执行完之后回收资源,做一个漂亮的收尾)
@classmethod
def tearDownClass(self):
3,定义main函数运行运行这些测试用例进行调试,我这里时成熟的代码,没有调试的代码,小伙伴们可以用unittest框架中的TestSuite,TestRunner方法去调试

这里提醒一下大家,unittest框架是根据英文字母的排列顺序,数字的排列顺序去执行测试用例的

第一篇:web自动化测试全套(一:架构介绍,邮件模块包代码)
第二篇:web自动化测试全套(二:公共模块包)
第四篇:web自动化测试全套(四:单元测试用例集成测试,生成测试报告并发送指定邮箱)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值