I am preparing a program in java and its purpose is to make the HttpWebRequest for a url which can open in any browser (i am planning to use watij for opening the page in different browser).Now my program should take the screen shot of the presently opened url once the page has loaded successfully.I need to run a piece of code after the browser has loaded successfully in the browser.I dont want to use the javascript document.ready function for identifying if the page is loaded successfully or not.I should be able to know the page load complete status in the server side (s0 that i can execute that piece of code for taking the screenshot).How can i do it in java.Or is there any other way to do achieve that if its not possible in java.May be create some plugins.
An early reply is highly valued,
Regards,Sagar.
you can do it using applet, [just taking snapshot i meant that you want.]
Related
I want to simulate opening a web page in java, I know I can do this to actually open the page in my browser on my computer,
String htmlFilePath = "path/to/html/file.html"; // path to your new file
File htmlFile = new File(htmlFilePath);
// open the default web browser for the HTML page
Desktop.getDesktop().browse(htmlFile.toURI());
// if a web browser is the default HTML handler, this might work too
Desktop.getDesktop().open(htmlFile);
But is there a way to simulate it so I don't actually see it open on my computer, but it still evaluates like someone did open the web page.
Or if that is not possible what would be the easiest way to physically open it on my computer and then have a way of getting a callback so that I know when the page has been loaded?
Thanks
There are several ways to emulate an HTTP client (such as a web browser):
Jersey (Java) - https://jersey.java.net/documentation/latest/client.html
Apache HTTPClient (Java) - https://hc.apache.org/
JMeter (Java) - Use JMeter to record an HTTP request and replay it as a test - https://jmeter.apache.org/usermanual/jmeter_proxy_step_by_step.pdf
Selenium (browser plugin) - http://www.seleniumhq.org/
CURL (command line tool) - http://curl.haxx.se/
I do recommend Jersey in your case. It is a tool especially designed for REST. So it may even help server-side development.
I know you specifically asked for a Java solution, but the last two options are really popular.
I have a sample program here that uses the Selenium library.
Launch Firefox and Wait until it is Closed
The program has a code that launches a browser and opens a website. It can detect if the browser has done loading the website. It can also detect if the browser was closed.
I have a regular JSP/Servlet/Java web application that is used for uploading pictures from a mobile device. I am using Apache Commons library for the upload. Application is hosted on WebSphere Application Server 7.0.
Everything is working fine and the user can upload several images totaling 8MB or more if he has a really good/strong signal/connection or on a good WiFi.
The problem arises when the user is at a location with poor 3G/4G signal/connection. He gets errors like "Illegal state exception" or some time-out error, and in some cases the mobile browser just stays on the submit page with the progress bar no longer moving.
Any suggestions on how to "gracefully" handle this? Like is there a way to intervene after a set amount of time and give the the user an option to submit the form without the file attachment (i.e. just submit the form text fields)? Any other suggestions are welcome too.
UPDATE: The setTimeout solution below worked for me. The other missing piece was that I have to issue a "browser stop" command to stop the original submission that's in progress before I can issue a re-submit. Otherwise, my re-submit command will just be ignored by the browser.
The usecase here is simple - if the upload didn't finish in N minutes, remove/clear the field using javascript and resend the form.
You don't need to control the upload in the basic implementation, just safely asume that if you set a timeout to resend, it won't happen if the first attempt was successful and the page reloaded.
jQuery pseudocode:
setTimeout(function(){
$imageFieldNode.remove();
$form.trigger('submit');
},30000);//after 30 seconds
The more advanced way is to use a ready solution for controlled upload. They work like that:
upload starts
js prompts the server in intervals with a GET query to get the size of content that was already received.
everytime it gets the info - it reports progress.
You can do a lot with these libs.
You can think about the approach used in popular webmail clients (when attaching files to a message):
The files are uploaded independently (i.e. before) of the form submit, using javascript. Each of the files are stored in a temporary directory, and after the upload succeeds the user can proceed with the action.
The upload status is displayed to the user, and if it fails the main action (form fill/submit) does not get interrupted.
I am trying to trigger an event in JavaScript using a Java class.
How can I send javascript code (either direct input or from a file) in a Java class to a specified destination file which is already opened in the browser (Firefox)?
Currently I'm using the ScriptEngineManager to run my JavaScript code, but the code is executed in my Java environment and I don't know how to trigger an event in this way (as I cannot use e.g. window.postMessage("hi", "*");)
Any suggestions how to solve this? Work-arounds are also appreciated (preferably without extensions, plugins, ...).
Best regards.
When executing from java you have no access to window events - because there is no browser.
I don't really understand the use case here but you could load the destination file into an iframe and then do a meta refresh - once you update the files contents then the updated results will be displayed on the next refesh.
In any case you can only return results from the inputted javascript functions back to java - not call events.
You could pass a javascript function (a call that simulates the event) back to the browser and then execute it.
How you would pass this back to the client depends on the situation :
It could be on the response of the request to upload the javascript
You could use the meta-refresh mechanism
You could look into an ajax style way with client polling (if you didn't want to refresh manually each time).
EDIT
The server has no link to the browser between requests. To asynchronously receive a server event in a browser you could have a look at:
WebSockets
Comet
So the browser would receive notification when the task is complete and then it would be able to react - for instance prompt for the tab to close.
I need to download an attachment from a URL (say http://mywebsite.com) programmatically using Java. The tricky part is that the above URL opens up a new tab/window onload from which the downloading actually starts.
E.g.
Open http://mywebsite.com
http://mywebsite.com loads and opens up a new tab to http://mywebsite.com/attachments/someRandomFileName.pdf
*Note the file name in step 2 (above) is randomly assigned so I cannot hit http://mywebsite.com/attachments/someRandomFileName.pdf directly.
Thanks in advance!
The whole point of randomly generated URLs is to prevent people to access content directly.
They probably want users to go through their web site to access the file. There are numerous ways to check that request is coming from a real user/browser. This includes executing javascript and/or flash. Random URLs are generated on server and at some point they will be sent to browser.
So basically, to download a file, you'd have to look at what their web site is doing, what kind of requests it is making and what replies it gets from servers. Then try to reverse engineer this to get directly to random URL representing the file.
How can I access Java applet data before it displays on my browser?
The applet usually downloads the data from the server using the HTTP protocol, so there is nothing stopping you from using the same URL outside of the applet. The only questions is: What happens with the data? The browser can only download and save it to disk. If you want to process the data, you either need Java (and the applet) or, if the data is really simple, you can do some basic processing in JavaScript.
A third option is to process the data on the server and present it as HTML on a different URL.