0

Question

I want to start the Safari web browser as the process to visit a specific website, then wait until it is closed.

A special situation is that the browser may already be open and running, as the user may have visited some website already.

In that case, the browser would probably open a new tab in an existing window and the newly launched process will be terminated immediately. This should not confuse my waiting process: I either want a new browser window (if that can somehow be enforced, maybe via command line arguments) and wait until that is closed, or keep the existing browser window and wait until all the tabs resulting from my process are closed.

Environment

I think it doesn't matter, but my programming environment is Java and you can assume that I know the path of the browser.

Example

The only browser for which I can obtain the expected behavior is Internet Explorer (sigh.). Here, I need to basically create a new batch script in a temp folder with something like

start /WAIT "" "C:\Program Files\Internet Explorer\iexplore.exe" -noframemerging http://www.test.com/

I then run the batch script instead of directly the browser and delete it once I am finished with waiting.

Intended Process

To make the intended process clearer:

  1. My program starts.
  2. My program launches the Safari browser as separate process and provides an URL to visit as argument to that process.
  3. The Safari browser runs asynchronously, as a new process, and visits the provided URL. So far, this is easy.
  4. After launching the new process (the Safari browser), my own program should wait for the said process to terminate. This is the hard part, as
    1. Many modern browsers start multiple processes. I would need to wait for all of them.
    2. Many modern browsers may somehow "detach" themselves from the process that I launched. Sorry, I don't know a better word, what I mean is: I start a process which then starts another process and terminates immediately while the other process keeps running. If I wait for the browser process originally started by my program, the waiting will be finished while the browser is still open.
    3. A special case of the above is tabbed browsing as realized in many browsers: If the browser is already open (in a separate process started by the user) when I launch it, my newly started browser process may simple communicate the URL to visit to the existing process and terminate. The user is still on my provided URL while my program thinks she has closed the browser. This issue can maybe be forbidden by specifying a special command line argument, like noframemerging for the IE.
  5. Once the browser has terminated or all tabs related to the URL I provide have been closed, my program will cease to wait and instead continue doing its business.

The use case is that I have a web application which can either run locally or on a server. If it is run locally, it launches a web server, then opens the browser to visit the entry page. Once the browser is closed, that web application should shut down as well. This works reliably for Internet Explorer. For all other cases, the user has to close the browser and then, explicitly, the web application. Thus, if I could wait reliably for Safari to finish, this would make the user experience much better.

Solution Preferences:

Solutions are preferred in the following order

  1. Anything which ships with the pure Java JRE. This includes special command line arguments to the browser.
  2. Things that require me to, e.g., create a batch script (such as in the IE case.)
  3. Anything that requires 3rd party open source libraries.
  4. Anything that requires 3rth party closed source libraries.

Any programming language independent solution (e.g., command line arguments only) is preferred over a pure Java one.

Reason: In the ideal case, I would like to know what exactly is done and include it into my own code. As I want to support different browsers (see "PS" below), I would like to avoid having to include one library per browser. Finally, I cannot use commercial or closed source libraries, but if no better answer turns up. Of course, I will honor any working solution with acceptance. I will accept the first (reasonably nice) working answer of type "1". If answers of lower preference turn up, I will wait a few days before accepting the best one among them.

PS

I will launch a couple of similar questions for other browsers. Since I believe that browsers are quite different in the command line arguments they digest and the way they launch threads and sub-processes, I think this makes sense. If I would just ask how to start a/any browser and wait for its termination, it would be quite hard to determine the "right" answer. Having, like, one question per well-known browser could be a nice point of reference, too.

Community
  • 1
  • 1
Thomas Weise
  • 319
  • 3
  • 13
  • Will the target device be a mac? If so you can use a script. /Applications/Safari.app/Contents/MacOS/Safari & sleep 1 && osascript -e 'tell application "Safari" to open location "http://localhost:8080"' – Pumphouse Aug 10 '15 at 20:59
  • I'm assuming it will not only be a mac, so this solution may not work. – Pumphouse Aug 10 '15 at 21:00
  • This question is *not* a duplicate. As I explicitly stated, I am looking ideally for answers which work without libraries, e.g., command line parameters of the specific browser or other ways to wait for the specific browser (maybe something via stdin/stdout, whatever). Such answers will be highly browser-specific, a Firefox answer is unlikely to work in safari, and vice versa - as I explicitly stated. If I would ask for "browsers" in general, what do I do in case of multiple browser-specific answers? (Of course, I will accept the selenium answer if no library-free answer occurs.) – Thomas Weise Aug 11 '15 at 06:04
  • To further elaborate on why I think that this question is not a duplicate: If someone asks "How can I make a colored table in Word?", that would not be a duplicate of the question "How can I make a colored table in OpenOffice?". I believe the situation is similar here. – Thomas Weise Aug 13 '15 at 10:28

1 Answers1

0

Since there is a Selenium Safari driver here

https://code.google.com/p/selenium/wiki/SafariDriver

I assume that what you want above is possible on MacOS (and in other OS) using the Selenium library. I have only tested a basic functionality of this on Firefox and Chrome as was posted in your other questions. If those are really applicable to you requirements, then this is also possible with Safari. This may need more work, though.

Selenium was intended for software testing, but I believe it can be utilized for the purpose of your application. And I think this technique is possible also with other browsers as long as Selenium supports it.

For Selenium for Chromium I found a hint in this: Use Selenium with Chromium Browser

And here the link for the Opera driver https://code.google.com/p/selenium/wiki/OperaDriver

For Edge, I found this hint Is there a Selenium WebDriver available for the Microsoft Edge browser?

Community
  • 1
  • 1
d_air
  • 591
  • 1
  • 3
  • 8