def select_by_option(self, option): ''' 选中指定的文本项目 ''' self.bind() self.select_instance.select_by_visible_text(option) Log.log_step("下拉框元素选中: %s.(元素位置:%s)" % (option, self.locator))
def row_text_should_equal(self,row,text): cell_text = self.__find_element_by_row(row).text if cell_text != text: Log.log_error_info("%s is not equal %s, the real text is %s\n" % (self.class_name,text, cell_text)) Log.log_step("通过判断,表的%d行等于文本:%s.(元素位置:%s)"%(row,text,self.locator))
def do_get(self, url, **kwargs): ''' 发送get请求 ''' self.http_response = self.http_hander.get(url, **kwargs) Log.log_step("发送get请求,请求是:%s" % (url)) return self.http_response
def make_un_selected(self): if self.is_selected(): self.click() if self.is_selected(): Log.log_error_info("couldn't make %s un-select" % (self.class_name)) Log.log_step("不选中复选框元素.(元素位置:%s)" % (self.locator))
def row_column_text_should_not_null(self, row, column): cell_text = self.__find_element_by_row_column(row, column).text if cell_text is not None: return True else: Log.log_error_info("未根据自动分配规则分配任务给电销") Log.log_step("通过判断,表的%d行第%d列不为空"%(row, column))
def select_by_index(self, index=1): ''' 选中指定的index项 ''' self.bind() self.select_instance.select_by_index(index) Log.log_step("下拉框元素选中位置为%d的选项.(元素位置:%s)" % (index, self.locator))
def set_attribute(self, attribute, vaule): ''' 设置属性 ''' el = self.element() self.driver.execute_script("arguments[0].setAttribute('%s' ,'%s)"%(attribute, vaule), el) Log.log_step("设置元素属性%s=%s.(元素位置:%s)"%(attribute,vaule,self.locator))
def should_enable(self): ''' 判断元素是可用 ''' if not self.get_element().is_enabled(): Log.log_error_info("%s should be enable\n" % (self.class_name)) Log.log_step("通过判断,元素可用.(元素位置:%s)"%(self.locator))
def response_keys_type_is_right(self, *args, **kwargs): ''' 判断响应的python格式的指定字段格式如target_struct指定的格式. eg. response_keys_type_check("content", "list", 0, target_struct = {"name" : "STRING", "type" :"HASH", "index" : "INT", "verify" : "BOOL"}) #e.g response_keys_type_check(target_struct = {"name" : "STRING", "type" :"HASH", "index" : "INT", "verify" : "BOOL"}) 该结果表示将结果的result['content']['list'][0]同target_struct指定的字段和类型做对比 ''' target_struct = kwargs["target_struct"] content = self.conver_response_body_to_struct() for key in args: content = content[key] for key, value in target_struct.items(): #类型是None,需要特别处理 if value == "NULL": if content[key] is not None: Log.log_error_info( "Verify..., the type of key %s is not None type" % (key)) else: if not isinstance(content[key], HttpHandle.TYPES[value]): Log.log_error_info( "Verify..., the type of key %s is not %s " % (key, value)) Log.log_info("Verify..., response deep key type is ok.") Log.log_step("检查响应结构内容符合指定内容")
def mouse_over(self, interval_time =2): ''' 模拟鼠标放到元素上面 ''' self.action.move_to_element(self.get_element()) self.action.perform() time.sleep(interval_time) Log.log_step("鼠标放在元素上.(元素位置:%s)"%(self.locator))
def should_equal_text(self, expected_text): ''' 判断字符串等于指定的字符串 ''' text = self.get_text() if text != expected_text: Log.log_error_info("%s is not equal %s, the real text is %s\n" % (expected_text, text, self.class_name)) Log.log_step("通过判断,元素等于文本:%s.(元素位置:%s)"%(expected_text,self.locator))
def send_enter_key(self): ''' 在元素上,按Enter键 ''' try: self.web_element.send_keys(Keys.ENTER) Log.log_step("元素上按ENTER键.(元素位置:%s)"%(self.locator)) except: Log.log_error_info("fail to send tab key for %s\n" % (self.class_name))
def selected_option_should_equal(self, option): ''' 比对选中的条目符合特定的选项 ''' selected_option = self.get_selected_option().text.strip() if option != selected_option: Log.log_error_info("selected option is %s, not %s \n" % (selected_option, option)) Log.log_step("下拉框元素当前选中为: %s.(元素位置:%s)" % (option, self.locator))
def send_tab_key(self): ''' 在元素上,按Tab键 ''' try: self.web_element.send_keys(Keys.TAB) Log.log_step("元素上按TAB键.(元素位置:%s)"%(self.locator)) except: Log.log_error_info("fail to send tab key for %s\n" % (self.class_name))
def clear(self): ''' 清空文本框 ''' try: self.web_element.clear() Log.log_step("清空元素文本.(元素位置:%s)"%(self.locator)) except: Log.log_error_info("Couldn't clear %s\n" % (self.class_name))
def should_not_include_text(self, include_text): ''' 判断元素不包含指定的字符串 ''' text = self.get_text() index = text.find(include_text) if -1 != index: Log.log_error_info("%s include %s, the real text is %s\n" % (self.class_name, include_text, text)) Log.log_step("通过判断,元素不包含文本:%s.(元素位置:%s)"%(include_text,self.locator))
def is_exist(self): ''' 元素存在,返回真;元素存在,返回假 ''' try: Log.log_step("返回元素是否存在标示.(元素位置:%s)"%(self.locator)) return self.web_element.is_displayed() except: return False
def select_by_value(self, value): ''' 选中指定的内容项 ''' self.bind() try: self.select_instance.select_by_value(value) except: Log.log_error_info("Couldn't select option %s \n" % (value)) Log.log_step("下拉框元素选中: %s.(元素位置:%s)" % (value, self.locator))
def response_dictionary_should_not_have_key(self, key): ''' 判断响应的格式有指定字段 ''' dic = self.conver_response_body_to_struct() if key in dic: Log.log_error_info("Verify..., response key has key: %s" % (key)) else: Log.log_info("Verify...,ok, response has not key..") Log.log_step("检查响应不含有字段:%s" % (key))
def response_body_should_be_dictionary_struct(self): ''' 判断响应是dictionary格式,否则报错 ''' if not isinstance(self.conver_response_body_to_struct(), HttpHandle.TYPES['HASH']): Log.log_error_info("Verify..., response struct is not dictionary.") else: Log.log_info("Verify..., response struct is ok.") Log.log_step("检查响应是HASH结构")
def response_body_should_be_list_struct(self): ''' 判断响应是list格式,否则报错 ''' if not isinstance(self.conver_response_body_to_struct(), HttpHandle.TYPES['LIST']): Log.log_error_info("status code should be list") else: Log.log_info("Verify..., response struct is ok.") Log.log_step("检查响应是list结构")
def response_string_should_not_include(self, sub_string): ''' 判断响应字符串不包含指定的字符串 ''' if sub_string in self.get_response_body(): Log.log_error_info( "Verify..., response string does inclue string: %s, real response is: %s" % (sub_string, self.get_response_body())) else: Log.log_info("Verify..., response content is ok.") Log.log_step("检查响应不含有字符串:%s" % (sub_string))
def should_exist(self): ''' 判断元素是否存在 ''' try: is_displayed = self.web_element.is_displayed() if not is_displayed: Log.log_error_info("%s is not existed\n" % (self.class_name)) except: Log.log_error_info("%s is has not display method\n" % (self.class_name)) Log.log_step("通过判断,元素存在.(元素位置:%s)"%(self.locator))
def response_code_status_should_be(self, status_code): ''' 判断响应的状态码是否是指定的状态码,不是就报错 ''' if status_code != self.get_response_status_code(): Log.log_error_info( "status code should be %d, but the real value is %d" % (status_code, self.get_response_status_code())) else: Log.log_info("Verify..., status code is ok.") Log.log_step("检查状态码是:%d" % (status_code))
def do_post_with_json_string_payload(self, url, playload=None, **kwargs): ''' 发送json格式字符串post请求 ''' self.http_payload = json.loads(playload) self.http_request = url self.http_response = self.http_hander.post(self.http_request, json=self.http_payload, **kwargs) Log.log_step("发送post请求,请求是:%s" % (url)) return self.http_response
def do_post(self, url, data=None, **kwargs): ''' 发送post请求,且使用data作为playload参数 ''' self.http_payload = data self.http_request = url self.http_response = self.http_hander.post(self.http_request, data=self.http_payload, **kwargs) Log.log_step("发送post请求,请求是:%s" % (url)) return self.http_response
def get_text(self, time_interval = 2): ''' 获得元素文本 ''' try: #实际使用中发现,需要等待2秒才能稳定得到文本 if time_interval > 0 : time.sleep(int(time_interval)) Log.log_step("获取元素文本.(元素位置:%s)"%(self.locator)) return self.web_element.text except: Log.log_error_info("fail to get text for %s\n" % (self.class_name))
def response_dictionary_should_have_not_keys(self, key_list=[]): ''' 判断响应的格式有指定的字段集合 ''' dic = self.conver_response_body_to_struct() for key in key_list: if key in dic: Log.log_error_info("Verify..., response key has key: %s" % (key)) else: Log.log_info("Verify..., ok, response has not keys .") Log.log_step("检查响应不含有这些字段:%s" % (key_list))
def do_post_with_json_payload(self, url, json=None, **kwargs): #json_data = json.dumps(playload) ''' 发送json格式post请求 ''' self.http_payload = json self.http_request = url self.http_response = self.http_hander.post(self.http_request, json=self.http_payload, **kwargs) Log.log_step("发送post请求,请求是:%s" % (url)) return self.http_response
def response_dictionary_should_have_key_value(self, key, value): ''' 判断响应的指定字段的内容是指定的内容 ''' dic = self.conver_response_body_to_struct() if dic.get(key) != value: Log.log_error_info( "Verify..., the response dictionary value is %s, not %s" % (dic.get(key), value)) else: Log.log_info("Verify..., response key,value is ok.") Log.log_step("检查响应含有字段:%s,值为%s" % (key, value))