Launch Safari and wait until it closes

Question

I want to launch the Safari web browser as a process of visiting a specific website and then wait until it is closed.

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

In this case, the browser will likely open a new tab in the existing window and the recently launched process will be terminated immediately. This should not confuse my wait process: I either want a new browser window (if this can be implemented somehow, maybe using command line arguments) and wait for this to close, or save the existing browser window and wait. while all tabs, my process is closed.

Environment

I think it doesn't matter, but my programming environment Java

and you can assume that I know the path to the browser.

Example

The only browser I can get the expected behavior for is Internet Explorer (sigh). Here I need to create a new batch version of the script in a temporary folder with something like

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

      

Then I run the batch script instead of the direct browser and delete it as soon as I am done waiting.

Intended process

To make the required process clearer:

  • My program will start.
  • My program launches the Safari browser as a separate process and provides the visit URL as an argument to this process.
  • The Safari browser starts asynchronously as a new process and visits the provided URL. It's easy so far.
  • After starting a new process (Safari browser), my own program should wait for this process to complete. This is the hard part because
    • Many modern browsers run multiple processes. I will need to wait for them all.
    • Many modern browsers can somehow "detach" from the process I was running. Sorry, I don't know a better word, I mean: I start a process, which then starts another process and terminates immediately while the other process continues to run. If I expect the browser process originally started by my program, the wait will complete while the browser is still open.
    • A special case of the above is the tabbed browsing implemented in many browsers: if the browser is already open (in a separate process started by the user) when it starts, my newly launched browser can simply pass the URL to visit the existing process and terminate. The user is still at my provided URL while my program thinks it has closed the browser. This problem can be prevented by specifying a special command line argument, for example noframemerging

      for IE.
  • Once the browser exits, or all the tabs associated with the URL I specified have been closed, my program will stop waiting and continue its activity instead.

The use case is that I have a web application that can run locally or on a server. If it runs locally, it starts a web server and then opens a browser to visit the login page. When the browser is closed, this web application must also be disabled. It works safely for Internet Explorer. For all other cases, the user must close the browser and then, explicitly, the web application. Thus, if I could reliably wait for Safari to complete, it would make the user much better.

Solution preferences:

The solutions are preferred in the following order

  • Everything comes with a pure Java JRE. This includes browser-specific command line arguments.
  • Things that require me to, for example, create a batch script (like in the case of IE.)
  • Anything that requires third party open source libraries.
  • Anything that requires a 3rd party closed source library.

Any programming language independent solution (like command line arguments only) is preferred over pure Java.

Cause. Ideally, I would like to know what exactly is being done and include it in my own code. Since I want to support different browsers (see "PS" below), I would like to not include one library per browser. Finally, I can't use commercial or proprietary libraries, but unless a better answer comes up. Of course, I will honor any working decision with acceptance. I'll accept the first (reasonably good) working answer like "1". If lower preference answers come up, I will wait a few days before accepting the better one.

PS

I ran a couple of similar questions for other browsers. Since I believe browsers are very different in the command line arguments they digest and how they start threads and subprocesses, I think this makes sense. If I were just asking how to start / any browser and wait for it to finish, it would be quite difficult to figure out the "correct" answer. For example, a single question in a famous browser might be a good starting point.

0


source to share


1 answer


Since there is a Selenium Safari driver here

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

I am guessing that what you want above is possible on macOS (and other OS) using the Selenium library. I only tested the basic functionality of this on Firefox and Chrome as posted in your other questions. If it really does apply to your requirements, then it's also possible with Safari. This may require more work.

Selenium was intended for software testing, but I believe it can be used for your application. And I think this method is possible with other browsers as well, as long as Selenium supports it.



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

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

For Edge, I found this hint Is the Selenium web browser available for the Microsoft Edge browser?

0


source







All Articles