Is there a way to detect browser closing in JApplet? - java

There are certain configuration files that the user should save before closing the browser. Is there a way to detect if the user is trying to close the browser and to warn them to first save the files?

You should define a public method inside your applet and invoke it using Javascript before the user closes a page:
window.onbeforeunload = document.YourApplet.YourMethod(event);
where YourApplet is your applet's name attribute: <applet name="YourApplet" ...
and YourMethod is the method you defined in your applet.

You can use javascript to detect this. And you can invoke applet methods from javascript.
So you should define a javascript onunload handler which invokes your applet's close method.

Generally the PlugIn will try to stop the applet once the browser window (or just tab) closes or navigates elsewhere.
IIRC, you used to get 20 seconds to do your clear up before the PlugIn started getting medieval. I believe if there are no other applets sharing the JVM, the process will now get killed if you don't stop.
So, you really don't want to be hanging around attempting to show annoying save dialogs. I would go so far as to say, you don't want annoying save dialogs. So just do the saves quietly. Add a "you didn't save, do you want to restore these changes" thing (like the re-open windows dialogette beneath the toolbar in Chrome) when restarting. For extra marks, log deltas periodically in the background, so that even in the case of abrupt termination or network failure the data can be restored.

Related

Handling browser pop-ups that stop processing in Java/Selenium

Environment: Selenium testing in Java against Chrome
Scenario: in the GUI of my application I have a button that causes a form full of data to be submitted to an external service, whereupon the user is re-directed to the external service landing page.
Because my application is inside my corporate firewall, a username/password has to be supplied for consumption by the external service, but the application is not aware of this, so doesn't provide it (it would work normally in production, but the test environment is a special case). Therefore, a pop-up appears, and during manual testing the tester supplies a username and password manually and then submits the dialog. This is not a JavaScript dialog - I assume it's an actual modal Windows dialog, so it effectively halts processing and selenium just waits around till it's gone.
Problem: I am trying to automate this process, and cannot get passed the dialog. Because the dialog prevents java/selenium from processing, I cannot implement code to handle the dialog, such as integrating AutoIt or using Robot , because program flow never gets to that code after the button is pressed. Usually, I'd install an independent version of AutoIt to run on my machine in the background and catch the pop-up (not ideal, but it works), but due to very tight restrictions in the corporate domain policy this isn't possible in the short-term. I suspect they have a white-list for executables, so it may be tricky getting any third-party tool to work.
Can anybody think of a way around this?
Can't use Alerts, as these are not JavaScript dialogs
I have to be able to enter the username, password and submit the dialog
Can't use integrated capabilities like AutoIt or Robot
Can't use an independent tool like AutoIt due to domain policy
I suspect it's not possible, but worth checking if any bright spark has any ideas.
It's not supported in WebDriver so it can't be done using plain Selenium.
There is an issue open in the WebDriver project to support handling basic auth prompts:
https://github.com/w3c/webdriver/issues/385
https://github.com/SeleniumHQ/selenium/issues/453
Alas, the issue is open and nothing is implemented yet.
If you can't use AutoIt from another process because that process has to be in a certain whitelist, then you can probably use it from another thread using autoitx4java. If you can detect the dialog itself then you should certainly do it, but AFAIK the dialogs of Chrome are transparent to AutoIt (any other technologies based on Windows UIAutomation). In this case, just spawn the thread before pressing the button, make this thread sleep for 1 second or so (in the first statement of the thread method), and then "blindly" type the user name, Tab key, the password, and Enter. It's not very element, but I believe it should work.

A program that ask you which web browser you want to use

