通过Dropzone以编程方式上传/添加文件,例如通过硒 [英] Programmatically upload / add file via Dropzone e.g. by Selenium

查看:110
本文介绍了通过Dropzone以编程方式上传/添加文件,例如通过硒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Selenium测试用例,其中的一个步骤是通过Dropzone.js上传文件。

I am writing a Selenium test case where one of the steps is to upload a file via Dropzone.js.

(由于Selenium可以在浏览器中运行Javascript,因此,如果也可以使用Javascript以编程方式完成,那也很好。)

(As Selenium can run Javascript in the browser, so if it can be done programmatically in Javascript that would be fine too.)

我想避免一路模拟打开文件浏览器窗口,选择文件等,这超出了Web驱动程序可以处理的范围,并且变得非常复杂。用伪代码,我想做这样的事情:

I want to avoid going all the way to simulate opening the file browser window, selecting file etc, as that goes outside of what the web driver can handle and gets very complicated. In pseudo code, I'd like to do something like this:


1.选择一些Dropzone元素
2 。设置文件路径
3.提交(上传文件)

现有问题中提到了一种可能的方法(< a href = https://stackoverflow.com/questions/30201746/unable-to-upload-file-using-python-selenium-webdriver-on-http-www-dropzonejs-c>无法使用python硒上传文件http://www.dropzonejs.com 上的webdriver),该驱动程序使用 dz-hidden-input元素(DOM文件输入)。

There is one likely approach mentioned in an existing question (Unable to upload file using python selenium webdriver on http://www.dropzonejs.com), which uses the "dz-hidden-input" element (a DOM file input).

不幸的是,它不起作用(至少在当前版本的Dropzone中不起作用)-将文件设置为元素后,Dropzone .files仍然为空,并且没有上传。

Unfortunately it does not work (at least not in the current version of Dropzone) - after setting the file to the element, the Dropzone .files is still empty and no upload takes place.

在查看Dropzone的源代码之后,我通过扩展上述内容提出了一个可行的解决方案:

After looking in the Dropzone source, I came up with a working solution by extending the above:


1.设置 dz-hidden-input元素
中的文件路径。2.使用javascript从e检索File对象。 lement
3.将文件传递给dropzone.addFile(file)

但是我担心的是hack,因为没有记录下hidden-input和.addFile,并且如果Dropzone更改实现等,将来测试会中断。

But my concern is it is really a hack, as both the hidden-input and .addFile are not documented, and the test will break in the future if Dropzone changes implementation etc.

有没有更好的/记录的执行此操作的方法?

Is there any better / documented way to do this?

(为澄清-我正在尝试上传 new 文件,而不是显示如Dropzone常见问题解答)

(To clarify - I am trying to upload a new file, not to show an existing file as mentioned in the Dropzone FAQ)

推荐答案

单击输入按钮->使用Web驱动程序剪贴板/ java机械手->粘贴/键入文件位置+

Click to Input button -> Use web driver clipboard/java robot -> Paste/type file location + file name > Hit robot enter.

final String fileName = "textfile.txt";
final String filePath = "\\data\\public\\other\\" + fileName;
zUploadFile (filePath );

public void zUploadFile (String filePath) throws HarnessException {

    // Put path to your image in a clipboard
    StringSelection ss = new StringSelection(filePath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    // OR use java robot for entire filepath
    Thread.sleep(10000);

    // Imitate mouse events like ENTER, CTRL+C, CTRL+V
    Robot robot;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);

    } catch (AWTException e) {
        e.printStackTrace();
    }
}

这篇关于通过Dropzone以编程方式上传/添加文件,例如通过硒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