Wednesday, 10 February 2016

File uploading in selenium @ 3 ways

File uploading in selenium @ 3 ways:
=============================
We can upload a file using 3 ways

1.Using WebDriver sendKeys() method
2.Using Autoit tool
3.Using Robot class and StringSelection java class

1.Using WebDriver sendKeys() method:
===============================
If we want to upload a file we can upload by using webdriver sendKeys() method.
//example:
-----------
package blog;

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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class FileUploadSendKeys {

WebDriver webDriver;

@BeforeSuite
public void launchingIEBrowser() {

System.setProperty("webdriver.chrome.driver",
"D:\\Ramesh\\ChromeDriver.exe");
webDriver = new ChromeDriver();

// webDriver = new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

}

@Test
public void testScript() throws InterruptedException, AWTException {

webDriver.get("https://www.sendspace.com/");

webDriver.findElement(By.id("upload_file")).sendKeys(
"C:\\Users\\aramesh\\Desktop\\abc.txt");
;
Thread.sleep(2000);

}

@AfterSuite
public void closeBrowser() {
webDriver.quit();

}

}

2.Using Robot class:
================

Robot class is used to (generate native system input events) take the control of mouse and keyboard. Once you get the control, you can do any type of operation related to mouse and keyboard through with java code.

There are different methods which robot class uses. Here in the below example we have used 'keyPress' and 'keyRelease' methods.

keyPress:
-------------
This method with press down arrow key of Keyboard.
Example: robot.keyPress(KeyEvent.VK_DOWN).

keyrelease:
--------------
This method with release down arrow key of Keyboard
Example: robot.keyRelease(KeyEvent.VK_DOWN) 

mousePress():
-----------------
This method will press the right click of your mouse.
mouseMove() :  This will move mouse pointer to the specified X and Y coordinates.
Example: robot.mouseMove(point.getX(), point.getY()):


mouseRelease():
--------------------
This method will release the right click of your mouse.

robot.keyPress(KeyEvent.VK_DOWN):
---------------------------------------------------
To press down arrow key of Keyboard.

robot.keyPress(KeyEvent.VK_TAB):
------------------------------------------------
To press TAB key of keyboard


robot.keyPress(KeyEvent.VK_ENTER):
---------------------------------------------------
To press copy key from the keyboard

robot.keyRelease(KeyEvent.VK_ENTER):
------------------------------------------------------
To press release copy key from the keyboard

robot.keyPress(KeyEvent.VK_ENTER):
----------------------------------------------------
To press Enter key from the keyboard

//example:
--------------
package blog;

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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class RobotFileUpload {

WebDriver webDriver;

@BeforeSuite
public void launchingIEBrowser() {

System.setProperty("webdriver.chrome.driver",
"D:\\Ramesh\\ChromeDriver.exe");
webDriver = new ChromeDriver();

// webDriver = new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

}

@Test
public void testScript() throws InterruptedException, AWTException {

webDriver.get("https://www.sendspace.com/");

webDriver.findElement(By.id("upload_file")).click();
Thread.sleep(2000);
// StringSelection is a class that can be used for copy and paste
// operations.
StringSelection stringSelection = new StringSelection(
"C:\\Users\\aramesh\\Desktop\\abc.txt");
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);
// press enter key of keyboard to perform above selected action
robot.keyRelease(KeyEvent.VK_ENTER);
// release the neter key

Thread.sleep(4000);

webDriver.findElement(By.name("recpemail_fcbkinput")).sendKeys(
"rameshat914@gmail.com");

webDriver.findElement(By.id("ownemail")).sendKeys(
"rameshat914@gmail.com");

Thread.sleep(8000);

}

@AfterSuite
public void closeBrowser() {
webDriver.quit();

}

}

3.Using autoit:
============
Please refer the post autoit in our blog@rameshselenium.blogspot.in

No comments:

Post a Comment