|
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.
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:
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. |
|