How to upload the file using Junit Selenium? - java

I tried to upload the file using JUNIT_Selenium.
WebElement fileInfo = driver.findElement(By.xpath("//input[#type='file']"));
fileInfo.sendKeys('my file root');
But, the popup window was not closed in this way :(
Cloud you please give me the answer to solve this problem?
(browser used : Chrome)

I have used some special library Robot.
details.ClickChooseFile();
StringSelection ss = new StringSelection("C:\\Your File");
waitmethod.Waitsec();
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
waitmethod.Waitsec();
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);
details.ClickSubmitEmpl();

Related

Download PDF opened in new Window using Selenium JAVA

There are many questions which are similar to this has been asked in Stackoverflow. But this is something different
I am now currently automating an web application in which at the end of the application, PDF will be generated (Which will be displayed as a link, on clicking it PDF will open in a new window).
I am now currently using AUTOIT to automate Save AS dialog box. Is there any other way to download without using AutoIT.
I tried fetching the PDF URL from the link in the application under tag, but it is actually invoking a JS to open a PDF in new Window
In new window PDF is actually in Embed tag in which src is about::blank
I fetched PDF complete src but when I used that URL without clearing any cookies, I am unable to get the PDF
Is there any suggestions for this issue?
Upload File Using ROBOT Class
public String imagePath = System.getProperty("user.dir") +"\\src\\test\\java\\filepath.pdf";
public void uploadFileWithRobot(String imagePath) {
StringSelection stringSelection = new StringSelection(imagePath);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
robot.delay(250);
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.delay(150);
robot.keyRelease(KeyEvent.VK_ENTER);
}
Now Click on PDF and its open in new window than Verify text is present or not in PDF
public String readPDFInURL(String text) throws EmptyFileException, IOException {
System.out.println("Enters into READ PDF");
String output = "";
URL url = new URL(driver.getCurrentUrl());
System.out.println("url : " + url);
InputStream is = url.openStream();
BufferedInputStream fileToParse = new BufferedInputStream(is);
PDDocument document = null;
try {
document = PDDocument.load(fileToParse);
output = new PDFTextStripper().getText(document);
if (output.contains(text)) {
System.out.println("Element is matched in PDF is : " + text);
test.log(LogStatus.INFO, "Element is displayed in PDF " + text);
} else {
System.out.println("Element is not matched in PDF");
test.log(LogStatus.ERROR, "Element is not displayed in PDF :: " + text);
throw new AssertionError("Element is not displayed" + text);
}
} finally {
if (document != null) {
document.close();
}
fileToParse.close();
is.close();
}
return output;
}
Yes. You can download a file without using third party like AutoIT. Below is the code that works perfectly fine,
First you have to switch to the window.
Then apply some wait. So that the page will be loaded completely.
Now you have to trigger key events i.e. ctrl + s
Pop-up will be appeared on the page to save your file.
Just hit enter. So that you can save your file.
String str = TestUtil.switchToWindow();
pause(3000);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(2000);
Robot robot = new Robot();
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB); // file replace move to yes button
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
TestUtil.switchBackToParentWindow(str);

photo upload using Robot class in java

I use a mac machine and i am trying to automate photo upload scenario.
This is the step:
1. Click on "Add photo" element in a webpage
2. It will bring up the system dialogue box to select
3. Once I select a photo in the system dialogue box (either double click the image or single click and click on "open" button ) then
4. I will see a pop up from the web page which shows the preview of the selected photo.
5.In that Pop-up I click on the "Upload" button.
The Robot code I use selects the image from my desktop, but does't do any action after that.
Any suggestions? Or any better way to do this?
public photo_upload_page clickOnAddphoto() throws Exception {
waitAndClickElement(addPhoto);
Thread.sleep(1000);
File file = new File("/Users/mohand/Desktop/Defect.jpg");
StringSelection stringSelection = new StringSelection(file.getAbsolutePath());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_TAB);
robot.delay(500);
//Open Goto window
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_G);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_G);
//Paste the clipboard value
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_V);
//Press Enter key to close the Goto window and Upload window
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(500);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(1000);
waitAndClickElement(buttonUploadPhoto);
return new photo_upload_page();
}
}
Ok I just figured it out. I just added the below code couple of times after steps 4 and it worked!
robot.keyPress(KeyEvent.VK_ENTER);

