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 test_APP_179(self): Log.log_case_desc("合法用户成功登录appp") http_handle = HttpHandle() login_url = "http://" + self.config["Host"][ self.env] + self.config["Victy"]["Login"] #检查正确的电话号码 data = eval(self.case_data["APP_179"]["right_account"]) http_handle.do_post(login_url, data) http_handle.response_code_status_should_be(200) http_handle.print_response_python_strcut_body() http_handle.response_keys_type_is_right(target_struct={ "status": "BOOL", "msg": "STRING", "data": "HASH" }) http_handle.response_key_value_is_right(target_value={ "status": True, "msg": "登录成功" }) http_handle.response_keys_type_is_right("data", target_struct={ "userID": "INT", "account": "STRING", "imPass": "******" })
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 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 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 test_report(self): Log.log_case_desc("TC002:这个case是生产报告") print(sys.stdout, "<STEP_BEGIN>%s<STEP_END>" % ("发送 https://baidu.com/1/news")) print(sys.stdout, "<STEP_BEGIN>%s<STEP_END>" % ("发送 https://baidu.com/1/news")) Log.log_error_info("meet error when generate report")
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 select_by_option(self, option): ''' 选中指定的文本项目 ''' self.bind() self.select_instance.select_by_visible_text(option) Log.log_step("下拉框元素选中: %s.(元素位置:%s)" % (option, 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 test_print_response_body(): content = b'{"content": true, "int":1}' response = Response() setattr(response, '_content', content) instance = httpHandler.HttpHandle() instance.http_response = response instance.print_response_body() Log.log_info("test_print_response_body is ok")
def test_get_header(): response = Response() instance = httpHandler.HttpHandle() instance.http_response = response header_dic = {"content-type": "text/html"} instance.set_header(header_dic) TestCase().assertTrue(header_dic == instance.get_header()) Log.log_info("test_get_header is ok")
def quit(): ''' 关闭driver ''' try: WebDriver.driver.quit() except: Log.log_error_info("+++++close driver meet error")
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 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 get_selected_option(self): ''' 获得当前选中的条目 ''' self.bind() try: return self.select_instance.first_selected_option except: Log.log_error_info("Couldn't get selected option %s \n")
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 is_exist(self): ''' 元素存在,返回真;元素存在,返回假 ''' try: Log.log_step("返回元素是否存在标示.(元素位置:%s)"%(self.locator)) return self.web_element.is_displayed() except: return False
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_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 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 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 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 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 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 test_update_header(): response = Response() instance = httpHandler.HttpHandle() instance.http_response = response header_dic = {"content-type": "text/html"} instance.set_header(header_dic) instance.update_header({"add": "test", "content-type": "test"}) header_dic = {"add": "test", "content-type": "test"} TestCase().assertTrue(header_dic == instance.get_header()) header_dic = {"content-type": "test"} instance.delete_header(['add']) TestCase().assertTrue(header_dic == instance.get_header()) Log.log_info("test_update_header is ok")
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("检查响应结构内容符合指定内容")