Challenges during file upload:
Let us discuss one of the challenges that we face when running your Selenium WebDriver tests on the cloud. To make sense, let us assume we execute tests on a distributed system. Most commonly, the node gets to choose between thousands of random VMs in the cloud. We often run into a situation where we had to upload a file from local. This has always been an issue as the codebase remains in the Hub and the Node receives the commands to drive the test. This creates major issues like file uploads/downloads. The solution is simple. And this works like a charm in almost all the drivers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* param by - locator of the element where you enter the path of the file | |
* param fileName – name of the file to be uploaded | |
*/ | |
public void uploadFile(By by, String fileName) | |
{ | |
try | |
{ | |
WebElement element = driver.findElement(by); | |
LocalFileDetector detector = new LocalFileDetector(); | |
String path = new File("src/test/resources/testdata/").getAbsolutePath() | |
+"/"+ fileName; | |
File file = detector.getLocalFile(path); | |
((RemoteWebElement) element).setFileDetector(detector); | |
element.sendKeys(file.getAbsolutePath()); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
Assert.fail("Your message goes here"); | |
} | |
} |