简体   繁体   中英

ReactJS dropzone upload file via webdriver

Try to upload image into dropzone with selenium webDriver. Already realize it for site dropzonejsdotcom, but if i try use script for reactJS dropzone ( http://reactdropzone.azurewebsites.net/example/ ) I get a message: "unknown error: Dropzone is not defined".

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);//смотрим результат
}

U can import maven from here https://github.com/SaneQ/Drpzn

Basically, two steps:

  • find the element:
  • send the key with the location of the file you want to upload.

JavaScript:

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');

Python:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://reactdropzone.azurewebsites.net/example/')
input_field = driver.find_element_by_xpath('.//input[@type="file"]')
input_field.send_keys('full_path_to_your_file.png')

So, I've found easy way to upload image in react dropzone. React creates hiden inputs in DOM (Body > input) And after that I just use selenium sendKeys("pathToImg")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM