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 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 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 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_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_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 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_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 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_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 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 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_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))
def click(self, click_type = None): ''' 点击元素. click_type: 'js' 通过js直接操作元素 ''' try: if (click_type == "js") or (click_type == "JS"): self.driver.execute_script("arguments[0].click()", self.get_element()) else: self.web_element.click() Log.log_step("点击元素.(元素位置:%s)"%(self.locator)) except: Log.log_error_info("fail to click %s\n" % (self.class_name))
def wait_element_disappear(self, timeout = 30, interval = 2): ''' 在指定的时间里面,等待页面元素消失. ''' frequence = int(timeout/interval) for i in range(frequence): self.web_element = self.element() if self.web_element is None: time.sleep(1) return time.sleep(int(interval)) else: Log.log_error_info("fail to wait element %s present.locator is %s\n" % (self.class_name,self.locator)) Log.log_step("等待元素消失.(元素位置:%s)"%(self.locator))
def wait_element_enable(self, timeout = 30, interval = 2): ''' 等待元素变成可用 ''' frequence = int(timeout/interval) for i in range(frequence): self.web_element = self.element() if self.web_element.is_enabled(): if interval > 0: time.sleep(interval) return time.sleep(int(interval)) else: Log.log_error_info("fail to wait element %s enable\n" % (self.class_name)) Log.log_step("等待元素出现.(元素位置:%s)"%(self.locator))
def should_not_exist(self): ''' 判断元素不存在 ''' element = self.element() if element is None: Log.log_step("通过判断,元素不存在.(元素位置:%s)"%(self.locator)) return True else: is_displayed = element.is_displayed() if is_displayed: Log.log_error_info("%s is existed\n" % (self.class_name)) else: Log.log_step("通过判断,元素不存在.(元素位置:%s)"%(self.locator)) return True
def input(self, str="", input_type = None): ''' 输入文本. str:输入文本 input_type:'js',使用js直接进行赋值 ''' try: if (input_type == "js") or (input_type == "JS"): self.driver.execute_script("arguments[0].value='%s'"% (str), self.get_element()) else: self.clear() self.web_element.send_keys(str) Log.log_step("向元素输入文本:%s.(元素位置:%s)"%(str, self.locator)) except: Log.log_error_info("Couldn't input %s for %s\n" % (str, self.class_name))
def response_key_value_is_right(self, *args, **kwargs): ''' 判断响应的python格式的指定字段格式如target_struct指定的格式. eg. response_deep_key_is_struct("content", "list", 0, target_value = {"name" : "benhuang", "index" : "7", "verify" : "true"}) 该结果表示将结果的result['content']['list'][0]同target_struct指定的字段与响应结果做对比 ''' target_struct = kwargs["target_value"] content = self.conver_response_body_to_struct() for key in args: content = content[key] for key, value in target_struct.items(): if content[key] != value: Log.log_error_info( "Verify..., the value of key %s is not %s,it is %s " % (key, value, content[key])) Log.log_info("Verify..., response deep key,value is ok.") Log.log_step("检查响应结构符合指定格式要求,且内容正确")
def wait_element_present(self, timeout = 30, interval = 2): ''' 在设定的时间里,等待元素的出现;超过时间间隔,报错. timeout: 指定的时间 interval:轮询的间隔时间 ''' frequence = int(timeout/interval) for i in range(frequence): self.web_element = self.element() if self.web_element is not None: if interval > 0: time.sleep(int(interval)) return time.sleep(int(interval)) else: Log.log_error_info("fail to wait element %s present.locator is %s\n" % (self.class_name,self.locator)) Log.log_step("等待元素出现.(元素位置:%s)"%(self.locator))
def table_should_inculde_text(self,expected_text): table_text = self.get_table_text() index = table_text.find(expected_text) if -1 == index: Log.log_error_info("%s is not include %s, the real text is %s\n" % (self.class_name, expected_text, table_text)) Log.log_step("通过判断,表的包含文本:%s.(元素位置:%s)"%(expected_text,self.locator))
def test_report(self): Log.log_case_desc("TC001:这个case是生产报告") print(sys.stdout, "<STEP_BEGIN>%s<STEP_END>" % ("点击元素: //a['登陆']1")) print(sys.stdout, "<STEP_BEGIN>%s<STEP_END>" % ("等待元素://a['登陆']1")) Log.log_error_info("meet error when generate report")
def row_column_text_should_equal(self, row, column, text): cell_text = self.get_row_column_text(row, column) if cell_text != text: Log.log_error_info("text is not equal %s, the real text is %s\n" % (text,cell_text)) Log.log_step("通过判断,表的%d行第%d列,等于文本:%s.(元素位置:%s)"%(row, column,text,self.locator))
def make_selected(self): if not self.is_selected(): self.click() if not self.is_selected(): Log.log_error_info("couldn't make %s select" % (self.class_name)) Log.log_step("不选中复选框元素.(元素位置:%s)" % (self.locator))
def get_options(self): self.bind() try: return self.select_instance.options() except: Log.log_error_info("Couldn't get option %s \n")
def row_text_should_include(self,row, include_text): cell_text = self.__find_element_by_row(row).text index = cell_text.find(include_text) if -1 == index: Log.log_error_info("%s is not include %s, the real text is %s\n" % (self.class_name, include_text, cell_text)) Log.log_step("通过判断,表的%d行,包含文本:%s.(元素位置:%s)"%(row,include_text,self.locator))
def __init__(self, host, user, passwd, db, port= 3306): self.conn = None try: self.conn = pymysql.connect(host = host, user = user, passwd = passwd, db = db, port = int(port), charset='utf8') except: Log.log_error_info("fail to connect db %s %s %s %s %s \n" % (host, user, passwd, db, port))