1

I am trying to run a Visual Studio Load Test, but before it runs I have to call a login webAPI to generate a JWT token and then use this token in all of the webAPI web tests.

I only need to call the login to generate the token once. The same token value is used in all my other webAPI calls. How can I put this step into my load test before I really trigger the load test?

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
Geri
  • 41
  • 6
  • I have edited the question and I hope it is now clearer. Please [edit] it to correct it if I have misunderstood what you were asking. – AdrianHHH Apr 13 '18 at 09:28

1 Answers1

1

The "Test mix" part of a load test can specify an "initialisation" test that can do the login and collect the token. One of the context parameters (CP) of a load test is the $LoadTestUserContext. This is intended for holding values between tests executed for the same virtual user (VU). The login test can save the token into the $LoadTestUserContext and the other tests can access it from there.

Note that each VU has its own $LoadTestUserContext. If the "percentage of new users" in the scenario is high then the initialisation test for a new VU (i.e. the login test) will be executed many times.

If all the VUs should use the same token value then another possibility is to create a scenario that executes the login test once, by setting the iterations property of that scenario to 1; also set a constant load of 1. This test could have a PostRequest or a PostWebTest plugin that saves the token value into a static variable of some class. A second scenario then runs all of the real load test cases, its "delay start time" property could be a few seconds, long enough to run the login test. A plugin of these tests could write the static value into a CP of the test.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
  • Thank you for your answer. for the second way, do you mean I need to write a postrequest plugin to assign the token into a static variable? – Geri Apr 17 '18 at 16:16
  • this way is working for my test when I run the load test with my local setting and with any number of threads. but when I run the test from remoter controller and agent. the token value is not always assigned correctly if I have more than one thread for the web request in the load test. some requests good, most of request failed due to "401 Missing Jwt Token". Do you have any idea what's wrong? Thanks – Geri Apr 23 '18 at 19:52
  • @Geri, How many agent computers? It may be that each agent is getting a different token value. The test suite is copied to each agent computer and each "kind of" runs the test independent of the others. What do you mean by "thread" above? – AdrianHHH Apr 23 '18 at 21:48
  • yeah, you are right, I generate token for each agent, and it works fine. Thanks again. – Geri May 01 '18 at 14:20