Since a lot of websites have upload functionality, it's important to know that this can be tested via Selenium.
Uploading a file during a test
With Selenium WebDriver it's possible to upload a file from your own computer. In your test, you indicate which file you would like to upload, after which WebDriver will send the file from your computer, base64-encoded, to the Selenium node.
When you want to upload a file during your test via the TestingBot.com selenium grid, you can use the LocalFileDetector
feature in WebDriver to upload a file from the machine running your test to our test VMs. Since we guarantee a pristine virtual machine for every test, you can be sure that after every test your uploaded files are destroyed.
An example in Java:
import junit.framework.*;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.net.URL;
public class UploadTest extends TestCase {
public static void testUpload() {
String apiKey = "key";
String apiSecret = "secret";
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browserName", "IE");
caps.setCapability("version", "9");
caps.setCapability("name", "Test File Upload");
caps.setCapability("platform", "WINDOWS");
try {
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://" + apiKey + ":" + apiSecret + "@hub.testingbot.com/wd/hub"), caps);
driver.setFileDetector(new LocalFileDetector());
driver.get("http://sl-test.herokuapp.com/guinea_pig/file_upload");
WebElement upload = driver.findElement(By.id("myfile"));
upload.sendKeys("~/mypicture.jpg");
driver.findElement(By.id("submit")).click();
driver.findElement(By.tagName("img"));
Assert.assertEquals("mypicture.jpg (image/jpeg)", driver.findElement(By.tagName("p")).getText());
driver.quit();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Uploading a file before starting a test
In some cases, you might want to make sure a file is already uploaded on our test VMs before your test starts. With TestingBot, you can specify the location (URL) of the file and the path where you want the file to be placed on our VMs. You can put these details in your desired capabilities (the options you send at the start of the test). TestingBot will then first download the file and place it in the location you want.
The way to do this is by specifying in your desired capabilities:
{
browserName: "firefox",
version: 42,
platform: "VISTA",
upload: "http://mywebsite.com/myfile.ext",
uploadFilepath: "C:\\myfile.ext"
}
This will download the file from mywebsite.com/myfile.ext and place it in C:\myfile.ext During your test, you can use the C:\myfile.ext file to test your upload.
The same functionality is also available on our MAC and Linux VMs, where you can store them in the "/tmp" folder for example.
You can find more options for your tests on our test options page.