Yes it’s possible to a with via a script injection. The script needs to create an <input> web element to receive the file sent by SendKeys. Then simulate the dragenter, dragover and drop events on the targeted element with the file set in the dataTransfer object.

This example drops an image from the file on a drop area:

ChromeDriver driver = new ChromeDriver();

driver.get("http://html5demos.com/file-api");

// locate the drop area
WebElement droparea = driver.findElement(By.cssSelector("#holder"));

// drop the file
DropFile(new File("C:\Downloads\image.png"), droparea, 0, 0);

The DropFile method:

public static void DropFile(File filePath, WebElement target, int offsetX, int offsetY) {
    if(!filePath.exists())
        throw new WebDriverException("File not found: " + filePath.toString());

    WebDriver driver = ((RemoteWebElement)target).getWrappedDriver();
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    WebDriverWait wait = new WebDriverWait(driver, 30);

    String JS_DROP_FILE =
        "var target = arguments[0]," +
        "    offsetX = arguments[1]," +
        "    offsetY = arguments[2]," +
        "    document = target.ownerDocument || document," +
        "    window = document.defaultView || window;" +
        "" +
        "var input = document.createElement('INPUT');" +
        "input.type = 'file';" +
        "input.style.display = 'none';" +
        "input.onchange = function () {" +
        "  var rect = target.getBoundingClientRect()," +
        "      x = rect.left + (offsetX || (rect.width >> 1))," +
        "      y = rect.top + (offsetY || (rect.height >> 1))," +
        "      dataTransfer = { files: this.files };" +
        "" +
        "  ['dragenter', 'dragover', 'drop'].forEach(function (name) {" +
        "    var evt = document.createEvent('MouseEvent');" +
        "    evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null);" +
        "    evt.dataTransfer = dataTransfer;" +
        "    target.dispatchEvent(evt);" +
        "  });" +
        "" +
        "  setTimeout(function () { document.body.removeChild(input); }, 25);" +
        "};" +
        "document.body.appendChild(input);" +
        "return input;";

    WebElement input =  (WebElement)jse.executeScript(JS_DROP_FILE, target, offsetX, offsetY);
    input.sendKeys(filePath.getAbsoluteFile().toString());
    wait.until(ExpectedConditions.stalenessOf(input));
}



Source link https://sqa.stackexchange.com/questions/22191/is-it-possible-to---and-drop-from-a-file-in-system-to-a--in-s

LEAVE A REPLY

Please enter your comment!
Please enter your name here