File upload with Selenium WebDriver and FF24 - java

I am facing an issue with the file upload with WebDriver, using Java, on Firefox 24.
And I can NOT use some external program like AutoIT or similar.
I have to upload a file to a section which's HTML code is:
<td>
<input type="file" name="file">
</td>
And what I was using in Eclipse is
pageObject.getTypeFileLocation().sendKeys(textFile);
pageObject.getUploadButton.click();
which does not work; also tried the first answer of this question but neither did the trick.
The test returns 'OK', but I suspected that nothing was being done so added a check to wait for 'Upload complete' text present; But the file is not uploaded, and timeouts after 20 seconds, even when the file to upload is 5KB and takes less than a second when manually uploaded.
The input=file section contains a button and a 'No File Selected' text that changes to the filepath when a file is selected manually; and this is in what I am basing my idea that the file is not being upload; because the 'No File Selected' remains until the test fails.
I tried this on Chrome and seems to be working fine, and I know that there have been some reworks about input=file in FF since release 23, but mostly pointed to CSS styling, so I don't think it's related.
Also, a question that might sound kind of stupid, but questions are questions: Doesn't the sendKeys() action need a field to input those keys? I feel that the WebDriver is trying to write the path over a button, which can't perform the action as it's only a button.
Any help will be appreciated, and thanks in advance!

Solved it!
Don't know if it works for all browsers, but at least it does on FF and Chrome:
Found out that somebody used the FILE type, so reused it and got its absolute path:
protected File *fileName* = new File("*path to file*");
private String textFile = *fileName*.getAbsolutePath();
pageObject.getInput().sendKeys(textFile);
Hope somebody else finds it useful; as it's weird to be answering my own question.

Related

Selenium Java clear method will not clear the input field?

I ran into a little issue today while validating some input fields in our application.
Looks like we can't call the selenium java method clear() to clear the input and I know I can iterate over the string and use sendKeys(Keys.Backspace) but clear method should do the work?
Anyone else ran into the same issue and how did you work around this problem.?
Our app is built upon Ant libraries
Selenium
String hotmarketNameEdit = "ant-input";
WebElement edit = driver.findElement(By.className(hotmarketNameEdit));
edit.click();
edit.clear();
HTML
<input type="text" id="label" class="ant-input ant-input-sm" value="AUTOMATION TEST - Thu Oct 21 09:54:15 MDT 2021" xpath="1">
Thanks for any inputs
I've personally come across this issue quite a few times, that Selenium .clear() method does not work properly.
Selenium community is aware of this issue, please see the bug reported here - ClearText does not work
You can go through the above-mentioned link.
Workaround :
Majority of time, I try to simulate Ctrl + A to select the text and just press delete after that.
If You are using Java as a binding language, You can do the following
webElement.sendKeys(Keys.CONTROL + "a")
webElement.sendKeys(Keys.DELETE)
have you tried JavaScriptExecutor?
JavascriptExecutor jse = (JavascriptExecutor) indexWebDriver;
jse.executeScript("arguments[0].value=''",edit);
check this one also
How can I consistently remove the default text from an input element with Selenium?
Can you please try this work around, passing both selecting and deleting keywords in one command
driver.findElement(By.name("username")).sendKeys(Keys.chord(Keys.CONTROL,"a", Keys.DELETE));

response.setHeader error on google chrome

This is a project on JSP.
1)I have this code where a button is pressed and the action is rendered to another page,which is a JSP page containing a pdf and the user is able to save it.
I had tried my code a month back and it worked fine,now when I try it,it doesnt work on chrome,works on mozilla though.
this is the line of code
response.setHeader("Content-Disposition", "inline; filename=Question.pdf");
2)My second problem is that,I have a reset button,this button resets the database.this function works fine on chrome but not on mozilla.I have no idea why it doesnt,there is no action taking place at all.
this is the line of code for that
<button type="button" onclick="reset.jsp">RESET</button>
For question 1):
What I know from the difference between "content-disposition", "attachment;filename=somefile.ext" and "content-disposition", "inline;filename=somefile.ext" is that in the inline case the browser will try to render it alone and if it can't it will prompt the user to download it. Maybe in your case you had an extension of Chrome that was capable of showing PDF files in the browser and now not. See Content-Disposition:What are the differences between "inline" and "attachment"?
As for question 2) :
This code onclick="reset.jsp" is not a valid construction. Onclick has to be a valid JavaScript function. See http://www.w3schools.com/jsref/event_onclick.asp. And it you case it not necessary to have it at all as you have an link inside the button which performs the actual action. I guess Chrome ignores this error and MOzilla not. Try removing onclick="reset.jsp" from the button.

