Selenium Grid 4 File Upload

Selenium grid 4 file upload example.


// PREPARE TEST BY DOWNLOADING
// https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png

driver.get("https://the-internet.herokuapp.com/upload");

WebElement element = driver.findElement(By.id("file-upload"));
LocalFileDetector detector = new LocalFileDetector();
String home = System.getProperty("user.home");
String path = new File(home + "/Downloads/googlelogo_color_272x92dp.png").getAbsolutePath();

File file = detector.getLocalFile(path);
((RemoteWebElement) element).setFileDetector(detector);
element.sendKeys(file.getAbsolutePath());

driver.findElement(By.id("file-submit")).submit();
Thread.sleep(5000); // slow down for demo purposes

if (driver.getPageSource().contains("File Uploaded!")) {
	System.out.println("file uploaded");
} else {
	System.out.println("file not uploaded");
}
NOTE: Gridlastic auto scaling requires all 3 test environment parameters platform, browser and browser version to be specified in each request in order to launch test nodes to fulfill test demand. Video recording is optional. See test environments for capabilities options.
It is important to ensure that "driver.quit()" is always called for proper test execution and creation of video recordings of failed tests.