Exemple #1
0
    def tap(self, step):
        action = TouchAction(self.driver)

        elements = step['elements']  # click 支持多个元素连续操作,需要转换为 list
        # data = step['data']

        location = ''

        for element in elements:
            if ',' in element:
                position = element.split(',')
                x = int(position[0])
                y = int(position[1])
                position = (x, y)
                self.driver.tap([position])
                sleep(0.5)
            else:
                location = self.locat(element, 'CLICK')
                action.tap(location).perform()
                sleep(0.5)

        # 获取元素其他属性
        output = step['output']
        for key in output:
            if output[key] == 'text':
                vars.put({key: location.text})
            elif output[key] == 'tag_name':
                vars.put({key: location.tag_name})     
            elif output[key] in ('text…', 'text...'):
                if location.text.endswith('...'):
                    vars.put({key: location.text[:-3]})
                else:
                    vars.put({key: location.text})
            else:
                vars.put({key: location.get_attribute(output[key])})
Exemple #2
0
    def click(self, step):
        data = step['data']

        location = ''
        for element in step.get('elements'):
            # location = locating(self.driver, element, 'CLICK')
            location = self.locat(element, 'CLICK')
            try:
                location.click()
            except ElementClickInterceptedException:  # 如果元素为不可点击状态,则等待1秒,再重试一次
                sleep(1)
                if data.get('mode'):
                    self.driver.execute_script("arguments[0].click();",
                                               location)
                else:
                    location.click()
            sleep(0.5)

        # 获取元素其他属性
        output = step['output']
        for key in output:
            if output[key] == 'text':
                vars.put({key: location.text})
            elif output[key] == 'tag_name':
                vars.put({key: location.tag_name})
            elif output[key] in ('text…', 'text...'):
                if location.text.endswith('...'):
                    vars.put({key: location.text[:-3]})
                else:
                    vars.put({key: location.text})
            else:
                vars.put({key: location.get_attribute(output[key])})

        return location
Exemple #3
0
    def click(self, step):
        elements = step['elements']  # click 支持多个元素连续操作,需要转换为 list
        # data = step['data']

        location = ''
        for element in elements:
            location = self.locat(element, 'CLICK')
            sleep(0.5)
            try:
                location.click()
            except ElementClickInterceptedException:  # 如果元素为不可点击状态,则等待1秒,再重试一次
                sleep(1)
                location.click()
            sleep(0.5)

        # 获取元素其他属性
        output = step['output']
        for key in output:
            if output[key] == 'text':
                vars.put({key: location.text})
            elif output[key] == 'tag_name':
                vars.put({key: location.tag_name})
            elif output[key] in ('text…', 'text...'):
                if location.text.endswith('...'):
                    vars.put({key: location.text[:-3]})
                else:
                    vars.put({key: location.text})
            else:
                vars.put({key: location.get_attribute(output[key])})
Exemple #4
0
    def title(self, data, output):
        log.debug(f'DATA:{repr(data["text"])}')
        log.debug(f'REAL:{repr(self.driver.title)}')
        # try:
        if data['text'].startswith('*'):
            assert data['text'][1:] in self.driver.title
        else:
            assert data['text'] == self.driver.title

        for key in output:
            vars.put({key: self.driver.title})
Exemple #5
0
 def current_url(self, data, output):
     log.debug(f'DATA:{repr(data["text"])}')
     log.debug(f'REAL:{repr(self.driver.current_url)}')
     try:
         if data['text'].startswith('*'):
             assert data['text'][1:] in self.driver.current_url
         else:
             assert data['text'] == self.driver.current_url
     except:
         raise Exception(
             f'check failure, DATA:{data["text"]}, REAL:{self.driver.current_url}'
         )
     # 只能获取到元素 url
     for key in output:
         vars.put({key: self.driver.current_url})
Exemple #6
0
    def check(self, step):
        data = step['data']
        if not data:
            data = step['expected']
        element = step['element']
        by = element['by']
        output = step['output']

        location = ''
        if by in ('title', 'current_url'):
            getattr(self, by)(data, output)
        else:
            location = self.locat(element)
            for key in data:
                # 预期结果
                expected = data[key]
                # 切片操作处理
                s = re.findall(r'\[.*?\]', key)
                if s:
                    s = s[0]
                    key = key.replace(s, '')

                if key == 'text':
                    real = location.text
                else:
                    real = location.get_attribute(key)
                if s:
                    real = eval('real' + s)

                log.debug(f'DATA:{repr(expected)}')
                log.debug('REAL:{repr(real)}')
                try:
                    compare(expected, real)
                except:
                    raise Exception(
                        f'check failure, DATA:{repr(expected)}, REAL:{repr(real)}'
                    )

            # 获取元素其他属性
            for key in output:
                if output[key] == 'text':
                    v = location.text
                    vars.put({key: v})
                elif output[key] in ('text…', 'text...'):
                    if location.text.endswith('...'):
                        v = location.text[:-3]
                        vars.put({key: v})
                    else:
                        v = location.text
                        vars.put({key: v})
                else:
                    v = location.get_attribute(output[key])
                    vars.put({key: v})

        return location
