File Upload is achieved using the Java API java.awt.Robot class where awt stands for Abstract window Toolkit.
1. Robot class in java mainly useful to generate native system input events for the purposes of test automations.
2. The primary usage of Robot class is to facilitate automated testing of Java platform implementations
Test Steps:
1. Open Chrome Browser.
2. Enter the URL https://www.zamzar.com/.
3. Click on the button -Add Files
4. Opens the window Dialog box
5. Enter the absolute path of the file : C:\SeleniumArtifacts\TestCaseData.xlsx
6. Click on Enter button in the keyboard.
Java Logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package org.openqa.selenium.tests; import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class FileUploadUsingRobot { WebDriver driver; @Test public void fileuploadTest() throws AWTException, InterruptedException { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); driver.get("https://www.zamzar.com/"); driver.manage().window().maximize(); driver.findElement(By.xpath("//button[@class='btn btn-default btn-lg']")).click(); String fileAbsolutePath = "C:\\SeleniumArtifacts\\TestCaseData.xlsx"; StringSelection clipBoardPath = new StringSelection(fileAbsolutePath); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(clipBoardPath, null); Thread.sleep(5000); //Robot Class Robot robot = new Robot(); //Keyboard Action : CTRL+V robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyRelease(KeyEvent.VK_V); //Keyboard Action : Enter robot.keyPress(KeyEvent.VK_ENTER); robot.keyPress(KeyEvent.VK_ENTER); } } |
Expected Result:
File is uploaded successfully
Keyboard Actions:
1. ctrl + v ->C:\SeleniumArtifacts\TestCaseData.xlsx
2. enter
Conclusion:
File Upload is achieved in Selenium Automation Tool using the Java API.