undefined method for 'downcase'. (WATIR/Ruby related. webdriver-user-agent) - ruby

I am trying to get Watir to emulate a mobile environment and I followed the directions verbatim from the very helpful, http://watirwebdriver.com/mobile-devices/. Here's my code.
#!/usr/bin/ruby
require 'rubygems'
require 'watir-webdriver'
require "webdriver-user-agent"
require 'headless'
$headmode = 0
$screens = 0
headless = Headless.new if $headmode == 1
headless.start if $headmode == 1
driver = UserAgent.driver(:browser => :firefox, :agent => :iphone, :orientation => :landscape)
....... snip ......
....... snip ......
The exception being thrown is .....
/var/lib/gems/1.8/gems/webdriver-user-agent-0.0.5/lib/webdriver-user-agent.rb:39:in `agent_string_for': undefined method `downcase' for :iphone:Symbol (NoMethodError)
from /var/lib/gems/1.8/gems/webdriver-user-agent-0.0.5/lib/webdriver-user-agent.rb:11:in `driver'
from ./test_CAPI.rb:11
Not being a Ruby developer, or being proficient in WATIR (yet), I'm mystified by this error. Can anyone shed some light on this? Many thanks Janie

The webdriver-user-agent is complaining that the values you passed for :agent and :orientation cannot be downcased due to the method not existing.
I think there are two solutions:
Upgrade to Ruby 1.9.3. I am guessing like me, you ran the code using something like Ruby 1.8.7. The downcase() method for Symbols does not exist in 1.8.7, which gives you the error. So you need a later version of Ruby that does have the method (ex 1.9.3 based on http://www.ruby-doc.org/core-1.9.3/Symbol.html).
Pass in a string for the :agent and :orientation values (example below). It looks like it works for the one example (though I do not use this gem so cannot tell you if there are issues elsewhere).
driver = UserAgent.driver (:browser => :firefox, :agent => 'iphone', :orientation => 'landscape')

Related

need changes to watir/loader.rb to make webdriver-user-agent work

I am using webdriver-user-agent mentioned here – http://watirwebdriver.com/mobile-devices/
This is the code I am using when trying out this gem
Browser: FF/Chrome
Ruby: 1.9.3 / Selenium :2.30.0 / Watir : 4.0.2
http_client = Selenium::WebDriver::Remote::Http::Default.new
http_client.timeout = HTTP_TIMEOUT
profile = Selenium::WebDriver::Firefox::Profile.new
device = ENV["DEVICE"]
orientation = ENV["ORIENTATION"]
driver = UserAgent.driver(:browser => :firefox, :agent =>device, :orientation=>orientation)
devices = UserAgent.resolution_for(device,orientation)
UserAgent.resize_inner_window(driver,devices[0],devices[1])
Watir::Browser.new driver
Now when the last statement is executed, i get the following error
(STEP) Launching FIREFOX (using web driver user agent)……
browser:
#
undefined method `to_sym’ for # (NoMethodError)
/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:42:in `load_driver_for’
/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:8:in `new’
Based on some investigation, problem is happening at highlighted line below as its trying to .to_sym on the selenium webdriver object.
def load_driver_for(browser)
if browser && browser.to_sym != :ie && Watir.driver == :classic
Watir.driver = :webdriver
end
Watir.load_driver
end
But if we add a line like given below, this gem is working as expected.
def load_driver_for(browser)
if “#{ENV["BROWSER"]}”.eql?(“chrome_useragent”)||”#{ENV["BROWSER"]}”.eql?(“firefox_useragent”)
Watir.driver = :webdriver
else
if browser && browser.to_sym != :ie && Watir.driver == :classic
Watir.driver = :webdriver
end
Watir.load_driver
end
end
since this is watir code outside of our framework, this is not the right way to do this, any suggestion on how to avoid this situation ?
The problem is reproducible when you do:
require 'watir'
require 'webdriver-user-agent'
driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
browser = Watir::Browser.new driver
browser.goto 'tiffany.com'
browser.url.should == 'http://m.tiffany.com/International.aspx'
You can fix the issue by requiring watir-webdriver directly instead of through the watir metagem. Change the first line to:
require 'watir-webdriver'