Exemple #7
0
    def request(self, kw, step):
        url = step['element']
        if url.startswith('/'):
            url = url[1:]

        data = step['data']
        # 测试数据解析时,会默认添加一个 text 键,需要删除
        if 'text' in data and not data['text']:
            data.pop('text')

        _data = {}
        _data['headers'] = json2dict(data.pop('headers', '{}'))
        if data.get('cookies'):
            data['cookies'] = json2dict(data['cookies'])
        if kw == 'get':
            _data['params'] = json2dict(
                data.pop('params', '{}')) or json2dict(data.pop('data', '{}'))
        elif kw == 'post':
            if data.get('text'):
                _data['data'] = data.pop('text').encode('utf-8')
            else:
                _data['data'] = json2dict(data.pop('data', '{}'))
            _data['json'] = json2dict(data.pop('json', '{}'))
            _data['files'] = eval(data.pop('files', 'None'))
        elif kw in ('put', 'patch'):
            _data['data'] = json2dict(data.pop('data', '{}'))

        for k in data:
            for s in ('{', '[', 'False', 'True'):
                if s in data[k]:
                    try:
                        data[k] = eval(data[k])
                    except:
                        log.warning(f'try eval data failure: {data[k]}')
                    break
        expected = step['expected']
        expected['status_code'] = expected.get('status_code', None)
        expected['text'] = expected.get('text', '').encode('utf-8')
        expected['json'] = json2dict(expected.get('json', '{}'))
        expected['cookies'] = json2dict(expected.get('cookies', '{}'))
        expected['headers'] = json2dict(expected.get('headers', '{}'))
        timeout = float(expected.get('timeout', 10))
        expected['time'] = float(expected.get('time', 0))

        for key in keywords:
            if kw.upper() == key.upper():
                self.r.headers.update(self.headers[key.upper()])

        log.debug(f'URL: {self.path + url}')

        # 处理 before_send
        before_send = data.pop('before_send', '')
        if before_send:
            _data, data = getattr(http_handle, before_send)(kw, _data, data)
        else:
            _data, data = getattr(http_handle, 'before_send')(kw, _data, data)

        if _data['headers']:
            for k in [x for x in _data['headers']]:
                if not _data['headers'][k]:
                    del self.r.headers[k]
                    del _data['headers'][k]
            self.r.headers.update(_data['headers'])

        r = ''
        if kw == 'get':
            r = getattr(self.r, kw)(self.path + url,
                                    params=_data['params'], timeout=timeout, **data)
            if _data['params']:
                log.debug(f'PARAMS: {_data["params"]}')

        elif kw == 'post':
            r = getattr(self.r, kw)(self.path + url,
                                    data=_data['data'], json=_data['json'], files=_data['files'], timeout=timeout, **data)
            log.debug(f'BODY: {r.request.body}')

        elif kw in ('put', 'patch'):
            r = getattr(self.r, kw)(self.path + url,
                                    data=_data['data'], timeout=timeout, **data)
            log.debug(f'BODY: {r.request.body}')

        elif kw in ('delete', 'options'):
            r = getattr(self.r, kw)(self.path + url, timeout=timeout, **data)

        log.debug(f'status_code: {repr(r.status_code)}')
        try:  # json 响应
            log.debug(f'response json: {repr(r.json())}')
        except:  # 其他响应
            log.debug(f'response text: {repr(r.text)}')

        response = {'status_code': r.status_code, 'headers': r.headers,
                    '_cookies': r.cookies, 'content': r.content, 'text': r.text}

        try:
            response['cookies'] = requests.utils.dict_from_cookiejar(r.cookies)
        except:
            response['cookies'] = r.cookies

        try:
            j = r.json()
            response['json'] = j
        except:
            response['json'] = {}

        # 处理 after_receive
        after_receive = expected.pop('after_receive', '')
        if after_receive:
            response = getattr(http_handle, after_receive)(response)
        else:
            response = getattr(http_handle, 'after_receive')(response)

        if expected['status_code']:
            if str(expected['status_code']) != str(response['status_code']):
                raise Exception(
                    f'status_code | EXPECTED:{repr(expected["status_code"])}, REAL:{repr(response["status_code"])}')

        if expected['text']:
            if expected['text'].startswith('*'):
                if expected['text'][1:] not in response['text']:
                    raise Exception(
                        f'text | EXPECTED:{repr(expected["text"])}, REAL:{repr(response["text"])}')
            else:
                if expected['text'] == response['text']:
                    raise Exception(
                        f'text | EXPECTED:{repr(expected["text"])}, REAL:{repr(response["text"])}')

        if expected['headers']:
            result = check(expected['headers'], response['headers'])
            log.debug(f'headers check result: {result}')
            if result['code'] != 0:
                raise Exception(
                    f'headers | EXPECTED:{repr(expected["headers"])}, REAL:{repr(response["headers"])}, RESULT: {result}')
            elif result['var']:
                # var.update(result['var'])
                vars.put(result['var'])
                log.debug(f'headers var: {repr(result["var"])}')

        if expected['cookies']:
            log.debug(f'response cookies: {response["cookies"]}')
            result = check(expected['cookies'], response['cookies'])
            log.debug(f'cookies check result: {result}')
            if result['code'] != 0:
                raise Exception(
                    f'cookies | EXPECTED:{repr(expected["cookies"])}, REAL:{repr(response["cookies"])}, RESULT: {result}')
            elif result['var']:
                # var.update(result['var'])
                vars.put(result['var'])
                log.debug(f'cookies var: {repr(result["var"])}')

        if expected['json']:
            result = check(expected['json'], response['json'])
            log.debug(f'json check result: {result}')
            if result['code'] != 0:
                raise Exception(
                    f'json | EXPECTED:{repr(expected["json"])}, REAL:{repr(response["json"])}, RESULT: {result}')
            elif result['var']:
                # var.update(result['var'])
                vars.put(result['var'])
                log.debug(f'json var: {repr(result["var"])}')

        if expected['time']:
            if expected['time'] < r.elapsed.total_seconds():
                raise Exception(
                    f'time | EXPECTED:{repr(expected["time"])}, REAL:{repr(r.elapsed.total_seconds())}')

        output = step['output']
        # if output:
        #     log.debug('output: %s' % repr(output))

        for k, v in output.items():
            if v == 'status_code':
                status_code = response['status_code']
                vars.put({k: status_code})
                log.debug(f'{k}: {status_code}')
            elif v == 'text':
                text = response['text']
                vars.put({k: text})
                log.debug(f'{k}: {text}')
            elif k == 'json':
                sub = json2dict(output.get('json', '{}'))
                result = check(sub, response['json'])
                # var.update(result['var'])
                vars.put(result['var'])
                log.debug(f'json var: {repr(result["var"])}')
            elif k == 'cookies':
                sub = json2dict(output.get('cookies', '{}'))
                result = check(sub, response['cookies'])
                vars.put(result['var'])
                log.debug(f'cookies var: {repr(result["var"])}')
