unittest框架实战(七)

来源:互联网 发布:毕向东java基础25天 编辑:程序博客网 时间:2024/06/06 02:08
#coding=utf-8
'''

这个文件下面存在的是页面上的元素,

每一个输入框,下拉框......都在这里封装好,在case中只传入数据,不去考虑元素定位

上一篇的是公共模块,这里封装的是功能中的单独页面

'''

import sys, time
sys.path.append('..')
from selenium.webdriver.common.by import By
from Base.BasePage import My_page

class Outline(My_page):
    '''|选择"用例大纲"里面的模块'''
    def __init__(self, driver):
        super(Outline, self).__init__(driver)
        pass

    def Select_CaseMenu(self, Outline_text):
        '''直接传入模块名称'''
        CaseMenu_loc = (By.XPATH, "//*[text()='" + Outline_text + "']")
        self.Click(*CaseMenu_loc)

class Button(My_page):
    '''包括三个按钮,
    新建用例 按钮
    导入用例 按钮
    导出用例 按钮
    '''

    def __init__(self, driver, text):
        super(Button, self).__init__(driver)
        self.text = text
        self.allmenu = {
                u'新建用例': (By.XPATH, 'html/body/div[1]/div[1]/div[1]/a[1]/div'),
                u'导入用例': (By.XPATH, 'html/body/div[1]/div[1]/div[1]/a[2]/div'),
                u'导出用例': (By.XPATH, 'html/body/div[1]/div[1]/div[1]/a[3]/div')
        }
        
    def ClickButton(self):
        self.Click(*self.allmenu[self.text])

    def _NewCase(self, *loc):
        '''
        新建用例按钮
        '''
        self.Click(*loc)

    def _ImportCase(self, *loc):
        '''
        导入用例按钮
        '''
        self.Click(*loc)

    def _FromCase(self, *loc):
        '''
        导出用例按钮
        '''
        self.Click(*loc)

