Upload File
Problems in Selenium while handling window prompt
Selenium find some difficulties to find object and perform actions which is related to mouse or keyboard type of activity.It is difficult to handle Keyboard and mouse events/Action using selenium commands.There are some specific scenario where selenium command is launched but actual event did not fired.At that time Excecution stops and reports error.
Selenium doesn't provide support to handle browser pop-ups and window based pop ups.There are some methods in Robot class which are used to do the interaction with popups in WebApplications.For handling the pop-up and window-prompt we need to use Robot class..
What is Robot Class?
By using Robot Class we can perform actions on keyboard or mouse events.Robot class is very easy to use with automation process. It can be easily integrate with current automation framework.
Some of the methods of robot class which we are going to use
.keyPress();
KeyPress is a method if we pressctrl button of keyboard.
.keyRelease();
If we have press a ctrl button we have to release that button also.
Here i am giving you an example if i press ctrl v from keyboard.Then first i have to
Make an object of Robot Class and implement the robot methods.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL); //This is for pressing ctrl button of Keyboard
r.keyPress(KeyEvent.VK_V); //This is for pressing v button of keyboard
Now we have to release that keys so we will write..
r.keyRelease(KeyEvent.VK_V); //This is for relasing the ctrl button of KeyBoard
r.keyRelease(KeyEvent.VK_CONTROL);//This is for releasing V button of keyBoard.
Here i this scenario i have used Robot class.
Scenario
1.Go to
http://api.checklist.com/login
2.Enter Email as rgulati883@gmail.com
3.Enter Password as jaiguruji
4.Click on Login
5.Click on Checklist1
6.Click on upload
7.Then add path of your File
8.Click on open button.
4Click on Checklist1(0)
5.Click on upload
6.So next step is we copy the path where our file on desktop.
// StringSelection ss = new StringSelection("Your file path ");
StringSelection ss = new StringSelection("C:\\Users\\Hp\\Desktop\\FileuploadTest.txt");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
When you do a cut or copy of text in the operating system, the text is
stored in the clipboard
The following method returns the content that is currently in the
clipboard.
// Calling key press event to press Enter Key from keyboard to place cursor in window textbox
r.keyPress(KeyEvent.VK_ENTER);
//Releaseing the Enter Key
r.keyRelease(KeyEvent.VK_ENTER);
8.After that we copy and paste that path in this FilepathText box.
/*
* Now we are going to trigger CTRL+V action so first we will press CTRL and then V and finally will
* release these key.
*/
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
9.Now click on Open
// After pasting path now we are going to click on Open and that can be
//triggered by pressing enter key from Keyboard.
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
//Release open
10 File is added
package test;
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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestFileupload {
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Hp\\Desktop\\Chromepth\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// WebDriver driver = new FirefoxDriver();
driver.get("http://api.checklist.com/login");
driver.findElement(By.xpath("//*[@id='email']")).sendKeys("rgulati883@gmail.com");
driver.findElement(By.xpath("//*[@id='loginForm']/div[2]/input")).sendKeys("jaiguruji");
driver.findElement(By.xpath("//button[@class='btn btn btn-primary pull-right']")).click();
Thread.sleep(8000);
driver.findElement(By.xpath("//*[@id='userChecklists']/li[1]/a/span")).click();
WebElement UploadImg = driver.findElement(By.xpath("//*[@id='taskUploadFile']"));
UploadImg.click();
// StringSelection ss = new StringSelection("Your file path ");
StringSelection ss = new StringSelection("C:\\Users\\Hp\\Desktop\\FileuploadTest.txt");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot r = new Robot();
// Calling key press event to press Enter Key from keyboard to place cursor in window textbox
r.keyPress(KeyEvent.VK_ENTER);
//Releaseing the Enter Key
r.keyRelease(KeyEvent.VK_ENTER);
/*
* Now we are going to trigger CTRL+V action so first we will press CTRL and then V and finally will
* release these key.
*/
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
// After pasting path now we are going to click on Open and that can be
//triggered by pressing enter key from Keyboard.
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
//Release open
}
}
//After using this code file is added.










