Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]如何获取到requests请求到的登录info? #30

Closed
yileye opened this issue Aug 22, 2017 · 8 comments
Closed

[Question]如何获取到requests请求到的登录info? #30

yileye opened this issue Aug 22, 2017 · 8 comments
Labels
duplicate This issue or pull request already exists

Comments

@yileye
Copy link

yileye commented Aug 22, 2017

有这样一个使用场景
1.api会先登录拿到登录信息
2.之后的接口都基于这次的登录信息去请求数据

这个在用例里面应该怎么写?我看了文档后试着写了一下,大概是这个样子
- config:
    name: "Init Testcase."
    request:
        base_url: http://x x:8091

- test:
    name: get token
    request:
        url: /dologin.cl
        method: POST
        headers:
            User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/52.0.2743.116 Safari/537.36
            Content-Type: application/x-www-form-urlencoded
            X-Requested-With: XMLHttpRequest
            Accept: application/json
            Accept-Language: zh-CN,zh;q=0.8
            Accept-Encoding': gzip, deflate
        json:
            userName: xx
            passWord: x x
            sid: x x
    extract_binds:
        - token: content.token
    validators:
        - {"check": "status_code", "comparator": "eq", "expected": 200}

- test:
    name: Jump homepage when data is current
    request:
        url: /zc/realtime1.cl
        method: POST
        headers:
            token: $token
        json:
            edate: 2017-03-08
    validators:
        - {"check": "status_code", "comparator": "eq", "expected": 200}
        - {"check": "content.success", "comparator": "eq", "expected": true}

但是会报错,错误如下:

ERROR [0.086067s]: get token
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/finup/gitlab/ApiTestEngine/ate/task.py", line 17, in runTest
    self.assertTrue(self.test_runner.run_test(self.testcase))
  File "/Users/finup/gitlab/ApiTestEngine/ate/runner.py", line 100, in run_test
    extracted_variables_mapping_list = resp_obj.extract_response(extract_binds)
  File "/Users/finup/gitlab/ApiTestEngine/ate/response.py", line 73, in extract_response
    {key: self.extract_field(field)}
  File "/Users/finup/gitlab/ApiTestEngine/ate/response.py", line 46, in extract_field
    return utils.query_json(json_content, sub_query)
  File "/Users/finup/gitlab/ApiTestEngine/ate/utils.py", line 153, in query_json
    raise exception.ParseResponseError("failed to query json when extracting response!")
ParseResponseError: failed to query json when extracting response!

======================================================================
ERROR [0.000823s]: Jump homepage when data is current
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/finup/gitlab/ApiTestEngine/ate/task.py", line 17, in runTest
    self.assertTrue(self.test_runner.run_test(self.testcase))
  File "/Users/finup/gitlab/ApiTestEngine/ate/runner.py", line 84, in run_test
    parsed_request = self.context.get_parsed_request()
  File "/Users/finup/gitlab/ApiTestEngine/ate/context.py", line 136, in get_parsed_request
    self.testcase_functions_config
  File "/Users/finup/gitlab/ApiTestEngine/ate/testcase.py", line 170, in parse_content_with_bindings
    value, variables_binds, functions_binds)
  File "/Users/finup/gitlab/ApiTestEngine/ate/testcase.py", line 170, in parse_content_with_bindings
    value, variables_binds, functions_binds)
  File "/Users/finup/gitlab/ApiTestEngine/ate/testcase.py", line 197, in parse_content_with_bindings
    parsed_data = parse_variables(content, variables_binds)
  File "/Users/finup/gitlab/ApiTestEngine/ate/testcase.py", line 42, in parse_variables
    "%s is not defined in bind variables!" % variable_name)
ParamsError: token is not defined in bind variables!`
不知道这种情况该如何获取用例中的结果,例如登录的session,我知道requessrs中获取登录cookie只需要使用Session方法就可以,这个就不知道了,能帮我解答一下吗?
@debugtalk
Copy link
Member

@yileye 能先将格式调整为markdown么?这样实在是没法看清你的用例。

@yileye
Copy link
Author

yileye commented Aug 22, 2017

@debugtalk 格式已经调整

@debugtalk
Copy link
Member

@yileye 我看了下你写的用例,总的来说没有啥问题,可能在第一个接口中,headers中的Content-Type与实际不一致;因为如果你传的是json而非data,那么Content-Type应该只能是application/json

另外,推荐在调试用例的时候打开抓包工具,方便看到你执行脚本时实际的请求和响应,然后再跟抓包得到的请求进行对比,基本上就能发现差异了。

@yileye
Copy link
Author

yileye commented Aug 23, 2017

@debugtalk 嗯,可能是我还没有了解清楚脚本的结构,不太熟悉,我想实现的一个功能就是想再后续的请求中重用第一步登录的token,这个在脚本中应该如何使用?因为我后面的脚本在请求时都会验证是否在登录状态,所以会需要登录接口的Session

@debugtalk
Copy link
Member

@yileye 这块儿的说明已经挺多了。
你可以看下 issue #3 ;同时,QuickStart 里面的例子也完全满足你的要求。

@yileye
Copy link
Author

yileye commented Aug 23, 2017

@debugtalk 好的,我先看看文档吧,我刚才找到了关于我刚才提到的问题的代码实现方式,和我之前实现的一样,应该是可以满足我的要求的

@yileye yileye closed this as completed Aug 23, 2017
@debugtalk debugtalk added duplicate This issue or pull request already exists question labels Aug 23, 2017
@yileye
Copy link
Author

yileye commented Aug 23, 2017

@debugtalk 看了你的代码后收获很多,构思相当巧妙,之后如果有机会希望能为这个框架贡献一些代码,使框架更为健壮

@debugtalk
Copy link
Member

@yileye 十分欢迎!

debugtalk added a commit that referenced this issue Mar 22, 2022
v0.2.1

- feat: push load testing metrics to Prometheus Pushgateway
- feat: report events with Google Analytics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants