selenium webdriver:通过拖放上传文件

时间:2016-01-13 08:00:57

标签: selenium selenium-webdriver

在我试图测试的网页上,我们实现了拖放文件上传。我已经看过了selenium动作链API的拖放API。看起来它只支持在页面上的2个元素之间进行拖放。如何模拟从文件管理器拖动?

4 个答案:

答案 0 :(得分:2)

使用Selenium执行HTML5文件删除:

static final String JS_DROP_FILE =
  "var tgt=arguments[0],e=document.createElement('input');e.type='" +
  "file';e.addEventListener('change',function(event){var dataTrans" +
  "fer={dropEffect:'',effectAllowed:'all',files:e.files,items:{},t" +
  "ypes:[],setData:function(format,data){},getData:function(format" +
  "){}};var emit=function(event,target){var evt=document.createEve" +
  "nt('Event');evt.initEvent(event,true,false);evt.dataTransfer=da" +
  "taTransfer;target.dispatchEvent(evt);};emit('dragenter',tgt);em" +
  "it('dragover',tgt);emit('drop',tgt);document.body.removeChild(e" +
  ");},false);document.body.appendChild(e);return e;";

WebDriver driver = new FirefoxDriver();
driver.get("http://html5demos.com/file-api");

WebElement drop_area = driver.findElement(By.id("holder"));

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(JS_DROP_FILE, new Object[]{drop_area})
  .sendKeys("C:\\image.png");

答案 1 :(得分:0)

使用AWT Robot类执行拖放操作:

Robot robot=new Robot();
// drag
robot.mouseMove(x1, y1);
robot.mousePress(InputEvent.BUTTON1_MASK);
// drop
robot.mouseMove(x2, y2);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

答案 2 :(得分:0)

AutoIt有DLL包装器。我直接从C#/ Selenium代码中使用它。

答案 3 :(得分:0)

使用 jsExecutor 删除文件

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

     JavascriptExecutor jse = (JavascriptExecutor) getDriver();

     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, 0, 0);
      input.sendKeys(filePath.getAbsoluteFile().toString());
      waitFor(ExpectedConditions.stalenessOf(input));
    }