将带有selenium ide的文件上传到Dropzone.js

时间:2016-01-13 14:42:34

标签: selenium-ide dropzone.js

我正在创建selenium ide测试,现在我遇到了问题。 我无法编写从本地磁盘上传文件的测试。

我的dropzone看起来像: http://www.dropzonejs.com/examples/simple.html

有人可以帮助我吗?

4 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,我在这里找到答案: How to interact with Dropzone using selenium

我使用了大部分内容,但我必须创建自己的方法才能正确转换为base64。

public static String convertFileToBase64String(String fileName) throws IOException {

        File file = new File(fileName);
        int length = (int) file.length();
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
        byte[] bytes = new byte[length];
        reader.read(bytes, 0, length);
        reader.close();
        String encodedFile = Base64.getEncoder().encodeToString(bytes);

        return encodedFile;
    }

希望这有帮助!

答案 1 :(得分:0)

您可以使用上面回答中的convertFileToBase64String方法使用运行JS脚本的Java代码。 您需要提供4个参数:

  1. Dropzone元素的ID(例如“ VUIDropzone96326-dropzone”)
  2. 您要上传的文件的新名称(有时服务器希望使用特定名称)
  3. 您要上传的文件的路径
  4. 您从convertFileToBase64String方法获得的
  5. base64IFile字符串

这是完整的解决方案:

    String id = "Put here the ID of the Dropzone element"
    String fileName = "Put here desired file name";
    String base64IFile = convertFileToBase64String(filePath);
    ((JavascriptExecutor) driver).executeScript("var myZone = Dropzone.forElement('#" + id + "');" +
            "base64Image = '" + base64IFile + "';" +
            "function base64toBlob(b64Data, contentType, sliceSize) {  \n" +
            "    contentType = contentType || '';\n" +
            "    sliceSize = sliceSize || 512;\n" +
            "\n" +
            "    var byteCharacters = atob(b64Data);\n" +
            "    var byteArrays = [];\n" +
            "\n" +
            "    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {\n" +
            "        var slice = byteCharacters.slice(offset, offset + sliceSize);\n" +
            "\n" +
            "        var byteNumbers = new Array(slice.length);\n" +
            "        for (var i = 0; i < slice.length; i++) {\n" +
            "            byteNumbers[i] = slice.charCodeAt(i);\n" +
            "        }\n" +
            "\n" +
            "        var byteArray = new Uint8Array(byteNumbers);\n" +
            "\n" +
            "        byteArrays.push(byteArray);\n" +
            "    }\n" +
            "\n" +
            "    var blob = new Blob(byteArrays, {type: contentType});\n" +
            "    return blob;\n" +
            "}" +
            "var blob = base64toBlob(base64Image, 'image / png');" +
            "blob.name = '" + fileName + "';" +
            "myZone.addFile(blob);  "
    );

答案 2 :(得分:0)

通常,会有隐藏表单输入

<input type="file" multiple="multiple" class="dz-hidden-input" accept="image/jpg,image/jpeg style="visibility: hidden>

因此您可以简单地与该隐藏元素进行通信

答案 3 :(得分:0)

如果您无法使用“sendKeys()”方法上传文件,可能您找不到类型为“file”或...的输入,我认为最简单的方法是使用“java .awt”库:

  1. 点击网页元素以显示上传对话框,在本例中为拖放区 div。
  2. 通过单击并释放“ctrl + V”来粘贴文件路径。
  3. 按“Enter”键。
    public static void main( String[] args ) throws Exception
        {
            System.setProperty("webdriver.gecko.driver", "/home/bril/IdeaProjects/automation/geckodriver");
            WebDriver driver = new FirefoxDriver();
            driver.navigate().to("https://www.dropzonejs.com/");
            WebElement dropZone = driver.findElement(By.id("dropzone"));
            Robot rb = new Robot();
            // copying File path to Clipboard
            StringSelection str = new StringSelection("/home/bril/Downloads/img.jpg");
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(str, null);
            dropZone.click(); // Click on browse option on the webpage
            Thread.sleep(3000); // suspending e xecution for specified time period
            // press Contol+V for pasting
            rb.keyPress(KeyEvent.VK_CONTROL);
            rb.keyPress(KeyEvent.VK_V);
            // release Contol+V for pasting
            rb.keyRelease(KeyEvent.VK_CONTROL);
            rb.keyRelease(KeyEvent.VK_V);
            Thread.sleep(3000); // suspending execution for specified time period
            // for pressing and releasing Enter
            rb.keyPress(KeyEvent.VK_ENTER);
            rb.keyRelease(KeyEvent.VK_ENTER);
            System.out.println( "Finish" );
        }