Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate the bootstrap fileupload upload control using webdriver

I want to automate the file uploading process which is using a file upload control built in bootstrap. I am doing the same using webdriver. Below is my code, but unfortunately it is not working:

element=driver.findElement(By.xpath("//[@id='upload']/fieldset/div[2]/input[1]"));
element.sendKeys(pathToFile);

It is giving an element not visible error.

Here is the example of the bootstrap fileupload control which I am trying to automate- Via JavaScript: on this URL http://markusslima.github.io/bootstrap-filestyle/ Please see below style-

$(":file").filestyle({icon: false});
like image 896
Nikunj Aggarwal Avatar asked Nov 01 '22 07:11

Nikunj Aggarwal


1 Answers

Ok. i think i solved it.

    WebElement fileInput = driver.findElement(By.id("document"));

    JavascriptExecutor js = (JavascriptExecutor) driver;
    WebElement element = driver.findElement(By.id("document"));
    js.executeScript("arguments[0].setAttribute('style', 'left:30px')",
            element);

    fileInput.sendKeys(fileName);

the bootstrap-filestyle.js hide a input element so you have to move it to the visible area and then set it in the standard way.

so much trouble for such a simple solution.

Here is my original html code:

<span id="documentUpload">
    <input type="file" id="document" name="document" class="notMandatory" onkeypress="return noenter(event)" tabindex="-1" style="position: absolute; left: -9999px;">
    <div class="bootstrap-filestyle" style="display: inline;" tabindex="0">
        <input type="text" class="input-xlarge" disabled="" autocomplete="off"> 
        <label for="document" class="btn"><i class="icon-folder-open"></i> <span>Upload</span></label>
    </div>
</span>
like image 111
Gico Avatar answered Nov 08 '22 08:11

Gico