File Upload Support

BrowserMob makes it possible for scripts to upload files as part of the browser automation. This means that if you need to run a performance or load test that uploads files (ie: user-contributed video), it is possible to do with BrowserMob, regardless of whether you’re using our Real Browser User (RBU) service or our Virtual User (VU) service.

File Uploads for Real Browsers

To get started, we recommend first creating a Selenium script that uploads a local file on your company. You’ll note that when recording the script, Selenium will use the “type” command to set the value of the file upload field to the path where the file exists. Play back the script on your desktop to make sure it’s working perfectly.

Next, upload the Selenium script to BrowserMob. It will automatically start to validate and will most likely fail, since the the path referenced in the script (the one on your local desktop) does not exist on BrowserMob’s remote machines. This is OK and expected.

Once the script has been confirmed as “invalid”, click on the script name to edit the script. For our example, let’s assume it looks something like this:

var selenium = browserMob.openBrowser();
 
browserMob.beginTransaction();
browserMob.beginStep("Step 1");
 
selenium.open("http://example.com/video-upload");
selenium.type("description", "My home video");
selenium.type("file", "c:\\foo.m4v");
selenium.clickAndWait("uploadBtn");
 
browserMob.endStep();
browserMob.endTransaction();

Now let’s edit the script slightly. Instead of using a hard-coded path for the file to upload, let’s use a BrowserMob API to get a path to a file that will soon be associated with the script:

var selenium = browserMob.openBrowser();
 
browserMob.beginTransaction();
browserMob.beginStep("Step 1");
 
// get the path for the file named foo.m4v
var filePath = browserMob.filePath("foo.m4v");
 
selenium.open("http://example.com/video-upload");
selenium.type("description", "My home video");
selenium.type("file", filePath);
selenium.clickAndWait("uploadBtn");
 
browserMob.endStep();
browserMob.endTransaction();

What is happening here is we’re now asking BrowserMob for the path to the file with the alias “foo.mp4″. As soon as you save this script, BrowserMob will recognize that you need to upload foo.mp4 and will ask you to do so:

200904010931.jpg

Here you are given a list of all files you need to upload before the script is ready to be validated. The name of each file and the number of files in total is determined by each unique call to browserMob.filePath() in your script. In our example, there was one call with the parameter “foo.m4v”, which is why we were asked to upload one file.

Now when the script is validated or run during any real test, each file will be downloaded to a temporary location on the remote computer. Since the temporary location changes each time, the browserMob.filePath() function helps you dynamically determine the path based on a known alias (ie: foo.m4v). You can then use that path to set the value of the file upload field in the browser.

File Uploads for Virtual Users

File uploads with virtual users are a little different. The most obvious thing is that there is no actual user interface that you need to type the file path in to. Instead, you just need to attach a file directly to a POST request:

var c = browserMob.openHttpClient();
 
browserMob.beginTransaction();
browserMob.beginStep("Step 1");
 
// get a file reference for foo.m4v
var file = browserMob.getFile("foo.m4v");
 
var post = c.newPost("http://example.com/upload");
post.addRequestParameter("foo", "bar"); // regular post params
post.addFileUpload("video_file", file); // upload file as variable "video_file"
var resp = post.execute();
// check the response for success
 
browserMob.endStep();
browserMob.endTransaction();

The key thing here is that we’re asking the BrowserMob API for a handle to the file and then attaching that handle to the POST request.


7 Responses to “File Upload Support”

  1. this file upload didn’t work for me, i’m in trial period, would that be the cause?

    thanks

  2. Ryder,
    Except for the size of the test, trial users have access to the full set of features as our regular customers. We’ve followed up with you through our support channels and have placed a working file upload script in your account so that you have an example to work off of. If you have any further questions, please email support@browsermob.com.

    Thanks!

    Patrick

  3. I would like to test a solution and need to upload a large amount of xml files. Each xml file (24kb – 100kb) has a unique internal identifier.

    Will it be possible to upload a large amount of file to be used for testing? Or is it be possible to change this internal identifier in another way so I can simulate this large amount of unique filers?

    Thanks

  4. This comment seem only support on chrome

  5. Tu,
    You are correct – this only works for Firefox.

    Patrick

  6. The documentation is a little confusing. I got it working with a REST API:


    browserMob.beginStep("Step: Upload");
    var file = browserMob.getFile("foo.jpg");

    var post = client.newPost(base_url + "pictures.json");

    post.addRequestParameter('auth_token', auth_token);
    post.addFileUpload("picture[asset]", file);

    var resp = post.execute();
    var myObject = eval('(' + resp.getBody() + ')');
    browserMob.log("Upload ID: " + myObject.picture.id);

    browserMob.endStep();

  7. Tom,
    Thanks for the feedback. I just updated the example to be a bit clearer!

    Patrick

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

 

© 2012 The BrowserMob Blog Suffusion theme by Sayontan Sinha