Example #1
0
 def check_current_activity(self, app_activity):
     '''验证当前activity是否登录传入app_activity'''
     current_activity = self.driver.current_activity
     if current_activity:
         cb.checkEqual(current_activity, app_activity)
     else:
         logging.error('当前没有app_activity')
Example #2
0
 def open_baidu_url(self, describe=None):
     '''打开百度首页首页'''
     if describe: logging.info(describe)
     self.open(data_web.url[0])
     element = WebDriverWait(self.driver, 5,
                             0.5).until(EC.title_is(data_web.url[1]))
     cb.checkEqual(element, True)
Example #3
0
 def input_text_and_search(self, describe=None):
     '''搜索"poseidon并验证结果"'''
     if describe: logging.info(describe)
     try:
         self.click_element(data_web.search_box, is_button=False)
         self.set_text(data_web.search_box, 'poseidon')
         self.click_element(data_web.search_button[0], is_button=True)
         element = WebDriverWait(self.driver, 5, 0.5).until(
             EC.title_is(data_web.search_button[1]))
         cb.checkEqual(element, True)
     except Exception as e:
         logging.info(e)
         raise e
Example #4
0
 def test_parametrize(self, a, b, expected):
     ''' 验证parametrize'''
     diff = a - b
     cb.checkEqual(diff, expected)
