0

I am running my selenium scripts from Jenkins, which when executed runs on a machine specifically dedicated to Selenium Tests. But I have a problem. There are some actions as stated below, which I am performing using Robot class:

1) Click on image upload icon. 2) The window (file upload) pertaining to Ubuntu OS opens. 3) I have to past the location of the image. 4) And click on open.

All this works fine on a local machine. But fails on a remote machine because of the limitation of Robot class.

Can anyone please help me to overcome this?

I have attached a snapshot for better clarity.

enter image description here

<div class="dropify-wrapper">
    <div class="dropify-message">
        <span class="file-icon"/>
        <p>Drag and drop a file here or click</p>
        <p class="dropify-error">Sorry, this file is too large</p>
    </div>
    <input id="category_tile_upload" class="dropify" data-default-file="" type="file"/>
    <button class="dropify-clear" type="button">Remove</button>
    <div class="dropify-preview">
        <span class="dropify-render"/>
        <div class="dropify-infos">
            <div class="dropify-infos-inner">
                <p class="dropify-filename">
                    <span class="file-icon"/>
                    <span class="dropify-filename-inner"/>
                </p>
                <p class="dropify-infos-message">Drag and drop or click to replace</p>
            </div>
        </div>
    </div>
</div>
Andersson
  • 51,635
  • 17
  • 77
  • 129
Prakash P
  • 419
  • 1
  • 8
  • 18
  • If you need to upload file to web-app UI you might need to use `driver.find_element_by_xpath("//input[@type='file']").send_keys(path_to_file)` ( `Python` example) instead of `Robot` class... – Andersson Nov 22 '16 at 11:07
  • @Andersson I don't have a text box where I can send keys. It's not a browser element so I am not able to identify. – Prakash P Nov 22 '16 at 11:21
  • You're trying to click on button (icon). This element (e.g. `
    `), I supposed, has embedded element `` which actually should get the value from File Upload dialog box... Show an `HTML` for your upload button
    – Andersson Nov 22 '16 at 11:25
  • @Andersson Yes there is. But now the problem is that I am executing the tests using grid. As it is getting the executed on a remote machine the local path will be invalid. Is there anyway this can be handled? Like can I use getResourceAsStream? – Prakash P Nov 22 '16 at 11:45
  • Where do your test files that `Robot` uses located? You could set relative path to file – Andersson Nov 22 '16 at 12:55
  • It's under "/src/test/resources" folder. The code execution starts on Jenkins server where the latest code is pulled. Then it is executed on the node which is a remote machine. – Prakash P Nov 23 '16 at 05:29
  • I tried your option. It does append the path to that place holder. But when I click on "Save" in the application (Which is running on the node machine), it does not complete the operation and says "File not found". Please help. – Prakash P Nov 23 '16 at 07:11
  • Does your application uses drag'n'drop upload zone (like http://ajaxuploader.com/images/drag-drop-file.gif) or file input field (like http://www.jqueryscript.net/images/Pretty-Input-Type=File-Replacement-with-jQuery-FileInput.jpg)? Also specify your programming language and provide `HTML` code for input div – Andersson Nov 23 '16 at 08:27
  • Yes it does support drag and drop. I am coding using Java. – Prakash P Nov 23 '16 at 08:29
  • Add `HTML` code for `
    ` that contains upload area to your question
    – Andersson Nov 23 '16 at 08:40
  • @Andersson Done. Does that suffice? – Prakash P Nov 23 '16 at 09:03

2 Answers2

0

This shouldn't be done with Robot class, since it can be solved using Selenium. Don't click the element that opens the upload window and instead use:

If you have an ID to your input better use it with By.id("inputID"), else:

WebElement inputElement = driver.findElement(By.cssSelector("input[type='file']"));
element.sendKeys("/full/path/to/your/file");

And get the path, either with:

String pathToFile = System.getProperty("user.dir") + "/src/resources/your.file; 

To use getResourceAsStream() you can:

try (InputStream in = this.getClass().getResourceAsStream(fileName)) {
    return getStringFromInputStream(in);
} catch (IOException e) {
    // handle
}

private String getStringFromInputStream(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        br = new BufferedReader(new InputStreamReader(is));

        while ((line = br.readLine()) != null) {
           sb.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();
}
Moshisho
  • 2,781
  • 1
  • 23
  • 39
  • Thank you. But I have a doubt. My code execution begins on a jenkins server. The latest code is pulled out and hub is started. I have nodes continuously running on remote machines. The moment hub is started, nodes connect and the test case execution begins. My file are under "/src/test/resources" folder on the jenkins server. Now the browser is opened on the machine where the node is running. So how will this work. Will all the resources be synchronised to all the nodes? – Prakash P Nov 23 '16 at 05:42
  • The reason I am telling is because I tried this option. On the node machine where the browser is opened it says "File not found". – Prakash P Nov 23 '16 at 07:10
  • Your doubt is correct, if I'm not mistaken. You'll need to pre setup the file on your slave, or copy it from the server to the slave before uploading... See here: http://stackoverflow.com/questions/3812673/copy-file-from-one-server-to-another – Moshisho Nov 23 '16 at 07:18
  • I will check this. – Prakash P Nov 23 '16 at 09:03
0

Common approach to resolve your issue is to use

WebElement file_input = driver.findElement(By.id("category_tile_upload"));
file_input.sendKeys("/path/to/file/to/upload");

What is result of implementing these lines of code?

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • As I mentioned this works on the if both hub and node is running on the same machine. But if hub & node are different machines, then it is not. For me Jenkins is on "Server A". The latest code checks out on this machine and instantiates a hub. Then the node which is running on Server B connects to the hub on Server A and then test cases execution starts. In the solution given it is picking the path of the file in Server A and passing that to the browser executing on B. Upon Save it fails as the file is not on Server B. – Prakash P Nov 23 '16 at 09:19
  • I'm little confused with described scheme:) `Jenkins` should use your code that is located somewhere on VCS (e.g. `git` repo), but tests actually starts on remote machine, right? – Andersson Nov 23 '16 at 09:29
  • I am sorry. I will be more clear. I have configured Jenkins to check out the latest code from 'git' and run the same. In the selenium project I have start-up script for starting the 'hub'. My Jenkins server is what I am calling 'Server A'. Now on 'Server B' my node is running and this is where the tests are executed. Now 'File A' is in resource folder of the project and project is checked out in 'Server A'. When I extract the path of the file it comes out as '/SERVERA/somepath/fileA.jpg'. – Prakash P Nov 23 '16 at 09:52
  • This path gets appended to that tag through send_keys. But the browser is opened in SERVER B as the node runs their and this path is invalid on that machine. Hence the Save is failing. – Prakash P Nov 23 '16 at 09:53
  • 1
    Could this help http://stackoverflow.com/questions/16205299/create-and-upload-a-file-on-selenium-grid ? – Andersson Nov 23 '16 at 10:05
  • Actually I was looking at the same. But I have a problem. I am using evenFiringWebDriver which does not contain setFileDetector method. – Prakash P Nov 23 '16 at 11:54
  • 1
    I have not found a viable solution using EventFiringWebDriver class. But with remoteWebDriver it works. – Prakash P Nov 24 '16 at 07:02