Robot Class is not working for file upload in IE 11 in Selenium webDriver

I am trying to upload a file in selenium webdriver using java in IE11. Below code is clicking on Browse button but it is not entering or pasting the file name to be uploaded on the newly opened window. It just stucks and nothing happens. Not able to debug the code also.Seems that Robot Class is not responding.
I also tried Send keys also but the behaviour is not consistent.
<input name="ctl00$PlaceHolderMain$UploadDocumentSection$ctl05$InputFile" title="Choose a file" class="ms-fileinput ms-fullWidth" id="ctl00_PlaceHolderMain_UploadDocumentSection_ctl05_InputFile" onfocus="ResetSpFormOnSubmitCalled();" onchange="CheckAssetLibMediaExtension()" type="file" size="35">
driver.manage().window().maximize(); WebElement element12 = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(Main.newdocument(driver))); Main.newdocument(driver).click(); Thread.sleep(500); element12 = driver.findElement(By.xpath("//iframe[#class='ms-dlgFrame']")); driver.switchTo().frame(element12);
Thread.sleep(2000);
WebElement element = driver.findElement(By.xpath("//input[#type='file']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Robot robot = new Robot();
StringSelection sel = new StringSelection("C:\\Users\\m9kuil\\Desktop\\ImportAttendeeTemplate.xlsx");
// Copy to clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
// Release CTRL+V
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
//Press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
Try once by writing robot.delay(10000); before comment //Press Enter
Other simple alternative for uploading file without robot class :
driver.findElement(By.xpath(".//input[#type='file']")).sendKeys("C:\\Users\\m9kuil\\Desktop\\ImportAttendeeTemplate.xlsx");
wait(10000)

Uploading is not working in selenium using Robot in Chrome Broswer

I am using the Robot class to upload a file but it's not working.
File explorer is appearing for uploading but the Robot class is not working:
driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/automation-practice-form");
driver.manage().window().maximize();
driver.findElement(By.id("photo")).click;
Thread.sleep(2000);
StringSelection stringSelection = new StringSelection("C:\\Users\\Desktop\\Bug\\ui_1.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
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);
How ever Robot is working fine using chrome browser. Facing issue with Firefox. Is there any solution available?
You can actually avoid using Robot and solve it with selenium only. Just send keys to the file input and then submit the form:
WebElement photo = driver.findElement(By.id("photo"));
photo.sendKeys("C:\\Users\\Desktop\\Bug\\ui_1.png");
// TODO: fill out other fields
// this would find the corresponding form and submit it
photo.submit();
Wait for few seconds before hitting enter. Add robot.delay(3000); in your code as follows. This might solve the problem.
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.delay(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Using selenium with ajax

While uploading file in http://pdftableconverter.com/ with selenium
I have a problem to download file converted
to download xls file I need to click on the Like button which found in ajax web page check link to open image to see the problem
https://drive.google.com/file/d/0B4cxDnPAjctLOFcyZ29xeENHSG8/view?usp=sharing
My problem i need to click the like button to download the xls file
WebDriver driver = new FirefoxDriver();
driver.get("http://pdftableconverter.com/");
File file=null;
try {
file = new File(SeleniumProg.class.getClassLoader().getResource("21.pdf").toURI());
Assert.assertTrue(file.exists());
System.out.println("File Exited");
driver.findElement(By.name("userfile")).sendKeys(file.getAbsolutePath());
WebElement browseButton = driver.findElement(By.id("uploadButton"));
new Actions(driver).click(browseButton).perform();
//put path to your image in a clipboard
StringSelection ss = new StringSelection(file.getAbsolutePath());
//To Clear User Selection
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
//imitate mouse events like ENTER, CTRL+C, CTRL+V
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);
WebElement d = driver.findElement(By.id("email"));
d.sendKeys("osama70087#gmail.com");
driver.findElement(By.id("submit_form")).click();
System.out.println("kkkkkkkk");

Categories