Search code examples
rubyseleniumrspeccapybarasafaridriver

Basic browser authentication with Safari Capybara Selenium


I've got a problem with browser authentication on Safari using Capybara/Selenium.

I'm using this code to authenticate:

visit "https://#{ENV['AUTH_USERNAME']}:#{ENV['AUTH_PASSWORD']}@my-staging-app.heroku.com"

This works just fine on Chrome and FF but not on Safari.

Any ideas how to bypass this?


Solution

  • Okey, I've found the solution for this. I had to use reversed proxy using e.g. Nginx and send proper headers :)

    Here is how I've done it:

    In this example I'll be using creds login: admin and password: secret123.

    Go to https://www.base64encode.org and encode your creds admin:secret123.

    In this example it's YWRtaW46c2VjcmV0MTIz

    brew install nginx

    sudo vim /usr/local/etc/nginx/nginx.conf

    Past there this code:

    worker_processes  1;  
    
    events {
        worker_connections 1024;
    }
    
    http {
        server {
            listen 8080;
            server_name localhost;
    
            location / { 
                proxy_pass https://your_app.herokuapp.com;
                proxy_set_header Authorization "Basic YWRtaW46c2VjcmV0MTIz";
            }   
        }   
    }
    

    Change proxy_pass to match your app url.

    And proxy_set_header to Authorization "Basic <your_encoded_creds>"

    Then: brew services start nginx

    From now on, when you'll hit http://localhost:8080 you'll be redirected to your page and logged in.