Posting dynamic values in Locust

2019-08-04 12:49发布

问题:

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_)


标签: python locust