How to upload a file through automation when testing on LambdaTest?

I would like to know how can I upload a file from my local system through my automation test scripts?

Hi Tom,

The most optimum way to upload your files while testing on LambdaTest would be using the Selenium Localfile detector Library. The Local File Detector allows the transfer of files from the client machine to the remote server.

Here’s a sample code which you can refer to :

     public void upload() {
        try {
            driver.setFileDetector(new LocalFileDetector());
            driver.manage().window().maximize();

            //Set Selenium Local File Detector
            driver.get("https://angular-file-upload.appspot.com/");

            driver.findElement(By.xpath("//*[@id=\"editArea\"]/div/div/form/fieldset/input[1]")).sendKeys("lambdatest");

	    //Element for file upload	
            WebElement uploadElement = driver.findElement(By.xpath("//*[@id=\"editArea\"]/div/div/form/fieldset/input[2]"));
            
	    // Upload file system path
            uploadElement.sendKeys("D:\\New folder\\1.PNG");
            
            driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div/div/form/fieldset/button")).click();
            //Click on upload button

	   }  catch (Exception e) {
            System.out.println(e);
      
        }
}
1 Like