class NewCase_Page(My_page):
    '''
    新建用例页面
    '''
    def __init__(self, driver):
        super(NewCase_Page, self).__init__(driver)
        self.location = (By.XPATH, "//*[@id='select2-drop']/ul/li/div")
        self.input_loc = (By.XPATH, "//*[@id='select2-drop']/div/input")
        self.title_loc = (By.ID, 'casename')
        self.object_loc = (By.XPATH, "//*[@id='s2id_project']/a")
        self.type_loc = (By.XPATH, "//*[@id='s2id_casetype']/a")
        self.debug_loc = (By.XPATH, "//*[@id='s2id_integrationtag']/a")
        self.status_loc = (By.XPATH, "//*[@id='s2id_atstatus']/a")
        self.level_loc = (By.XPATH, "//*[@id='s2id_priority']/a")
        self.attribute_loc = (By.XPATH, "//*[@id='s2id_streamattr']/a")
        self.user_loc = (By.XPATH, "//*[@id='s2id_caseDef.assignTo']/a")
        self.function_loc_1 = (By.ID, "busiName")
        self.step_loc = (By.ID, 'newstep')
        self.add_loc = (By.XPATH, 'html/body/div[2]/div/div/div[6]/button[1]')
        self.sublimt_loc = (By.ID, 'btnSubmit')

    def Test_page_error(self, title, name, type_text, YOrN, status, \
        level, attribute, user):
        '''输入的功能模块为空,即不选择功能模块,新建测试用例'''
        self._Page_title(title)
        self._Object_name(name)
        self._Case_type(type_text)
        self._System_debug(YOrN)
        self._Automation_status(status)
        self._Priority_level(level)
        self._Case_attribute(attribute)
        self._Assign_user(user)

    def Test_page(self, title, name, type_text, menu_text, YOrN, status, \
        level, attribute, user):
        '''
        title -->标题名称,name  -->项目名称,type_text -->用例类型,menu_text -->功能模块
        YOrN -->是否需要省分联调,status -- >自动化状态,level -->优先级,attribut -->流属性
        user -->指派给,pre_text-->前置条件, test_data-->测试数据, test_step-->测试步骤,
        test_check-->检查点,test_result-->预期结果
        _Page_title --> 标题, _Object_name-->项目名称,_Case_type-->用例类型, _Function_module-->功能模块
        _System_debug-->是否需要省分联调, _Automation_status-->自动化测试状态, _Priority_level-->优先级
        _Case_attribute-->流属性, _Assign_user-->指派给,_Step_describle-->测试步骤,_sublimt-->提交按钮
        '''
        self._Page_title(title)
        self._Object_name(name)
        self._Case_type(type_text)
        self._Function_module(menu_text)
        self._System_debug(YOrN)
        self._Automation_status(status)
        self._Priority_level(level)
        self._Case_attribute(attribute)
        self._Assign_user(user)
        # self._Step_describle(pre_text, test_data, test_step, test_check, test_result)
        # # self._exit_iframe()
        # # self._exit_iframe()
        # self._sublimt()

    def ClickSublimt(self):
        '''点击提交'''
        self._clicksublimt(*self.sublimt_loc)

    def _Page_title(self, title):
        '''
        页面输入框
        标题
        '''
        self.Input(title, *self.title_loc)

    def _Object_name(self, name):
        '''
        项目名称
        '''
        self.Click(*self.object_loc)
        self.Input(name, *self.input_loc)
        self.Click(*self.location)

    def _Case_type(self, type_text):
        '''
        用例类型
        '''
        self.Click(*self.type_loc)
        self.Input(type_text, *self.input_loc)
        self.Click(*self.location)

    def _System_debug(self, YOrN):
        '''
        是否需要联调测试
        '''
        self.Click(*self.debug_loc)
        self.Input(YOrN, *self.input_loc)
        self.Click(*self.location)

    def _Automation_status(self, status):
        '''
        自动化状态
        '''
        self.Click(*self.status_loc)
        self.Input(status, *self.input_loc)
        self.Click(*self.location)

    def _Priority_level(self, level):
        '''
        优先级别
        '''
        self.Click(*self.level_loc)
        self.Input(level, *self.input_loc)
        self.Click(*self.location)

    def _Case_attribute(self, attribute):
        '''
        用例属性, 异常的还是正常的
        '''
        self.Click(*self.attribute_loc)
        self.Input(attribute, *self.input_loc)
        self.Click(*self.location)

    def _Assign_user(self, user):
        '''
        指派给
        '''
        self.Click(*self.user_loc)
        self.Input(user, *self.input_loc)
        self.Click(*self.location)

    def _Function_module(self, menu_text):
        '''
        功能模块
        '''
        iframe_location_1 = (By.ID, "mainFrame")
        self.Click(*self.function_loc_1)
        self._exit_iframe()
        time.sleep(1)
        self._select_function_module(menu_text)
        self._select_iframe(*iframe_location_1)

    def _select_function_module(self, menu_text):
        '''进入iframe
        功能选择模块
        即点击功能选择模块 弹出来的页面
        '''
        location = (By.ID, "jbox-iframe")
        input_loc = (By.ID, "key")
        click_select = (By.ID, 'btn')
        click_loc_1 = (By.XPATH, "//span[text()='" + menu_text + "']")
        # click_loc_2 = (By.XPATH, "//*[@id='jbox-state-state0']/div[2]/button[1]")
        click_loc_2 = (By.XPATH, "//*[@id='jbox-state-state0']/div[2]/button[1]")
        # click_loc_2 = (By.XPATH, "//button[text()=u'确定']")
        self.Iframe(*location)
        self.Input(menu_text, *input_loc)
        self.Click(*click_select)
        self.Click(*click_loc_1)
        self._exit_iframe()
        self.Click(*click_loc_2)

    def _select_iframe(self, *loc):
        ''''
        进入iframe
        '''
        self.Iframe(*loc)

    def _exit_iframe(self):
        '''
        退出iframe
        '''
        self.DisIframe()

    def _clicksublimt(self, *loc):
        '''点击提交按钮'''
        self.Click(*loc)