通过webdriver ReactJS dropzone上传文件

时间:2017-09-25 13:05:47

标签: javascript java reactjs selenium

尝试使用selenium webDriver将图像上传到dropzone。已经为网站dropzonejsdotcom实现了它,但是如果我尝试使用脚本来执行reactJS dropzone(http://reactdropzone.azurewebsites.net/example/),我会收到一条消息:"未知错误:Dropzone未定义"。

JavascriptExecutor executor;

public ReactDrpzn() {
    Configuration.browser = "chrome";
    this.executor = (JavascriptExecutor) WebDriverRunner.getWebDriver();
}
private By dropzone = By.cssSelector(".filepicker.dropzone.dz-clickable");

@Test
public void drzon() throws IOException, InterruptedException {
    open("http://reactdropzone.azurewebsites.net/example");
    $(dropzone).hover();
    String script = "var myZone, blob, base64Image; myZone = Dropzone.forElement('.filepicker.dropzone.dz-clickable');" +
            "base64Image = 'iVBORw0KGgoAAAANSUhEUgAAAO0AAABQCAYAAAD1GfIkAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAEGSURBVHhe7dPBCcAwAAMxp/vv3ObRJQ4kMJ7gzrb3Doh4/gciRAsxooUY0UKMaCFGtBAjWogRLcSIFmJECzGihRjRQoxoIUa0ECNaiBEtxIgWYkQLMaKFGNFCjGghRrQQI1qIES3EiBZiRAsxooUY0UKMaCFGtBAjWogRLcSIFmJECzGihRjRQoxoIUa0ECNaiBEtxIgWYkQLMaKFGNFCjGghRrQQI1qIES3EiBZiRAsxooUY0UKMaCFGtBAjWogRLcSIFmJECzGihRjRQoxoIUa0ECNaiBEtxIgWYkQLMaKFGNFCjGghRrQQI1qIES3EiBZiRAsxooUY0UKMaCFGtBAjWkjZPk/PAZ8fwt/rAAAAAElFTkSuQmCC';" +
            "function base64toBlob(r,e,n){e=e||\"\",n=n||512;for(var t=atob(r),a=[],o=0;o<t.length;o+=n){for(var l=t.slice(o,o+n),h=new Array(l.length),b=0;b<l.length;b++)h[b]=l.charCodeAt(b);var v=new Uint8Array(h);a.push(v)}var c=new Blob(a,{type:e});return c}" +
            "blob = base64toBlob(base64Image, 'image / png');" +
            "blob.name = 'file.png';" +
            "myZone.addFile(blob);";

    executor.executeScript(script);
    Thread.sleep(3000);//смотрим результат
}

你可以从这里https://github.com/SaneQ/Drpzn

导入maven

2 个答案:

答案 0 :(得分:4)

基本上,有两个步骤:

  • 找到元素:
  • 发送包含您要上传的文件位置的密钥。

JavaScript的:

public class javaAttempt {

   public static void main(String args[]) {
      char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
      String helloString = new String(helloArray);  
      System.out.println( helloString );
   }
   javaAttempt.main();
}

Error: javaAttempt.java:8: error: <identifier> expected
   javaAttempt.main();

的Python:

const {Builder, By, Key, until} = require('selenium-webdriver');

let driver = new Builder()
    .forBrowser('chrome')
    .build();

driver.get('http://reactdropzone.azurewebsites.net/example/');
driver.findElement(By.xpath('.//input[@type="file"]')).sendKeys('full_path_to_your_file.png');

答案 1 :(得分:0)

所以,我发现在反应dropzone上传图像的简便方法。 React在DOM中创建hiden输入(Body&gt; input) 之后我只使用selenium sendKeys(“pathToImg”)