views:

241

answers:

2

I'm trying to route watir through a proxy pragmatically -- this means within the script I'd like to change my proxy dynamically before launching the browser.

Here's what I've tried so far (and so far am failing): I'm running chrome and lucid lynx ubuntu. I chose TREX cause I thought watir might be making use of PROXY or something.

I rewrote /usr/bin/google-chrome as:

#!/bin/bash
/opt/google/chrome/chrome --proxy-server="$TREX" $@

The reason I'm passing in the environment variable to proxy-server rather than http_proxy is because I never could get http_proxy to work as is anyways

then I did a simple:

require 'rubygems'
require 'watir-webdriver'

ENV['TREX'] = "XX.XX.XX.XX:YY"
browser = Watir::Browser.new(:chrome)
browser.goto("http://mysite.com")

Anyways, what is happening here is that it is forwarding me to the login page of the proxy rather than just forwarding the request.

What am I missing here? I feel like I'm pretty close.

+1  A: 

require 'win32/registry'

PROXY_SERVER_IP = "192.168.1.20" PROXY_SERVER_PORT = "3128"

def set_browser_to_use_proxy

log "Enabling Proxy at #{PROXY_SERVER_IP}:#{PROXY_SERVER_PORT}" Win32::Registry::HKEY_CURRENT_USER.open('SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings' , Win32::Registry::KEY_WRITE) do |reg| reg.write('ProxyEnable', Win32::Registry::REG_BINARY, '1') reg.write('ProxyServer' , Win32::Registry::REG_SZ, "#{PROXY_SERVER_IP}:#{PROXY_SERVER_PORT}" )
reg.write('ProxyOverride' , Win32::Registry::REG_SZ, '') end end

def set_browser_to_use_proxy_off

log "Disabling Proxy at #{PROXY_SERVER_IP}:#{PROXY_SERVER_PORT}" Win32::Registry::HKEY_CURRENT_USER.open('SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings' , Win32::Registry::KEY_WRITE) do |reg| reg.write('ProxyEnable', Win32::Registry::REG_DWORD, '0') reg.write('ProxyServer' , Win32::Registry::REG_SZ, "#{PROXY_SERVER_IP}:#{PROXY_SERVER_PORT}" )
reg.write('ProxyOverride' , Win32::Registry::REG_SZ, '') end end

Paul Rogers
The code is not formatted, it is almost unreadable. If you select the code and click `101010` button in toolbar it will format it as code.
Željko Filipin
Also, looks like you have provided solution for Windows, and feydr is on Linux.
Željko Filipin
yeh.. I've seen this sample a lot but def. do not want to be relying on windows
feydr
+2  A: 

The problem here is that you cannot auto authenticate to a proxy server using chrome --proxy-server=blah:3128

Chrome will always prompt you for a user name and password on an authenticating proxy when it starts up (via webdriver or manually)

A complicated way around this is to install a local instance of apache with squid as a transparent proxy that authenticates to the remote proxy server. Then you just start chrome with the proxy flag pointed to the local squid proxy. In other words, chain an unauthenticated proxy in front of the authenticated proxy...

I've also been searching for a solution to this problem. I thought maybe something like webrick as a proxy, or corksrew... but can't get the answer just yet ...

Tim Koopmans
yeh.. I'm trying to avoid tacking on yet another proxy here but you are correct that is one way of doing it -- also, just in case someone wants to suggest tor -- I don't want to use it either -- there are webrick proxy samples all over the place and I was thinking about using curb to do the request and bring it through that ...
feydr
In case you're interested I added a chained proxy solution here:http://altentee.com/2010/authenticating-proxy-with-webdriver-and-watir/Unfortunately couldn't achieve the same with something less complicated.
Tim Koopmans