Selenium WebDriver: Java: Mac OS X: how control file upload automation for CuteWebUI_Uploader_Resource from CuteWebUI for ASP.NET

Selenium-2.37.0
Firefox 24.0 (although have also tried on Chrome)
Mac OS X Mountain Lion 10.8.5
Solutions involving Windows, Windows-specific automation tools/libraries or O/S other than Mac OS X are not acceptable answers
Firstly may I remark that very similar questions have be asked often here on Stackoverflow, I have examined every offered answer, and none of them work for the CuteWebUI_Uploader_Resource AJAX file uploader, as I will illustrate in detail below.
I have an Enterprise Java web application with data and files and wish to automate mapping and uploading a selected subset of the data and files from that web application against (into) a separate ASP.NET web application, which uses the CuteWebUI toolkit and the CuteWebUI_Uploader_Resource from CuteSoft.
At the time of writing the http://cutesoft.net server has been down for some days; Googling gives some CuteSoft forum hits that may help with this problem, but I currently can't access them.
Like many file uploaders, the CuteWebUI AJAX file uploader has a Browse button and an Upload button.
I want to be able to upload directly into the CuteWebUI AJAX file uploader without using that Browse button (and without having to play any tricks using the Browse dialog on Mac OS X, because the files I wish to upload will not be available to a browser on a local filesystem, but instead are stored on a server accessible to the Java web application via Java, although for testing I can have them on a local filesystem, as shown below).
I appreciate that this problem has been answered many times for other file uploaders (see exhaustive list at end of this posting), and the process usually described as a solution for most file uploaders is roughly:
Identify the INPUT field for the file upload path (sometimes hidden) populated by the Browse button. Sometimes one needs to unhide this using a JavascriptExecutor and ensure it is a text field, then use sendKeys to populate it.
Identify the matching SUBMIT Upload button and click() it.
This does not work for the CuteWebUI AJAX file uploader.
The system I am targeting does indeed have an INPUT element that by name would suggest it is used to store the file upload path, but in fact when I try it by hand (and examine it using Firebug and Selenium IDE) its value is not populated at all, and instead a completely new temporary table of uploadable files appears.
The following HTML shows the situation before any file browse or upload has been attempted. I have omitted some style markup and some irrelevant values:
<script src="/CuteWebUI_Uploader_Resource.axd?type=script&_ver=" type="text/javascript"></script>
<input
type="hidden"
autocomplete="off"
name="fileuploader_433"
id="fileuploader_433"
isuploaderfield="1"
value="">
<button>Browse</button>
<span style="display: none;"></span>
<button style="display: none;">Cancel upload</button>
<img
showprogressbar="1"
canceluploadmsg="Cancel upload"
resourcehandler="/CuteWebUI_Uploader_Resource.axd"
uploadtype="Auto"
cancelallmsg="Cancel all Uploads"
uploadurl="/Handlers/UploadHandler.ashx"
insertext="Upload a file"
uploadingmsg="Uploading.." filetoolargemsg=".." maxfileslimitmsg=".."
inserttext="Browse"
numfilesshowcancelall="2147483647"
barstyle="Continuous"
showprogressinfo="0"
multiplefilesupload="0"
windowsdialoglimitmsg=".."
manualstartupload="1"
extensions=""
contextvalue=".."
onerror="this.onload()"
onload="this.style.display="none" ;
if(!window.CuteWebUI_AjaxUploader_Initialize){
var xh=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHttp');
xh.open('GET','/CuteWebUI_Uploader_Resource.axd?type=script&amp;_ver=',false);xh.send('');eval(xh.responseText)}CuteWebUI_AjaxUploader_Initialize(this.id);"
src="/CuteWebUI_Uploader_Resource.axd?type=file&file=continuous.gif"
pageupload="1"
namespace="CuteWebUI"
uniqueid="fileuploader_433"
id="fileuploader_433_Loader_unique"
style="display: none;">
<button
onclick="return submitbutton_click('433')"
class="submitbutton"
name="SubmitButton"
id="SubmitButton">Upload</button>
The following Selenium in Java correctly unhides and populates the fileuploader_433 INPUT field, but on executing the click() on the submit I still get a popup window asserting I have to use the browse button with the message "Please use browse for file upload":
String name_fileuploader = "fileuploader_434";
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementsByName('" + name_fileuploader + "')[0].setAttribute('type', 'text');");
WebElement element_uploader = driver.findElement(By.xpath("//input[#name='" + name_fileuploader + "']"));
element_uploader.clear();
String filepath="/path/to/file.pdf";
element_uploader.sendKeys(filepath);
driver.findElement(By.xpath("(//button[#id='SubmitButton'])[2]")).click();
Firebug confirms that even if you do it by hand, that INPUT field is never used ! Instead, a new file queue table appears:
<script src="/CuteWebUI_Uploader_Resource.axd?type=script&_ver=" type="text/javascript"></script>
<input type="hidden" autocomplete="off" name="fileuploader_434" id="fileuploader_434" isuploaderfield="1" value="">
<button>Browse</button>
<table .. class="AjaxUploaderQueueTable">
<tbody>
<tr class="AjaxUploaderQueueTableRow">
<td>
<img src="/CuteWebUI_Uploader_Resource.axd?type=file&file=circle.png&_ver=null" title="">
</td>
<td>file.pdf</td><td>
<img src="/CuteWebUI_Uploader_Resource.axd?type=file&file=stop.png&_ver=null" title="Remove" style="cursor: pointer;">
</td>
</tr>
</tbody>
</table>
<span style="display: none;"></span>
<button style="display: none;">Cancel upload</button>
<img showprogressbar="1" canceluploadmsg="Cancel upload" ..
resourcehandler="/CuteWebUI_Uploader_Resource.axd" uploadtype="Auto"
cancelallmsg="Cancel all Uploads" uploadurl="/Handlers/UploadHandler.ashx" [snip]
onerror="this.onload()" onload="this.style.display="none" ;
if(!window.CuteWebUI_AjaxUploader_Initialize){var xh=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHttp');xh.open('GET','/CuteWebUI_Uploader_Resource.axd?type=script&amp;_ver=',false);xh.send('');eval(xh.responseText)}CuteWebUI_AjaxUploader_Initialize(this.id);"
src="/CuteWebUI_Uploader_Resource.axd?type=file&file=continuous.gif"
pageupload="1" namespace="CuteWebUI" uniqueid="fileuploader_434"
id="fileuploader_434_Loader_unique" style="display: none;">
<button onclick="return submitbutton_click('434')" class="submitbutton" name="SubmitButton" id="SubmitButton">Upload</button>
Please note that:
the hidden INPUT element is never populated with a file path value !
instead, the string file.pdf without the full file path appears in a new TABLE with class="AjaxUploaderQueueTable" below the Browse button.
That inserted AjaxUploaderQueueTable is clearly only cosmetic; it seems that the filepath selected during the Browse operation is stored server-side. I have searched for the full filepath after selecting with the Browse button using Firebug and it is not present in the page.
I am open - for the sake of testing only - to suggestions that instead involve tricks manipulating the Browse dialog in Mac OS X, but that approach does not meet my eventual requirements, because I can't rely on that approach for the final task of uploading many files from an Enterprise Java web application into the targeted ASP.NET web application. I must side-step the Browse dialog completely.
Background research
All of the following have related questions and answers but none of these solve the problem for the CuteWebUI AJAX file uploader:
How to type some text in hidden field in Selenium WebDriver using Java
How to handle windows file upload using Selenium WebDriver?
How to force Selenium WebDriver to click on element which is not currently visible?
how does selenium webdriver upload files to the browser?
How to deal with file uploading in test automation using selenium or webdriver
https://stackoverflow.com/questions/18886970/selenium-webdriver-upload-file-sendkeys-dont-work
File Upload using Selenium WebDriver and Java
How to handle with uploading files from modal window Selenium WebDriver Java
Handling a popup window using selenium
http://selenium.10932.n7.nabble.com/Handling-browser-pop-up-dialog-to-upload-a-file-td29153.html
This probably relevant forum link from CuteSoft is down:
Jul 24, 2012 - Hi,I'm doing automated testing for my web system using selenium webdriver, the problem is that I can't interact with ajaxuploader component
And a WebDriver forum posting with the INPUT unhide trick: https://groups.google.com/forum/#!topic/webdriver/JAXC_qEbQvI
I've found a solution for this; it comes with some caveats but it works for my situation. Hopefully it will help you too.
The biggest caveat is that I only know that it works with the current version of AjaxUploader as downloaded today. I'd been using an older version that worked differently; I can't specify a version number since CuteSoft seem pathologically inclined to make the version numbers of AjaxUploader almost impossible to figure out. I can't find a version number for the current version OR the older one that I was using.
The other caveat is that it relies on there only being one AjaxUploader control on the page at a time. It appears that AjaxUploader DOES create an <input type='file'> element, but it's left dangling in a div at the top of the <body> tag, without any id or name attribute or anything else to link it to a particular uploader. I'm not sure what AjaxUploader does when more than one of it are present: maybe it creates multiple file inputs and tracks which is which by javascript; maybe it shares one. For my situation I didn't need to solve that part so I didn't try it.
Anyway, the trick is to find the file input as follows:
webDriver.findElements(By.cssSelector("body > div > input[type='file']")).get(0).sendKeys(fileName);
Note that the selector here has nothing to do with the ID or any other characteristics of the particular upload button you're using. It's just "the first file input inside a div directly inside the body tag". That's where AjaxUploader inserts its file input.
There appears to be no need to interact with any of the rest of the UI, including the Browse button: sendKeysing the filename to the right file input causes the upload to begin immediately. (This is an edit; I originally thought that clicking the button was necessary too but it doesn't seem to be)
Note that to make sure the timing of your test works correctly, you probably want to follow this click up with a wait operation that waits until the file has finished uploading (eg by waiting for the HTML that the AjaxUploader inserts after uploading is complete) before proceeding to whatever you want to do next.

java.lang.ClassFormatError: Incompatible magic value 218774561

Hey everyone I am making my first applet for java today. I have been using a subdomain at a server and I don't know what to do because I am getting this really weird error.
I have my jar in the server and everything but every time I try to load the Applet this happens.
java.lang.ClassFormatError: Incompatible magic value 218774561 in class file Evolution/EvolutionApplet
Upon research it appears that an incompatible magic value means that something has been corrupted in the .jar
Here is the website http://dementedgames.site88.net/Main.html
the jars name is Evolution if you need the html code it is at the website.
Edit: The applet should be launched from Evolution.EvolutionApplet not Evolution.Evolution
The magic value of a valid Java class is 0xCAFEBABE, which is the hex value of 3405691582. This is represented by the first 4 bytes of the file. But you're getting 218774561 which in turn stands for the ASCII characters CR, LF, < and ! (the CRLF is a newline). To see it yourself, run this piece of code:
int magic = 218774561;
ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(magic);
System.out.println(new String(b.array()));
This in combination with the applet being served by a website suggests that it's the start of a <!DOCTYPE> which in turn suggests that it's a HTML document.
So, the request to Evolution.jar has apparently actually returned a HTML document. You should be able to see it yourself when you change the current request URI in browser address bar to point to applet's URL (e.g. change /page.html in end of URL to /Evolution.jar). Then you'll see what the browser actually retrieved when it tried to download the applet. Perhaps it's a simple HTTP 404 error document.
To fix it, just make sure that the URL in the archive attribute is correct. It's relative to the current request URL as you see in browser address bar.
The original problem seems fixed now. I could download the Jar from http://dementedgames.site88.net/Evolution.jar
Update
It seems the Evolution.Evolution class is not an applet! Running it from the command line using:
java -jar Evolution.jar
Produces a frame (with a very 'retro' look)! As such, forget this applet nonsense, and launch the frame from a link using Java Web Start.
Old Answer
OTOH it now throws a ClassNotFoundException that (after inspecting the Jar) makes me think it should be:
<html>
<head>
<title>Evolution</title>
</head>
<body bgcolor="#000000" text="#906060">
<center>
<applet code="Evolution.Evolution" archive="Evolution.jar" width="800" height="600">
</applet>
</center>
</body>
</html>
There are two changes to the code attribute worth noting.
The .class extension was removed. A minor matter, adding it is tolerated, but not correct.
The Applet removed from the class name.
BalusC above has explained it really well. In addition to that you can check this link
Thread: Incompatible magic value 218774561 error in applet
It seems that the codebase and/or the code attribute of ur applet tag need to pointed properly.

How to get all file paths while using html "file" input with multiple attribute

I am developing a web page in which I have to upload multiple files on
a single browse.
I am using html <input id="filelist" type="file" multiple=multiple>
This enables the multiple file selection and also retrieves the
full file path of all the selected file, which shows in file
upload text area.
<script language="JavaScript">
<!--
function showname(){
var filepath = document.form1.filelist.value ;
alert(filepath); //this shows only first filename among selected file
}
-->
</script>
But the problem is when I get the value of input, it returns only the first file
name among selected files.
Now how can I get the file paths which is shown in file upload text area.
Thanks!
This is browser specific. So you might be running this in a browser that doesn't support this. For example Firefox does. Here's an example of how to use this feature:
http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/
I would consider using http://www.uploadify.com/about/ or http://www.fyneworks.com/jquery/multiple-file-upload/. They should help you out, and also add some cool features to your file upload form.
File paths are deliberately hidden from the page for security purposes. To look at the local filesystem you need to use Java, Active-x (meh), or Flash.

Categories