Friday 8 May 2015

Selenium:How to Upload a file using ROBOT Class

package fileupload;
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.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class FileUpload1 {

    WebDriver driver;
    @BeforeClass
    public void beforeClass() {

        System.setProperty("webdriver.firefox.bin",
                "C:\\Users\\Ramesh\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();

    }

    @AfterClass
    public void afterClass() {
        driver.quit();
    }
   
   @Test
    public static void main11() throws AWTException {
    // TODO Auto-generated method stub
    WebDriver  driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("https://www.sendspace.com/");
    WebElement element = driver.findElement(By.xpath("//*[@id='upload_file']"));
    Actions actions = new Actions(driver);
    actions.click(element).build().perform();
    StringSelection stringSelection = new StringSelection("C:\\Users\\IN00914\\Desktop\\CC-BILL.png");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
   
   
    Robot robot = new Robot();
    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);
    }
                                                                   
}

No comments:

Post a Comment