Example #5
0
 def test_commonbase(self):
     """验证commonbase中比较参数"""
     cb.checkEqual(1, 1)  # 比较n=expected
     cb.checkResultIsNotNone(2)  # 验证n不为空
     cb.checkDictionary({'a': 1}, {'a': 1})  # 字典比较
     '''
Example #6
0
    def _sendRequest_pycurl(self,
                            method,
                            url,
                            data=None,
                            headers=None,
                            cookie=None,
                            files=None,
                            httpStatusExp=None,
                            statusExp=None,
                            needJson=True,
                            bText=True,
                            agent=None,
                            **kwargs):
        '''
        pycurl发送发送API请求,支持application/x-www-form-urlencoded,application/json格式
        同时输出请求时间片段

        '''

        try:
            import pycurl

            b = BytesIO()  # 创建缓存对象
            c = pycurl.Curl()  # 创建一个curl对象
            c.setopt(pycurl.URL, url)
            c.setopt(pycurl.WRITEDATA, b)  # 设置资源数据写入到缓存对象
            if pyconfig["sections"].get('http').get("debug").lower() == 'true':
                c.setopt(pycurl.VERBOSE, True)

            logging.info("请求url: {}".format(url))
            logging.info("请求method: {}".format(method))

            if headers is None:
                c.setopt(pycurl.HTTPHEADER, self._default_header)
                logging.info("请求header: {}".format(self._default_header))
                logging.info("请求header type is: {}".format(
                    type(self._default_header)))
            else:
                if isinstance(headers, list):
                    c.setopt(pycurl.HTTPHEADER, headers)
                    logging.info("请求header: {}".format(headers))
                    logging.info("请求header type is: {}".format(type(headers)))
                else:
                    c.setopt(pycurl.HTTPHEADER,
                             self.conver_header_dict_2_list(headers))
                    logging.info("请求header: {}".format(
                        self.conver_header_dict_2_list(headers)))
                    logging.info("请求header type is: {}".format(
                        type(self.conver_header_dict_2_list(headers))))

            if agent:
                c.setopt(pycurl.USERAGENT, agent)  # agent加入c对象
                logging.info("请求agent:{}".format(agent))
            else:
                agent = self.get_agent()
                c.setopt(pycurl.USERAGENT, agent)  # agent加入c对象

            if cookie:
                c.setopt(pycurl.COOKIE, cookie)  # cookie加入c对象

            if data:
                new_headers = self.dict_key_to_lower(headers)
                if 'content-type' in new_headers and new_headers.get(
                        'content-type') == 'application/x-www-form-urlencoded':
                    c.setopt(pycurl.POSTFIELDS,
                             self.conver_data_dict_to_str(data))
                    logging.info("请求data: {}".format(data))
                elif 'content-type' in new_headers and new_headers.get(
                        'content-type') == 'application/json':
                    c.setopt(pycurl.POSTFIELDS, json.dumps(data))
                    logging.info("请求data: {}".format(json.dumps(data)))

            if method == "GET":
                pass
            elif method == "PUT":
                c.setopt(pycurl.CUSTOMREQUEST, "PUT")
            elif method == "DELETE":
                c.setopt(pycurl.CUSTOMREQUEST, "DELETE")
            elif method == "POST":
                c.setopt(pycurl.CUSTOMREQUEST, "POST")
            elif method == "PATCH":
                c.setopt(pycurl.CUSTOMREQUEST, "PATCH")
            else:
                logging.error('not support method: {}'.format(method))

            # 增加容错处理,默认retry_num=3
            for i in range(self._retry_num):
                try:
                    self.bContinue = False
                    c.perform()

                    total_time = c.getinfo(pycurl.TOTAL_TIME)  # 上一请求总的时间
                    dns_time = c.getinfo(pycurl.NAMELOOKUP_TIME)  # 域名解析时间
                    connect_time = c.getinfo(pycurl.CONNECT_TIME)  # 远程服务器连接时间
                    redirect_time = c.getinfo(
                        pycurl.REDIRECT_TIME)  # 重定向所消耗的时间
                    ssl_time = c.getinfo(pycurl.APPCONNECT_TIME)  # SSL建立握手时间
                    pretrans_time = c.getinfo(
                        pycurl.PRETRANSFER_TIME)  # 连接上后到开始传输时的时间
                    starttrans_time = c.getinfo(
                        pycurl.STARTTRANSFER_TIME)  # 接收到第一个字节的时间

                    transfer_time = total_time - starttrans_time  # 传输时间
                    serverreq_time = starttrans_time - pretrans_time  # 服务器响应时间,包括网络传输时间
                    if ssl_time == 0:
                        if redirect_time == 0:
                            clientper_time = pretrans_time - connect_time  # 客户端准备发送数据时间
                            redirect_time = 0
                        else:
                            clientper_time = pretrans_time - redirect_time
                            redirect_time = redirect_time - connect_time
                        ssl_time = 0
                    else:
                        clientper_time = pretrans_time - ssl_time

                        if redirect_time == 0:
                            ssl_time = ssl_time - connect_time
                            redirect_time = 0
                        else:
                            ssl_time = ssl_time - redirect_time
                            redirect_time = redirect_time - connect_time

                    connect_time = connect_time - dns_time
                    logging.info('请求总时间: %.3f ms' % (total_time * 1000))
                    logging.info('DNS域名解析时间 : %.3f ms' % (dns_time * 1000))
                    logging.info('TCP连接消耗时间 : %.3f ms' % (connect_time * 1000))
                    logging.info('重定向时间: %.3f ms' % (redirect_time * 1000))
                    logging.info('SSL握手消耗时间 : %.3f ms' % (ssl_time * 1000))
                    logging.info('客户端发送请求准备时间: %.3f ms' %
                                 (clientper_time * 1000))
                    logging.info('服务器处理时间: %.3f ms' % (serverreq_time * 1000))
                    logging.info('数据传输时间: %.3f ms' % (transfer_time * 1000))

                    reps_code = c.getinfo(pycurl.RESPONSE_CODE)  # 返回code
                    body = b.getvalue()
                    c.close()
                    break
                except Exception as e:
                    msg = "send request [%s] %s failed: %s" % (method, url,
                                                               str(e))
                    logging.info(e)
                    logging.info(msg)
                    if (str(e).find('Max retries exceeded') > 0
                            or str(e).find('Read timed out') > 0
                            or str(e).find('Connection aborted') > -1
                        ) and i + 1 < self._retry_num:
                        time.sleep(10)
                        self.bContinueb = True
                        continue
                    assert False, msg
                finally:
                    if not self.bContinue:
                        pass

        except Exception as e:
            logging.error(e)

        if httpStatusExp:
            logging.info("校验httpStatusExp")
            cb.checkEqual(reps_code, httpStatusExp)

        resp = body.decode('utf-8')
        logging.info("响应response:{}".format(resp))

        if needJson:
            return json.loads(resp)
        else:
            return resp