How to send binary data in a browser test during load testing

LegacyForumLegacyForum Posts: 1,669 ✭✭
edited December 2016 in SOAtest
When load testing a web-based (browser) scenario, the browser is not used. Instead, Load Test sends a series of requests to simulate the result of a user action in the browser. If you need to send binary data in the browser request then you need to do some extra setup to prepare the scenario for load testing; otherwise, Load Test will send the contents as text. The most common use case would be when you need to submit a binary file through a web page.

After recording the scenario, first run the scenario using the test configuration "Configure for Load Test". This will configure the scenario with dynamic parameters needed to make the request during load testing.

This example assumes that you are uploading a binary file, in particular a PDF file. Normally if you submit a file through a web page, the request will use a Content-Type of "multipart/form-data", where the POST data will look something like this:

CODE
-----------------------------7d9271373005fa
Content-Disposition: form-data; name="comment1"

first comment
-----------------------------7d9271373005fa
Content-Disposition: form-data; name="binaryFile"; filename="binary.pdf"
Content-Type: application/pdf

the contents of the file
which will contain binary data if this is a PDF file
-----------------------------7d9271373005fa
Content-Disposition: form-data; name="comment2"

second comment
-----------------------------7d9271373005fa
Content-Disposition: form-data; name="submit"

submit
-----------------------------7d9271373005fa--

[attachment=335:scenario.png]

Open in the Load Test perspective the test that submits the PDF file. In the example scenario (see the attached images), this is "Test 4: Click submit". In in the "POST Data" tab, highlight the contents of the file and choose to "Parameterize Selected Text". Name the parameterized value "fileContents" and use a "Scripted" value.

(If the PDF file is large, the contents might be more than can fit in the text box that displays the POST data. In that case, set the scenario to use a small file, run Configure for Load Test, create the parameterized value, and then reset the scenario to used the desired PDF.)

For the scripted value, use the following Python code. Substitute your own path to the binary file.

CODEfrom com.parasoft.api import IOUtil
from java.io import File

def readBinaryFile(context):
    binaryFile = File("c:\\tmp\\binary.pdf")
    return IOUtil.readBinaryFile(binaryFile)

The scripted value must return a value of type byte[]. This is a Java type: you cannot return a Python array. To create the byte[] value, you can use the utility method IOUtil.readBinaryFile(). You can also return a Jython jarray. (See the Jython documentation for more information on how to create a jarray object.)

Your POST data should now use the parameterized ${fileContents} value:

CODE-----------------------------7d9271373005fa
Content-Disposition: form-data; name="comment1"

first comment
-----------------------------7d9271373005fa
Content-Disposition: form-data; name="binaryFile"; filename="binary.pdf"
Content-Type: application/pdf

${fileContents}
-----------------------------7d9271373005fa
Content-Disposition: form-data; name="comment2"

second comment
-----------------------------7d9271373005fa
Content-Disposition: form-data; name="submit"

submit
-----------------------------7d9271373005fa--

Now when you either run the scenario in Load Test or when you run the scenario in SOAtest using the "Validate for Load Test" test configuration, the contents of the PDF file will be sent as binary data.

[attachment=336:scripted_contents.png]
Tagged:

Comments

  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    This information is included in the user's guide for SOAtest 9.0 and later. In the 9.0 user's guide, see "Load Testing > Preparing Web Functional Tests for Load Testing > Configuring Browser Tests to Send Binary Data".
Sign In or Register to comment.