3

How to I configure Selenium to drive Safari in Private mode? (I'm using the Ruby interface).

For context, here is why it is important to run tests in Safari's Private mode: Attempting to write to the local storage will raise an error in Safari's Private mode. Therefore, I want automated tests that verify that my code avoids writing to local storage when in Private mode. (And, perhaps more importantly, will bring future errors to the attention of developers

Community
  • 1
  • 1
Zack
  • 6,232
  • 8
  • 38
  • 68
  • Can't you just run your tests in normal mode, and afterwards check if anything is in local storage using normal file I/O in ruby - forget Selenium? – SiKing May 28 '15 at 16:47
  • The normal operation of the page is to save some data in local storage. Consequently, the JavaScript crashes if a user visits the page using Safari in Private mode. I want an automated test (think CI) that will verify that the page works in Safari's Private mode. The automated part is important so that if, a year from now, another developer forgets that you cant write to local storage when in private browsing mode, the automated test will catch the oversight. – Zack May 29 '15 at 12:24
  • Also, for clarification, by "local storage", I mean the HTML5 local storage (http://www.w3schools.com/html/html5_webstorage.asp), not file on the local file system. – Zack May 29 '15 at 12:26

1 Answers1

3

Lets look on the definition of Private Browsing here: https://support.apple.com/kb/PH19216?locale=en_US

When you use Private Browsing windows, Safari doesn’t save your browsing history, and it asks websites you visit not to track you.

and here:http://en.wikipedia.org/wiki/Privacy_mode

Privacy mode or "private browsing" or "incognito mode"[1] is a privacy feature in some web browsers to disable browsing history and the web cache. This allows a person to browse the Web without storing local data that could be retrieved at a later date. Privacy mode will also disable the storage of data in cookies and Flash cookies. This privacy protection is only on the local computing device as it is still possible to identify frequented websites by associating the IP address at the web server.

So this means that Selenium is equivalent to turning on Private Browsing. Every time you start any driver via Selenium it creates a brand new anonymous profile, you are actually browsing privately. (if you do not use already created profile of safari)

BUT! If you still think that you need to run safari in incognito mode, you can use the following hack:

you can automate the process of enabling the Private Browsing option with AppleScript. Like so.

  1. First, launch the Universal Access system preference and enable the Enable Access for Assistive Devices option.

  2. Launch Script Editor (within the AppleScript folder inside the Applications folder) and enter the following script:

tell application "Safari"
  activate
end tell

tell application "System Events"
  tell process "Safari"
      tell menu bar 1
          tell menu bar item "Safari"
              tell menu "Safari"
                  click menu item "Private Browsing"
              end tell
          end tell
      end tell
  end tell
end tell

Info from here: http://www.macworld.com/article/1139714/enableprivatebrowsing.html

Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101
  • 1
    The script is very helpful, thank you. Now, how do I apply it to the particular instance of Safari that is being driven by selenium? – Zack Jun 02 '15 at 15:39
  • 4
    Selenium is not equivalent to turning on Private Browsing. If I turn on Private browsing in Safari, then this line of JavaScript will throw a QuotaExceededError: 'localStorage.setItem("key", "value")'. That doesn't happen simply by using Selenium. – Zack Jun 03 '15 at 12:51
  • QuotaExceededError shown because of browser's fail of saving information locally. In case of selenium -- items will be not saved not because of they blocked by browser, but because of they will be deleted by selenium before next session will be started. So in some sense they are equivalent -- as this data will be not used on the next session. If you still need to run exactly in incognito mode, I think there are exist ability to change script above for running incognito for some specific profile. In this case you will be needed to create this profile and to use it on selenium start. – Andrew_STOP_RU_WAR_IN_UA Jun 03 '15 at 16:25
  • Add in the fact that that when you use SafariDriver, simply navigating to any page you're already logged in, will still be logged in. Private browsing will start you with absolutely no session information. This is what brought me to the question. – Levi Roberts Jan 10 '16 at 09:37