Monday 23 March 2015

How to upload the file using selenium web driver except type the url in send key or autoit

How to upload the file using selenium web driver except type the url in send key or autoit

I have used robot class(generate native system input events)
it is used to take the control of mouse and keyboard event.
Once you get the control, you can do any type of operation related to mouse and keyboard through with java code.
Below example I have used 'keyPress' and 'keyRelease' methods with description.

keyPress - Takes keyCode as Parameter and Presses here a given key.
keyrelease - Takes keyCode as Parameterand Releases a given key.
package automationFramework;.

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.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class fileupload {

    String URL = "http://convertonlinefree.com/PDFToWORDEN.aspx";
    WebDriver driver;
    @Test
    public void testUpload() throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver", "D:\\AutomationPHP\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(URL);
        driver.manage().window().maximize();
        WebElement element = driver.findElement(By.xpath("//*[@id='MainContent_fuDOCX']"));
        element.click();
        uploadFile("C:\\Users\\Administrator\\Desktop\\UCLA - Copy.pdf");
        Thread.sleep(2000);
    }
   
    /*
      This method will set any parameter string to the system's fileupload box.
     */
    public static void setClipboardData(String string) {
        //Class used for copy and paste operations -StringSelection
        System.out.println(string);
           StringSelection stringSelection = new StringSelection(string);
           Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
        }
   
    public static void uploadFile(String fileLocation) {
        try {
            // file location set with file dialogue box
            setClipboardData(fileLocation);
            //CTRL, V and ENTER keys for native key strokes
            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);
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
}


Donate:

Please Donate the some money (anything do you want) for my
blog if you beneficial for this, I will provide more real example for the
latest technique for whom who wants to make a carrier in IT field or solved
some problem, My Name is – RITESH KUMAR SINGH A/C number- 913010044116345 AXIS
Bank LTD :- Vaishali NCR, India-001
 


No comments:

Post a Comment