Posting dynamic values in Locust

眉间皱痕 提交于 2019-12-11 10:33:25

问题


I have an application that I am trying to load test with Locust. If I know the parameters of a post in advance, I can add them to a post and that works fine:

self.client.post("/Login", {"Username":"user", "Password":"a"})

The application uses a bunch of hidden fields that get sent when the page is posted interactively. The content of these fields is dynamic and assigned by the server at runtime to manage sessions etc. e.g.

<input type="hidden" name="$$submitid" value="view:xid1:xid2:xid143:xid358">

Is there a way I can pick these up to add to my post data? I know the names of the hidden inputs.


回答1:


You write a function to extract this data by using PyQuery. You just need to call it before sending post request. If you want to create a bunch of data you can call it in on_start function store them in an array, then use it in tasks. See the example below:

from locust import HttpLocust, TaskSet, task
from pyquery import PyQuery

class UserBehaviour(TaskSet):

    def get_data(self, url, locator):
        data = []
        request = self.client.get(url)

        pq = PyQuery(request.content)
        link_elements = pq(locator)
        for link in link_elements:
            if key in link.attrib and "http" not in link.attrib[key]:
                data.append(link.attrib[key])

        return data

    @task
    def test_get_thing(self):
        data_ = self.get_data("/url/to/send/request", "#review-ul > li > div > a", "href")
        self.client.post("url", data = data_)


来源:https://stackoverflow.com/questions/33914814/posting-dynamic-values-in-locust

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!