Is the browsermob-proxy gem for Ruby not working for anyone else?

I've been trying to implement Browsermob Proxy to detect network traffic for something I'm trying to automate, and I've copied the example code in the Github repository to my own code, but the terminal tells me the proxy.har (the har portion) is an undefined method. Upon further inspection, the proxy.rb file is pretty much completely empty, without any defined methods nor constructors.
Here's the github repo:
https://github.com/jarib/browsermob-proxy-rb
Can anyone tell me what's going on here?
Before ('#selenium_firefox_networkproxy') do
#server = BrowserMob::Proxy::Server.new("/Users/eliotchan/.rvm/gems/ruby-1.9.2-p320#cucumber/gems/browsermob-proxy-0.0.9/lib/browsermob-proxy.rb")
#server.start
#proxy = server.create_proxy
#profile = Selenium::WebDriver::Firefox::Profile.new
#profile.proxy = proxy.selenium_proxy
#driver = Selenium::WebDriver.for(
:remote,
:url => "http://"+FIREFOX_IP+"/wd/hub",
:desired_capabilities => :firefox,
:profile => #profile)
#proxy.new_har "Analytics"
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
end
After ('#selenium_firefox_networkproxy') do
#harfile = #proxy.har
#har.save_to "/Users/eliotchan/Documents/Analytics.har"
#proxy.close
#driver.quit
#verification_errors.should == []
end
The error I get is undefined method `har' for nil:NilClass (NoMethodError)

using redis and ruby to implement a tiny short url app

I'm making a short URL app, using Ruby, Sinatra, and Redis. Currently it's under 15 lines:
require 'rubygems'
require 'sinatra'
require 'redis'
require 'uri'
configure do
REDISTOGO_URL = "redis://localhost:6379/"
uri = URI.parse(REDISTOGO_URL)
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
get '/' do
haml :index
end
post '/shorten' do
a = rand(9999)
REDIS.set(a.to_s, params[:long])
"<pre>http://199.19.118.186/get/#{a}</pre>"
#haml :shorten
end
get '/get/:url' do
redirect REDIS.get(params[:url])
end
Where index.haml is a form that POSTs long to /shorten. I've no problem with that.
Right now, however, when I try to use Redis (with the server running, yes), I get this error:
What am I doing wrong?
EDIT: Copy/paste from Emacs... facepalm
EDIT: When trying to access redis alone from ruby (code below), I get this:
/var/lib/gems/1.8/gems/redis-2.2.2/lib/redis/client.rb:47:in `call': ERR unknown command (RuntimeError)
from /var/lib/gems/1.8/gems/redis-2.2.2/lib/redis.rb:841:in `set'
from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /var/lib/gems/1.8/gems/redis-2.2.2/lib/redis.rb:840:in `set'
from test_redis.rb:9
With this code:
require 'rubygems'
require 'redis'
require 'uri'
REDISTOGO_URL = "redis://localhost:6379/"
uri = URI.parse(REDISTOGO_URL)
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
REDIS.set("test", "blah")
puts REDIS.get("test")
Ruby being case sensitive, I would try to replace REDIS.SET by REDIS.set and REDIS.GET by REDIS.get. You can find the documentation of the Redis client here:
https://github.com/ezmobius/redis-rb
I have tested your example with ruby 1.8.7. (default on my Linux box).
After installing sinatra, haml, redis and hiredis gems, I have modified the code as follows:
require 'rubygems'
require 'sinatra'
require 'redis'
require 'uri'
configure do
REDISTOGO_URL = "redis://localhost:6379/"
uri = URI.parse(REDISTOGO_URL)
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
get '/' do
"Hello"
haml :index
end
post '/shorten' do
a = rand(9999)
REDIS.set(a.to_s, "http://"+params[:long])
"<pre>http://localhost:4567/get/#{a}</pre>"
end
get '/get/:url' do
redirect REDIS.get(params[:url])
end
I have added the following template in views/index.haml.
!!!
%html
%head
%title My Sinatra Website
%body
%h1 Welcome
%p
Welcome to my website made with Sinatra and HAML
%form{ :action => "/shorten", :method=>"POST" }
%fieldset
%input{ :type =>"text", :name=>"long" }
%input{ :type =>"submit" }
Once Redis is started on port 6379 and sinatra on port 4567, it works like a charm.
I suggest you check your ruby installation and try to access Redis from ruby with a simple non sinatra script.
UPDATE:
The error message is peculiar because normally, when an unknown command is sent to the server, the faulty command is provided:
ERR unknown command 'dummy'
while you just have:
ERR unknown command
Actually, this specific fix was introduced in Redis server more than 2 years ago (in December 2009) - an eternity for Redis.
https://github.com/antirez/redis/commit/2c14807b2dd5c15f1471bec32a7c6dbb077720ee
In other words, you are trying to use a very old (i.e. pre 1-3) version of Redis server with the last version of the Redis client ruby gem, which probably does not support anymore the initial protocol. You may want to compile and install a recent version of Redis server (it is easy), it should work better.

