2

I'm having troubles running Ruby/RSpec tests against Safari 11 when I'm trying to run tests via ssh manually or via Jenkins (where machine where Safari exists is remote slave).

When executing tests, I'm getting following error:

Selenium::WebDriver::Error::WebDriverError:
   unable to connect to safaridriver 127.0.0.1:7050

What is weird is that I'm able to run tests when I'm logged to the Mac machine directly. This leads me to the conclusion that there could be some permission which, by default, disables execution from ssh session but not sure why?

Also, to my knowledge, Safari Driver is part of Safari 10+ and as such is not installed anymore as extension

Update: I've found out that safaridriver executable that should be spawned by tests cannot be spawned from some reason when I login via ssh. Example:

/usr/bin/safaridriver -p 7050

It will just terminate with non-zero exit code while running same command directly on machine will run safaridriver in foreground. Since I expect Jenkins to be running test job on this machine, my idea to overcome this issue would be to launch safaridriver on this machine (by cron or launchctl) and then use this instance to connect to it with my tests. However, so far, I was not able to make my tests (Selenium/Capybara) re-use existing safaridriver instead of always trying to spawn new one on different port. Any idea on this would also be greatly appreciated.

This is my environment: OS: MacOS Sierra 10.12.6 Browser: Safari 11.0.2

Thanks in advance

Bakir Jusufbegovic
  • 2,806
  • 4
  • 32
  • 48

2 Answers2

3

There is a possible workaround to do this. First you need to create a Automator Workflow or Apple Script that launches the SafariDriver

Safari App

Then save this as a application. Let assume we name it SafariDriver7050.

Then from the SSH session you need to execute

open /Applications/SafariDriver7050.app

This will actually launch SafariDriver in the logged in session and it should work for you.

The caveat being shutting it down, you will need to first kill the SafariDriver7050 app and then you need to kill the safaridriver process. The order matters, else it will create a error dialog on UI

Edit-1:

As you suggested, it would be even easier to do this, when you wrap your test as a app and then it will be automatically be able to launch SafariDriver without any issues. The key to issue is using open command in a SSH session

Edit-2

Why does SafariDriver not work in SSH? Well if you look at the linux counterpart

In case of linux we can use the DISPLAY environment variable to launch an app in an existing display or we can use something like XVFB to launch the browser in a virtual display. That is the concept that most frameworks use in case of linux machines.

But Mac doesn't have such kind of feature, which is why this workaround is needed. Now why it doesn't have that, I am not sure. There may be some other workaround that I may not be aware of, so anyone who has valuable info, can help improve this part of the answer

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Thanks. You gave me good direction in which I would go. At the end, I've created *.app which actually runs my tests so I don't even need to spinup safaridriver independently. – Bakir Jusufbegovic Jan 16 '18 at 12:07
  • That's even better, updated the answer with the same – Tarun Lalwani Jan 16 '18 at 12:10
  • At the end, do you know what was the reason for not allowing to run safaridriver from ssh directly in the first place? I didn't encounter same problem with chromedriver and geckodriver (firefox). Also, can you describe what is the actual difference from the OS perspective when shell script is packaged in .app? I think that would be great addition to this answer that you wrote – Bakir Jusufbegovic Jan 16 '18 at 12:29
  • 1
    @BakirJusufbegovic, added my thoughts – Tarun Lalwani Jan 16 '18 at 12:38
0

For my case:

  1. On OSX host side was created Automator app as described above and modified a little bit:
security unlock-keychain -p your_host_password /Users/$USER/Library/Keychains/login.keychain-db
safaridriver --enable
safaridriver -p 7050
  1. On CCI/Jenkins side:
open /Applications/StartSafariDriver7050.app/
run pytest cmd
osascript -e 'quit app "StartSafariDriver7050"'
pkill safaridriver
  1. In Python:
def safaridriver():
    return webdriver.Safari(desired_capabilities=Caps.SAFARI, port=7050)
gnarkill
  • 1
  • 1
  • 1
  • 1