I'm trying to introduce XPath to my Ruby on Rails, Cucumber and Capybara setup for the first time. My code
row_count_on_page = page.find(:xpath, "count(//table[#id='foo']/tbody/tr)")
is resulting in error
undefined method `map' for 10.0:Float
Did you mean? tap (NoMethodError)
The actual XPath part of the code seems to be working correctly because there are exactly ten rows in the body of the table. I don't get how the code can't convert that 10.0 into a Ruby 10 though. What am I doing wrong?
You should do the count outside the method.
row_count_on_page.count
As you are currently using, after the second member of find is evaluated you will have something like:
row_count_on_page = page.find(:xpath, "10.0")
which is not a valid xpath.
Related
Currently maintaining some old Ruby server and got the following error in Log:
NoMethodError (undefined method `find_all_by_X_ID' for #<Class:0x00000005555555>):
app/controllers/some_controller.rb:10:in `buggy_function'
When viewed the faulty line in code of the buggy function is looks like this:
Hash[S.find_all_by_X_ID(TaskRun.select(:x_id).uniq.where(y_id: #y.Y_ID).map(&:x_id)).map { |s| [s.S_IDENTIFIER, s.X_ID] }]
To be frank, I'm new to Ruby, and wondering how implementing this find_all_by_X query would be the best, and why it appears as it should be automatic (as it has to do with the model component).
We're working on Ruby version 2.
It seems that find_all_by was deprecated in Rails 4 ...
Internally Rails implemented methods like find_all_by_x_id using method_missing (the method is actually defined dynamically through metaprogramming) ... but you don't have to worry about that for your use-case.
In terms of your code, if we extract the x_ids list into a variable:
x_ids = TaskRun.select(:x_id).uniq.where(y_id: #y.Y_ID).map(&:x_id)
Then you have this line that you need to rewrite:
S.find_all_by_X_ID(x_ids)
You can rewrite this as:
S.where(x_id: x_ids)
See https://stackoverflow.com/a/23921890/2981429
I'm using rails 5 with ruby 2.3.3 . Today I added a gem, there was a version conflict so I took the gem out. Since then dot notation such as hash.test no longer works. It gives NoMethodError: private method test called for {:test=>"value"}:Hash
How can I access hashes with dot notation again?
Whatever you're using to use dot-notation to access a hash is probably using method_missing to trap your dot-notation method calls. But everything has a test method because Kernel#test exists and everything includes Kernel; also, pretty much everything in Kernel is private because Kernel is where methods go that we want to pretend are functions. For example:
> 'pancakes'.test
NoMethodError: private method `test' called for "pancakes":String
I suspect that you problem is your choice of :test as hash key.
I do a lot of spatial queries that dump massive amounts of text in the form of logs every time I run queries. These slow down my programs enormously.
I'm being forced to update my rails to '4.1.2' from '4.0.0' and ActiveRecord::Base.silence has been completely deprecated as in, it doesn't work. Here's what used to work
ActiveRecord::Base.silence do
noisy_query
end
When I try this now, I get this error....
ArgumentError: wrong number of arguments (0 for 1)
from /Users/davidddouglas/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.1.2/lib/active_support/core_ext/kernel/reporting.rb:82:in `capture'
In 4.0.0 it sent a deprecation warning, and now the script just doesn't work. Oddly enough, the function is still declared, it just doesn't work anymore and expects some kind of parameter. I've tried passing in nil and got this error:
NoMethodError: undefined method `reopen' for nil:NilClass
I'm looking for a way to monkeypatch the old functionality back into my program to get my scripts to work again. Not too worried about best practices as this is an application I'm using internally with little to no front end and 0 users other than myself.
Thanks
silence moved to a core extension on logger.
From their example,
logger = Logger.new("log/development.log")
logger.silence(Logger::INFO) do
logger.debug("In space, no one can hear you scream.")
logger.info("Scream all you want, small mailman!")
end
I wish to use the capybara method drag_to in order to manually sort items on a page. Below is my code:
pos2 = find('#first_element')
target = find(#second_element)
pos2.drag_to(target)
However I get the error message:
undefined method `drag_to' for nil:NilClass (NoMethodError)
Am I invoking the method incorrectly? I am attempting to implement as defined here: http://rubydoc.info/gems/capybara/0.4.0/Capybara/Element#drag_to-instance_method
Please note I am able to use other capybara methods fill_in, visit etc without any problems...
Any help would be greatly appreciated!
#drag_to actually won't move sortable elements, since you're not moving it "to" anywhere as much as a set distance in a certain direction. Selenium implements #drag_by but it is not yet supported by Capybara.
See also:
https://github.com/jnicklas/capybara/issues/222
https://github.com/jnicklas/capybara/issues/119
I am using the Cucumber unit test tool, and am trying to retrieve the value of a textarea, however I can't get it to work.
This is my code...
page.find(:xpath, "//div[#id='process']/table/tbody/tr/td/div/textarea").value
This is the error I'm getting...
Error: undefined method `value' for nil:NilClass (NoMethodError)
This URL listed below confirms that value is the correct method to retrieve the value:
http://www.w3schools.com/jsref/dom_obj_textarea.asp
Could someone please help me fix this problem.
The nil error indicates that you're not getting the textarea -- find is returning nil. Try a simpler xpath query, or referencing the textarea by name or id.