1

i want to upload a file as an automated test, with the following code, it correctly selects the file & clicks the upload button, however nothing happens after the upload button is clicked, I believe its not triggering the javascript on the page:

var fileUpload = './testFile.txt',
absolutePath = path.resolve('./testFile.txt', fileUpload);
$('input[type="file"]').sendKeys(absolutePath);

element(by.model('documentFile'));

element(by.css('button[ng-click="uploadSelectedFiles()"]')).click();

Is there another method for this?

EDIT: I have managed to do this, thank you to who helped me, here is my code:

var fileUpload = './testFile.jpg',
            absolutePath = path.resolve('/home/xxx/workspace/xxx/xxx/xxx/xxxxx/test/x/x/x/x/filesToUpload', fileUpload);

        var uploadInput = $('input[type="file"]');
        uploadInput.sendKeys(absolutePath);
        uploadInput.submit();
        element(by.css('button[ng-click="uploadSelectedFiles()"]')).click();
2
  • could you provide html code? Commented Apr 7, 2015 at 10:21
  • HTML for chooseFile: <input ng-file-select="selectFiles($files)" class="peg-e2e-batch- upload ng-pristine ng-untouched ng-invalid ng-invalid-required" ng-model="documentFile" name="file" ng-accept="'.jpg, .pdf'" ng-model-rejected="rejFiles" required="" type="file"> html for uploadButton: <button class="btn btn-private-primary pull-left" ng- click="uploadSelectedFiles()">Submit</button> Commented Apr 8, 2015 at 11:30

2 Answers 2

2

Instead of clicking "upload" button, send keys to the input to set the path and submit the form by clicking Submit button:

var uploadInput = $('input[type="file"]');

uploadInput.sendKeys(absolutePath);

var submitButton = element(by.xpath("//input[.='Submit']"));
submitButton.click();
Sign up to request clarification or add additional context in comments.

8 Comments

it is still not uploading the file, selecting it correctly but not submitting form, i even tried putting in a browser.pause(); & submitting the file manually, still nothing happens.
so you are not able to perform such kind of action manually also? in this case it is problem in application itself
@Jordan.Muscat can you also edit the question and include the complete HTML code for the form and uploadSelectedFiles() function implementation.
How is that a correct answer? ng-click on the button won't be triggered if you just submit the form.
@meze question is, do u need to click the upload button to get stuck with the choose file dialog that u cannot control with selenium? The idea here is to set the path to the file via sendKeys and submit the form.
|
0

Appears to be a space between "ng-" and "click".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.