Ruby namespacing issues

I'm attempting to build a gem for interacting w/ the Yahoo Placemaker API but I'm running into an issue. When I attempt to run the following code I get:
NameError: uninitialized constant Yahoo::Placemaker::Net
from /Users/Kyle/.rvm/gems/ruby-1.9.2-p290/gems/yahoo-placemaker-0.0.1/lib/yahoo-placemaker.rb:17:in `extract'
from (irb):4
from /Users/Kyle/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
yahoo-placemaker.rb
require "yahoo-placemaker/version"
require 'json'
require 'ostruct'
require 'net/http'
module Yahoo
module Placemaker
def self.extract (text = '')
host = 'wherein.yahooapis.com'
payload = {
'documentContent' => text,
'appid' => APP_ID,
'outputType' => 'json',
'documentType' => 'text/plain'
}
req = Net::HTTP::Post.new('/v1/document')
req.body = to_url_params(payload)
response = Net::HTTP.new(host).start do |http|
http.request(req)
end
json = JSON.parse(response.body)
Yahoo::Placemaker::Result.new(json)
end
end
end
I have yet to figure out how exactly constant name resolution works in Ruby (I think the rules are a bit messy here), but from my experience it could well be that Net is looked up in the current namespace instead of the global one. Try using the fully qualified name:
::Net::HTTP::Post.new
A similar problem could occur in this line:
Yahoo::Placemaker::Result
You should replace it with either ::Yahoo::Placemaker::Result or better Result (as it lives in the current namespace).
Try requiring net/http before. Ruby is falling back to find it in the module if it isn't defined.
require 'net/http'

Can't get pluralize/singularize working with ActiveSupport::Inflector (in irb)

irb(main):001:0> require 'active_support'
=> true
irb(main):002:0> require 'active_support/inflector/inflections'
=> true
irb(main):003:0> ActiveSupport::Inflector.pluralize('test')
=> "test"
irb(main):004:0> ActiveSupport::Inflector.singularize('tests')
=> "tests"
irb(main):005:0> ActiveSupport::Inflector.titleize('hat simulator')
=> "Hat Simulator"
<ort::Inflector.tableize("america's number one hat simulator")
=> "america's number one hat simulator"
Well, basically, that's the question. It's confusing me that methods such as titleize seem to work fine, but tableize, pluralize and singularize don't.
Have I forgotten to require something?
(On a separate note, I notice this page provides examples like "post".pluralize, which when I tried, resulted in NoMethodError: undefined method 'pluralize' for "post":String. But maybe that's something to save for another question.)
Access to #pluralize without adding new methods to the String class:
require 'active_support/inflector'
ActiveSupport::Inflector.pluralize('test')
#=> "tests"
For String class:
require 'active_support/core_ext/string'
"test".pluralize
#=> "tests"
which actually calls ActiveSupport::Inflector.pluralize underneath:
def pluralize
ActiveSupport::Inflector.pluralize(self)
end

Resources