How to upload a file using Robot Class?
package pkg_selenium;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class UploadFile {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException, AWTException {
// TODO Auto-generated method stub
// Initialize WebDriver
driver = new FirefoxDriver();
// Wait For Page To Load
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://sandbox.checklist.com/login");
// Maximize Window
driver.manage().window().maximize();
// Enter UserName
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).sendKeys("rajendra1988@gmail.com");
// Enter Password
driver.findElement(By.name("j_password")).clear();
driver.findElement(By.name("j_password")).sendKeys("test123");
// Click on 'Login' button
driver.findElement(By.name("login")).click();
Thread.sleep(15000L);
// Click on 'checklist1' link
driver.findElement(By.xpath("//*[@id='userChecklists']/li")).click();
Thread.sleep(3000L);
// Click on 'upload' link
driver.findElement(By.id("taskUploadFile")).click();
Thread.sleep(15000L);
StringSelection ss = new StringSelection("D:\\MouseOverScript.txt");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
System.out.println("Copied the file");
// Declare Robot Class Object
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
System.out.println("Test Completed");
Thread.sleep(30000L);
driver.close();
}
}
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class UploadFile {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException, AWTException {
// TODO Auto-generated method stub
// Initialize WebDriver
driver = new FirefoxDriver();
// Wait For Page To Load
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://sandbox.checklist.com/login");
// Maximize Window
driver.manage().window().maximize();
// Enter UserName
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).sendKeys("rajendra1988@gmail.com");
// Enter Password
driver.findElement(By.name("j_password")).clear();
driver.findElement(By.name("j_password")).sendKeys("test123");
// Click on 'Login' button
driver.findElement(By.name("login")).click();
Thread.sleep(15000L);
// Click on 'checklist1' link
driver.findElement(By.xpath("//*[@id='userChecklists']/li")).click();
Thread.sleep(3000L);
// Click on 'upload' link
driver.findElement(By.id("taskUploadFile")).click();
Thread.sleep(15000L);
StringSelection ss = new StringSelection("D:\\MouseOverScript.txt");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
System.out.println("Copied the file");
// Declare Robot Class Object
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
System.out.println("Test Completed");
Thread.sleep(30000L);
driver.close();
}
}
Nice Article, Please do post this kind of posts more, thanks.
ReplyDeleteI wish you might like to have a look at my article http://javaseleniumworld.com/robot-class/
Thank you... Please visit http://seleniumbestpractices.blogspot.com for more updates....
ReplyDelete