As you can see in the title, I am trying to upload a file using protractor.
In my project, I have a webpage where I can drag & drop a file, using resumable.js assigned to a div representing the drop area.
The fact is, when doing e2e testing, I obviously can't drag & drop a file, so I chose to upload it using sendKeys() with the absolute path of my file path.resolve('./tests', 'test.csv').
Until here, everything works (path is good and I can find my element with element(by.id(...)) but when I use sendKeys() it throws an error saying "unknown error: cannot focus element".
I tried to upload on an <input type="file"></input>, it works, but since it's on what I use I can't upload this on my original div.
I think I have to specify that this is a file, I saw the setFileDetector(detector) function but I am not sure this is the right way to proceed, and it doesn't seem to work with it anyway.
Here is the HTML drag & drop :
<p translate>Drag & Drop your file below</p>
<div class="drop" ng-class="dropClass" id="import-drop-area" data-ng-init="init()" translate> </div>
And here is the test that works on an but not on my div :
it('Should begin the import wizard by dropping a file', function(done) {
// Begin the wizard
var nextButton = element(by.id('next'));
nextButton.click();
// Upload a file
var uploadArea = element(by.id('import-drop-area'));
var file = path.resolve('./tests/import', 'test.csv');
uploadArea.sendKeys(file);
setTimeout(done, 1000);
});
I hope someone could help me, thank you by advance !
Best regards.
Related
2020-01-29 20:32:22.470194+0300 Myapp[8905:2391245]
-canOpenURL: failed for URL: "file:///private/var/containers/Bundle/Application/146EA027-7A**/Myapp.app/assets/src/assets/policy.html" - error: "This app is not allowed to query for scheme file"
I am getting this error on xCode console output on real device. On simulator, everything works fine.
Here is my simple full code:
import React, { Component } from 'react';
import { WebView } from 'react-native-webview';
const PolicyHTML = require('../assets/policy.html');
export default class PolicyScreen extends React.Component {
render() {
return (
<WebView
source={PolicyHTML}
style={{flex: 1}}
/>
);
}
}
Couldn't find much solution about that online, what am i missing ?
The solution for me was just add originWhitelist={['*']} in the <WebView> component and then iOS would load HTML correctly.
I was having the same issue, i am using webview and wanted to load my local html file in that webview, which works perfectly fine in Android but was not in IOS device. After a lot of research i ended up with the following solution.
I have placed my html file in the following path:
MyReactNativeProjectFolder>app>views>monthly>trip.html
Where monthly is my custom folder that i created myself and has a local html file called trip.html.
And in the view, lets say MyView.js, where i want to call my html file i used the following syntax:
<WebView originWhitelist={['*']} source={require('./monthly/trip.html')} ref={( webView1 ) => this.webView1 = webView1} />
MyView.js is in the follwing path:
MyReactNativeProjectFolder>app>views>MyView.js
If you are not getting any error on simulator then this shall fix your problem, otherwise try changing the html file path as I have mentioned above, that is in the views folder, and try again.
I hope this may resolve the error Unable to open URL file:///private/var/containers/Bundle/Application/146EA027-7A/Myapp.app/assets/src/assets/policy.html
I don't know is this can be considered as proper solution but here how i solved it;
I am not sure but as my thinking Xcode is not allowing some codes on html files, so I thought about that found some websites on google which converts to html files to 'clean html file' and removes the unnecessary codes. After cleaning I replaced the new clean file with old one and it worked.
Hope it helps (Especially in Privacy Policy Files).
I want to upload an image file in protractor. the problem is i can not get "input[type = "file"]" element without clicking on upload button.
when I click upload button, file upload dialogue popup opens.
i have tried
browser.actions().sendKeys(protractor.Key.ESCAPE).perform(); but it does not work.
This is what I am doing:
var image = '../images/image1.png';
var absPathImg = path.resolve(__dirname, image);
element(by.id('uploadImage')).click();
browser.actions().sendKeys(protractor.Key.ESCAPE).perform();
element(by.css('input[type=file]')).sendKeys(absPathImg);
element(by.id('upload')).click();
How can i close that file upload dialogue to upload an image?
Try it like this. I used this for Protractor, but I think it's similar for Selenium.
I also have a problem for Windows File dialog not closing.
This is how I solved that:
element(by.css('elementForOpenDialog')).sendKeys(absPathImg);
Send Keys to the element which will open file dialog, not click on element to open file dialog. Just send to that element absolute path and it will upload without opening dialog.
It's worked for me. Good Luck!
This is an old thread but I'll answer in case anyone else runs to this problem.
I had to solve this same problem today. I was able to work around opening the upload dialog, because I am quite sure it cannot be closed without some external libraries and that would not work in headless mode.
You need to inspect the javascript linked to the upload button's click event and execute some script before sending keys to the file input.
This is individual to each case, but in my case i had to do this (Python):
script = (
"var url = '/Contacting/UploadCsv'; "
"$('#buttonFileUpload').data('url', url);"
)
driver.execute_script(script)
driver.find_element_by_id('buttonFileUpload').send_keys(csv_path)
I picked that script from the JS function linked to the button.
I landed here because I was searching on HOW TO CLOSE AN IMAGE WHEN CLICKED
In case that helps someone.
So what I did.
var Img = Driver.FindElement(XPath.News.YourImg);
Img.Click();
try
{
Img.Click();
Assert.IsFalse(feedImg.Displayed);
}catch(Exception ex)
{
}
This way you can check if the image is closed by trying to click on it again.
Instead of clicking on to ESCAPE, why not simulate the ENTER key press to simulate a person entering the path and pressing the ENTER key to upload the image.
I am doing this in my code and this works perfectly fine.
var path = require('path')
this.fileUploadOne = element(by.css('locator_to_element'));
var fileUpload = 'path_of_file_to_upload'
absolutePath = path.resolve(__dirname,fileUpload);
console.log(absolutePath);
this.fileUploadOne.sendKeys(absolutePath);
browser.actions().sendKeys(protractor.Key.ENTER).perform();
//just for debugging purposes.
browser.sleep(20000);
I'm using version 12.01 of the excellent ng-file-upload and attempting to create an e2e test with protractor using Chrome 49. I'm using sendKeys to path the file path, and I'm able to see the upload being triggered, but the file size in the
<button
class="avatar-upload__button"
ngf-select="uploadFiles($file, $invalidFiles)"
ng-model='ngfFile'
accept="image/*"
ngf-max-size="14.5MB">
</button>
I'm able to see the file upload being called in this function:
$scope.uploadFiles = (file, errFiles) => {
}
But at this point file.size is 0, where when I run this manually, i get the correct size. I wouldn't be too fussed about this except that the upload seems to be getting choked off/is not happening (presumably because the file information is bad).
My guess at this point is it is related to using this on a button rather than an input[type="file"]. I'm going to start digging into ng-file-upload, but any suggestions/ideas appreciated.
I knew something smelled bad here because nobody else was having this problem. I assembled the path incorrectly and the file did not exist.
I download fine uploader 3.5, created http handler for a file upload function in my little website . the website is done by asp.net ajax and jquery. it runs at IE9. every time I upload a file, there is a list of file names shown below the load button. if I don't want thme, what should I do?
my code is like this:
html: ...
'<tr><td><div id="jquery-wrapped-fine-uploader"></div></td></tr>...
'ajax/jquery:...
'$('#jquery-wrapped-fine-uploader').fineUploader({
'request: { endpoint: 'xxx.ashx' }
'})
'$('#jquery-wrapped-fine-uploader').on("complete",
'function (event, id, fileName, responseJSON) {
' alert("UPLOAD SUCCESS");
' $.ajax({some ajax calls here});
' })
// WHERE TO PUT this TO CLEAR the UPLOADED FILE LIST??? $('#jquery-wrapped-fine-uploader').fineUploader('reset');
XXX.ashx:
'... public void ProcessRequest (HttpContext context) {
'do some http request work..
'context.Response.ContentType = "text/plain";
'context.Response.Write("{\"success\":true}");
'}
My question is:
I want to completely remove the uploaded file list which shows automatically in green color ( or red if they fail), in order to clear them, I tried to put: $('#jquery-wrapped-fine-uploader').fineUploader('reset'); right after .on('complete'), it's not working, also #jquery-wrapped-fine-uploader seems cached all the time. please help on this.
If you don't want to see the file list at all, you should be using FineUploaderBasic mode instead of using FineUploader mode and then removing all elements in the pre-built UI. FineUploaderBasic mode gives you access to the API, options, and callbacks, but assumes you will be creating your own UI. In other words, FineUploaderBasic mode does not create any DOM elements (except the opaque file input element as a child of your button container, if you supply one). This is all explained (in a great amount of detail) in the documentation.
Start here: http://docs.fineuploader.com
I have a cucumber step where the file gets downloaded, but i am unable to click the save button in the dialog box that a browser provides, with cucumber step.
I have found some pages which solves the similar kind of problems, but it didn't solve mine
How to test a confirm dialog with Cucumber?
I have included this cucumber step for file download
When /^I confirm a js popup on the next step$/ do
page.evaluate_script("window.alert = function(msg) { return true; }")
page.evaluate_script("window.confirm = function(msg) { return true; }")
end
But didn't work.
Thanks
Did you try the page.driver syntax?
i.e., from: How to test a confirm dialog with Cuccumber?:
When /^I confirm popup$/ do
page.driver.browser.switch_to.alert.accept
end
When /^I dismiss popup$/ do
page.driver.browser.switch_to.alert.dismiss
end
ian.
I know this is old, but since this was one of the first SO posts I came across when searching for solutions, I figured I'd post a solution.
We can use good ol Ruby, along with open-uri (so include open-uri in your Gemfile if you aren't already using it):
Then /^I receive a PDF$/ do
link_url = find_link("Report")[:href]
file = open(link_url)
file.content_type.should == 'application/pdf'
end