美文网首页
python selenium培训模块测试源码带注释!

python selenium培训模块测试源码带注释!

作者: DD丿 | 来源:发表于2021-10-21 17:57 被阅读0次

    -- coding: utf-8 --

    对应lib套件下的cmtclogin文件中 声明的cmtc_login方法

    from time import sleep
    from selenium.webdriver.remote.command import Command

    from selenium.webdriver.common.by import By

    from selenium.webdriver.support import expected_conditions as EC
    from hytest import *
    from hytest import STEP, INFO
    from selenium.webdriver.support.wait import WebDriverWait
    from lib.cmtclg_djj01 import cmtc_login
    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.remote.webelement import WebElement

    初始化方法套件setup登录,无需self参数

    wd调用lib/cmtclogin中cmtc_login

    def suite_setup():
    INFO('初始化登录流程')
    cmtc_login()
    STEP(1, '登录成功')

    套件清除方法 如果底下class用例有多个且之间具有数据依赖性,需要在顶部加套件初始化

    防止获取不到数据元素

    def suite_teardown():
    wd = GSTORE['wd']
    wd.quit()

    class cmtc002():
    name = '培训管理'
    tags = ['培训']

    # 测试用例开始执行
    def teststeps(self):
        # 从lib cmtclogin中取出gstore方法中的wd变量
        wd = GSTORE['wd']
        INFO("跳转到" + wd.title +"平台")
        # 获取当前页面的源码并断言
        pagesource = wd.page_source
        print("数据类型为:")
        print(type(pagesource))
        # 好像获取到的源码就是gbk格式,不需要下面转成gbk格式,因此注释掉了下面那句
        # print(pagesource.encode("gbk",ignore))
        # 获取某些关键字在源码中的数量
        print("页面关键字数量为:%d"%+pagesource.count("DDauto"))
    
        try:
    
            assert u"CMTC" in pagesource, "页面源码中未找到'CMTC'关键字"
    
        except:
    
            print("页面源码中存在CMTC关键字", "\n")
    
        sleep(2)
        # 隐式等待
        wd.implicitly_wait(10)
        STEP(2, '培训管理列表查询')
        # 培训 div#app div:nth-child(12) > li'表示ul标签下第12个div下的li元素
        wd.find_element_by_css_selector(
            'div#app div:nth-child(10) > li').click()
        # 定义变量菜单menu获取内容 并用check_point检查菜单是否符合 注:一定要定位到文案标签头span 缩小定位范围很重要
        eles = wd.find_elements_by_css_selector(
            '#app > div > div.main-container.hasTagsView > div > div > div.scrollbar-wrapper.el-scrollbar__wrap > div > ul div > a > li > span ')
        wd.implicitly_wait(10)
        menuText = [ele.text for ele in eles]
        INFO(menuText)
        # 总共4个模块,所以是[:4]
        CHECK_POINT('左侧菜单检查',
                    menuText[:4] == ['培训计划', '培训申请一览', '借阅记录', '数据统计'],
                    True)
        wd.implicitly_wait(10)
    
        STEP(3, '培训申请一览')
        wd.find_element_by_css_selector(
            'div#app div:nth-child(2) > a > li > span').click()
        INFO("菜单跳转正常")
        wd.implicitly_wait(10)
        # 新增计划外培训
        STEP(4, '新增培训申请')
        wd.find_element_by_css_selector(
            'div#app div.mb8.el-row > div:nth-child(1) > button[type="button"]').click()
        wd.implicitly_wait(10)
        # 获取当前页面url并断言 url=cpurl则跳转成功,不符合则提示网址非预期,结束进程
        currentPageUrl = wd.current_url
        print("当前页面的url是:", currentPageUrl+"跳转成功")
        assert currentPageUrl == "http://xxxxxxxxxxx", "当前网页网址非预期!"
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(2) > div:nth-child(1) > div > div > div.el-input.el-input--mini > input').send_keys(
            "DDauto培训名称1")
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(2) > div:nth-child(2) > div > div > div > input').send_keys(
            "DDauto主办机构1")
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(3) > div:nth-child(1) > div > div > div > textarea').send_keys(
            "DDauto主要内容1")
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(3) > div:nth-child(2) > div > div > div > textarea').send_keys(
            "DDauto申请说明1")
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(4) > div:nth-child(1) > div > div > div > input').send_keys(
            "DDauto培训项目1")
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(4) > div:nth-child(2) > div > div > div > input').send_keys(
            "2021.10.18")
        wd.find_element_by_css_selector(
            'form#train-info div.el-input.el-input--small.el-input-group.el-input-group--append > input').send_keys(
            "24")
        # 培训要求
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(5) > div:nth-child(1) > div > div > div > div > input').click()
        sleep(1)
        wd.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div[1]/ul/li[2]/span').click()
        pxyq = wd.find_element_by_xpath('/html/body/div[2]/div[1]/div[1]/ul/li[2]/span').text
        print
        pxyq
    
        # 培训类别
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(5) > div:nth-child(2) > div > div > div > div > input').click()
        wd.implicitly_wait(10)
        wd.find_element_by_xpath(
            '/html/body/div[3]/div[1]/div[1]/ul/li[5]/span').click()
        # 培训方式
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(5) > div:nth-child(3) > div > div > div > div.el-input.el-input--mini.el-input--suffix > input').click()
        sleep(1)
        wd.find_element_by_xpath('/html/body/div[4]/div[1]/div[1]/ul/li[5]/span').click()
        # 考核形式
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(6) > div:nth-child(1) > div > div > div > div > input').click()
        sleep(1)
        wd.find_elements_by_css_selector('div:nth-child(11) > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li.el-select-dropdown__item.hover > span')
        sleep(1)
        # 组织部门
        zzbm=wd.find_element_by_css_selector(
            'form#train-info div:nth-child(6) > div:nth-child(2) > div > div > div > div > input').click()
        sleep(1)
        zzbm.find_element_by_xpath(
            '/html/body/div[6]/div[1]/div[1]/ul')
        zzbm.find_element_by_css_selector(
            'div:nth-child(11) > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li.el-select-dropdown__item.hover').click()
        # 项目级别
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(6) > div:nth-child(3) > div > div > div > div > input').click()
        wd.implicitly_wait(10)
        wd.find_element_by_xpath(
            '/html/body/div[3]/div[1]/div[1]/ul/li[3]/span').click()
        # 经费预算
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(7) > div.el-col.el-col-8 > div > div > div > input').send_keys('1000')
        # 签培训协议 是
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(9) > div.el-col.el-col-8 > div > div > div > div.el-input.el-input--mini.el-input--suffix > input').click()
        wd.implicitly_wait(10)
        wd.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div[1]/ul/li[1]/span').click()
        # 培训地点
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(7) > div.el-col.el-col-8 > div > div > div > input').send_keys("DD培训地点1")
        # 培训日期
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(10) > div > div > div > div').click()
    
        wd.find_element_by_css_selector(
            'td.available.today > div > span').click()
        # 培训对象
        wd.find_element_by_css_selector(
            'form#train-info div:nth-child(11) > div:nth-child(1) > div > div > div.el-textarea.el-input--mini > textarea').send_keys(
            "DDauto培训对象1")
        # 添加参加人员
        wd.find_element_by_css_selector(
            'form#train-info div.el-row > button[type="button"] > span').click()
        wd.implicitly_wait(10)
        wd.find_element_by_css_selector(
            'form#train-info div.el-input.el-input--medium.el-input--suffix.is-focus > span > span > i').click()
        wd.find_element_by_css_selector(
            'ul > li.el-select-dropdown__item.hover').click()
    
    image.png

    相关文章

      网友评论

          本文标题:python selenium培训模块测试源码带注释!

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