Exemple #8
0
    def sql(self, step):

        response = {}

        _sql = step['element']

        log.debug(f'SQL: {repr(_sql)}')

        row = {}
        if _sql.lower().startswith('select'):
            row = self.db.fetchone(_sql)
            log.debug(f'SQL response: {repr(row)}')
            if not row:
                raise Exception('*** Fetch None ***')

        elif _sql.lower().startswith('db.'):
            _sql_ = _sql.split('.', 2)
            collection = _sql_[1]
            sql = _sql_[2]
            response = self.db.mongo(collection, sql)
            if response:
                log.debug(f'find result: {repr(response)}')
        else:
            self.db.execute(_sql)

        if _sql.lower().startswith('select'):
            text = _sql[6:].split('FROM')[0].split('from')[0].strip()
            keys = dedup(text).split(',')
            for i, k in enumerate(keys):
                keys[i] = k.split(' ')[-1]
            response = dict(zip(keys, row))
            log.debug(f'select result: {repr(response)}')

        expected = step['data']
        if not expected:
            expected = step['expected']
        if 'json' in expected:
            expected['json'] = json2dict(expected.get('json', '{}'))
            result = check(expected.pop('json'), response['json'])
            log.debug(f'json check result: {result}')
            if result['code'] != 0:
                raise Exception(
                    f'json | EXPECTED:{repr(expected["json"])}, REAL:{repr(response["json"])}, RESULT: {result}'
                )
            elif result['var']:
                vars.put(result['var'])
                log.debug(f'json var: {repr(result["var"])}')

        if expected:
            for key in expected:
                sv, pv = expected[key], response[key]
                log.debug(
                    f'key: {repr(key)}, expect: {repr(sv)}, real: { repr(pv)}')

                compare(sv, pv)

        output = step['output']
        if output:

            for k, v in output.items():
                if k == 'json':
                    sub = json2dict(output.get('json', '{}'))
                    result = check(sub, response['json'])
                    vars.put(result['var'])
                    log.debug(f'json var: {repr(result["var"])}')
                else:
                    vars.put({k: response[v]})
            log.debug(f'output: {vars.output()}')