I want to make a simple program that ask you which browser you want to use.
When you'll click on a link (email or pdf for example), a program will appear instead of the browser and will ask you which browser you want to use.
First : in which code it's the best to write this?
Secondly : How to open the program and not the browser?
I need a bit of help to write this, because i don't know how begin
This is tricky. There is no uniform event for "User clicks on url" that spans all applications.
The most straightforward solution would be to create your application and register it as the default browser.
Personally I would be annoyed by having to select a browser each time I click a link so I would probably make a helper application that when it starts remembers the current default browser and then registers the browser selector as the default. And when the helper closes it would restore the default browser.
My Java is quite rusty but I think this should be possible in either language. Maybe a little trickier in Java due to the lack of a dedicated executable for your application.
For C#:
This will open the url in the default browser:
string url = "http://google.com/";
System.Diagnostics.Process.Start(url);
This will open the url in the specified browser:
System.Diagnostics.Process.Start(#"C:\Program Files\Mozilla Firefox\firefox.exe", url);
There will be an error if the browser does not exist.

Using print dialog from applet makes browser window unresponsive

I have a ten-year-old applet that wraps itself around the Crystal Reports Viewer applet in order to handle some custom features. It has worked satisfactorily, displaying and printing reports as designed. The web-app provides data selection, and then generates the HTML to invoke the applet with the correct parameters via an AJAX call.
I have one user (so far) who experiences the following problem: after generating the applet and viewing the reports, she prints the reports. Once the print dialog has appeared, even if it is cancelled, the following things no longer work:
the Close link which is supposed to clear out the generated applet HTML no longer works;
the navigation links to other pages no longer work: they dim appropriately when the mouse hovers over them, but the browser does not respond to clicking on them;
the window cannot be closed, whether
by clicking the X in the upper-right-hand corner,
by clicking on the system menu and selecting Close,
by right-clicking on the entry in the task bar and selecting Close, or
by trying to invoke the File/Exit menu item (I believe the File menu is disabled);
clicking on the window title-bar no longer makes it appear active.
In order to close the window, one has to start the Task Manager and end the iexplore process.
It may be that this is another occasion where the applet is retaining focus and it should not: similar bugs such as
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4838000
are reported as being folded into a bug I can no longer find in the Java bug base:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4918282
There are also reports of invisible applets retaining focus: it might be that the custom applet is invoking the original Crystal Reports applet in some way, but I didn't myself develop this code, just the wrapper that drives it.
I do not believe I have a case where a dialog of some sort is appearing behind the IE window; I should think that would occur on more than one system, and this appears to be unique to this user, but the system looks like one of our stock images.
There are also reports of printing locking up an IE window until printing is complete, where the solution is to put the printing into another thread. In my case, it is unnecessary to actually print: invoking the Print dialog and then cancelling it is enough to cause the buggy behavior.
I would like to understand what's going on here, and get things running for this user if possible. I'd appreciate any reports of similar experiences, or directions to look that I've so far missed.
Some version notes:
Win XP Pro: 5.1.2700 SP 2 Build 2600
IE: 7.0.5730.13
Java JRE: 1.6.0_31-b05
Printer: Imagistics 2500 USB printer (on the off chance)

zombie java interpreter after launching applet prevents java console to reappear if relaunched

I have a java application that can be run as a JFrame or as an applet.
Launching the applet in the browser adds a new tab in the browser and also makes the Java Console appear. After I close both of them, I expect to be able to repeat this operation and see the Java Console reappear. It does not reappear, however I can reload my applet in the browser.
Everytime I have to kill this zombie java interpreter process launched by firefox.
/usr/lib/jvm/java-6-sun-1.6.0.26/jre/bin/java -D__jvm_launched=22906841394 -Xbootclasspath/a:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/deploy.jar:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/javaws.jar:/usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/plugin.jar -Djava.class.path=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/classes -Dsun.awt.warmup=true sun.plugin2.main.client.PluginMain write_pipe_name=/tmp/.com.sun.deploy.net.socket.1444.1704699298565364673.AF_UNIX
After killing the process, the Java Console will reappear. Also tried the repro steps with chromium, it's the same result except the zombie process' commandline is different.
Things to note:
When I close the browser tab containing the applet, the applet is in a normal state, waiting for the user to perform actions on the UI.
Closing the browser completely between applet relaunches will also fix the problem since the java process is a child of the browser process.
Anyone has an explanation or workaround? Thanks!
Anyone has an explanation or workaround?
For the work-around, test in the applet viewer. (The console is not relevant to production, unless the design is very flawed.)
As to the explanation, a browser will end the JRE/console when/if it feels like it. It is not up to the developer or applet to override that. I've noticed that FF will typically leave the JRE running for 30-60 seconds after leaving an applet page (judging by when the console vanishes).
It's not a zombie, by which I imagine you mean to imply something is going wrong; this is just how the Java plug-in has always worked. The first time it's needed, it starts up, and this may involve opening the console, depending on your settings. It will then continue to run until the browser shuts down. In other words, this is not a bug, but a feature.
Some browsers include a menu item for opening the Java console; you could use that to reopen it as needed, without killing or restarting anything.

In Java, it's possible change execution from Graphic Mode to Text Mode in Runtime?

I making a webcrawler and I only need the GUI to set parameters for the crawling, after that, the execution runs forever in an infinite loop.
It's possible when I start crawling (currently clickin on a button), I dispose of the GUI to free resources and and run the application in text/console mode?
You should be able to close your application's frame. Just make sure that the frame isn't set to exit on close. The only way to exit the process will be to send it a signal of some sort. You might want to arrange to catch the signal so you can exit in a controlled way. Another approach might be to have the process listen on a port for a shutdown message.
It's possible. You just need a way to close the GUI programmatically right? This question might be helpful: How to programmatically close a JFrame
So the idea is when you click the button, the handler of the button will send exist signal to the GUI, and start the crawling process (probably by creating some object).
At the very least, you can always start a child console Java process (passing a generated configuration/data file if the amount of data is significant).

Categories