Thursday, 4 December 2014

How to handle windows popup and upload file ?


Let us see the scenario of file upload which invokes the windows based popup,on clicking the browse button or the 'Add file' button.

The first thing people immediately suggest is to use AutoIt.

I have been looking for other possible ways and this worked well for me,

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.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;


 public class UploadDemoWeTransfer {

public static void main(String[] args) throws AWTException, InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.navigate().to("https://www.wetransfer.com/");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

driver.findElement(By.xpath("//*[@id='takeover-skip']")).click();
driver.findElement(By.xpath("//*[@id='accepting']")).click();

driver.findElement(By.xpath("//*[@id='uploader_field']")).click();

WebElement element = driver.findElement(By
.xpath("//*[@id='uploader_field']"));
element.click();

StringSelection ss = new StringSelection(
"C:\\Users\\devatha\\Desktop\\New folder\\note.jpg");

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, 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);
    
    
   new FluentWait<WebDriver>(driver)
                .withTimeout(25, TimeUnit.SECONDS)
                .pollingEvery(5,TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class)
                .until(ExpectedConditions.textToBePresentInElement(By.id("add-file"), "Add more files"));

driver.findElement(By.xpath("//*[@id='add-recipient']")).click();
driver.findElement(By.xpath("//*[@id='to']")).sendKeys(
driver.findElement(By.xpath("//*[@id='add-sender']")).click();
driver.findElement(By.xpath("//*[@id='from']")).sendKeys(
driver.findElement(By.xpath("//*[@id='transfer']")).click();

}

}



Hope this helps. If it does, PAY IT FORWARD .. 



Lots of Energy,


DR

2 comments:

  1. Lots of energy DR .. Payitforward.. take it forward ..way to go DR :) godspeed

    ReplyDelete