2

How is it possible to get browser IP with Watir? I'm using proxy and I want to verify if it's working correctly. Perhaps there is some other way if proxy is working?

Here's my current code:

profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :http => 'my.proxy.com', :ssl => 'my.proxy.com'
browser = Watir::Browser.new :firefox, :profile => profile
browser.goto 'http://someurl.com'

The browser will open the url, although the proxy is not working.

Thanks for help

all jazz
  • 2,007
  • 2
  • 21
  • 37
  • I do not think browser has IP, but the entire machine. Watir just drives the browser. How would you get the IP manually? – Željko Filipin Apr 18 '12 at 14:48
  • The problem is that if you are behind a proxy, the IP the rest of the world see's when you make requests is not the IP of your local box, hence the need to bounce off a server out in the wild and see where it thinks the request came from. This can be important if you need to configure a firewall on a cloud server hosting your test environment so that it will accept your requests. – Chuck van der Linden Apr 18 '12 at 17:50
  • @ChuckvanderLinden - I think the problem that he needs to solve is verifying that he is indeed going through a proxy and that the IP has changed. – Dave McNulla Apr 19 '12 at 16:27
  • You could help people help you if you were more upfront about the problem you are trying to solve. – Dave McNulla Apr 19 '12 at 16:27
  • @ŽeljkoFilipin - You are right, the browser is never assigned an IP, but technically the network devices are assigned IP addresses. Then when a call goes through the proxy, that call is assigned an ip through the protocol. Every call could be assigned a new IP by the proxy. – Dave McNulla Apr 24 '12 at 06:01

2 Answers2

3

This is really not a pretty way of getting around this but you could use the following to get the ip.

browser.goto("http://www.whatsmyip.org/")
ip = browser.span(:id, "ip").text

As i said it is really not an ideal way but i am not sure if watir has access to the ip you are on.

Note that if you use the site above, please respect the author's wishes and do not generate a high volume of requests against the site. If you look at the source there, you will find this comment:

Please DO NOT program a bot to use this site to grab your IPs. It kills my server and thats not nice. Just get some cheap or free web hosting and make your own IP-only page to power your bot. Then you won't even have to parse any html, just load the IP directly - better for everyone!!

As good citizens of the net we need to respect that. I doubt he would be upset by a few hits a day, but if your scripts are doing this a lot, make your own reflector page to report your IP back to you.

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
lambsubstitute
  • 1,981
  • 2
  • 13
  • 9
  • yeah but ipchicken's html is lacking any identifiers and that makes it a little more of a pain to get the ip address using something that won't be brittle (although a regular expression will likely work just fine I'd think, just that for some newbies, regex is akin to dark magic) – Chuck van der Linden Apr 18 '12 at 17:47
  • Is there any other way beside reading websites that show your IP? – all jazz Apr 18 '12 at 19:21
  • Yeah, Chuck is completley right, i didnt think about the load of hitting this constantly. We should all try to be good citizens :) – lambsubstitute Apr 19 '12 at 08:44
2

You don't need Watir to go through a proxy to get the IP. You can use net/http, which has less overhead and is easier. BTW, I used whatsmyip.com here but I do not believe that it's so reliable. there are others including http://whatismyipaddress.com, http://show-ip.net, http://ipchicken.com, http://www.ipaddresslocation.org, http://www.myipaddress.com/show-my-ip-address/, http://www.lawrencegoetz.com/programs/ipinfo/, http://www.find-ip-address.org.

require 'net/http'
uri = URI("http://automation.whatismyip.com/n09230945.asp")
Net::HTTP::Proxy(proxyhost, proxyport, proxyuser, proxypassword).start(uri.host) do |http|
    req = Net::HTTP::Get.new(uri.path=='' ? '/' : uri.path)
    @ip = http.request(req).body.scan /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
end
Dave McNulla
  • 2,006
  • 16
  • 23
  • just don't pick a site like whatismyip.org that has the ip address in an image.. unless you want to spend a lot of time shaving an OCR yak. – Chuck van der Linden Apr 18 '12 at 17:48
  • I need to verify if proxy is working, so I was thinking what would be the easiest way to check that. Is there any other solution perhaps? – all jazz Apr 18 '12 at 19:20
  • Is there any other way to check if proxy is working? Or to get IP without using those services? – all jazz Apr 18 '12 at 19:20
  • You could write your own javascript site that figures out the IP address. The rest of the code would be the same. http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript – Dave McNulla Apr 18 '12 at 20:34
  • One more thing. If you are testing that you are going through an ip, you should get the IP with and without the proxy to see it change. – Dave McNulla Apr 18 '12 at 21:15
  • The javascript should run on the server side. Whatever tool you use gets the data directly from the web server. – Dave McNulla Apr 19 '12 at 13:44