Capybara - overriding an attribute on the node - ruby

Lets say I successfully retrieve some Capybara node:
node = find("#some-element")
The element has some nasty attribute that I just want to ignore or override for the context of the given test:
node[:onClick] # "someInlineJSCrap(); return false;"
Is it possible to override an attribute on the Capybara node? I've tried ruby hash assignment syntax with no luck;
node[:onClick] = "return true;"
Edit (full error):
undefined method `[]=' for # <Capybara::Element tag="a"> (NoMethodError)
Any help would be appreciated!

i don't know if that's even possible, but i think that i have a workaround for you.
you can use page.execute_script to change the onClick value of your node via JavaScript.
that would be something like page.execute_script("$('#some-element').attr('onClick', 'return true;')")

Related

Selenium search bar protected ? " undefined local element or method"

I am using selenium trying to send a key to a search bar.
This is the code of the search bar.
<input type="text" value="XXXXXXX" onblur="ga('send', 'event',
'Search', 'Champ_de_recherche');" name="champs" id="input_search" aria-
label="Recherche XXX.com" autocapitalize="none" autocorrect="off">
I am unable to locate it with selenium using name / css / class or id selector
element = element.find_element(:id, "input_search")
element = element.find_element(:name, "champs")
both are returning "undefined local variable or method `element' for main:Object (NameError)"
any guess ?
I think it looks like an issue with your code, e.g.:
element = element.find_element
More specifically this bit:
element.find_element
Shouldn't that be:
browser.find_element
or
driver.find_element
(or similar) ?
It's saying that you don't have element defined before attempting to call those methods. If you aren't trying to find a child element from an element, use the find_element method off of your driver instance

Clear input field: undefined method `clear' for #<Watir::Input:XYZ> (NoMethodError)

I am not sure why I can not clear my input field.
PageObject:
element(:test_input_field) { |b| b.input(class: "search-field") }
def set_search_value(search_entry)
test_input_field.when_present.clear
test_input_field.when_present.set(search_entry)
end
Step_file:
page.set_search_value(search_entry)
Output:
undefined method `clear' for #'<'Watir::Input:0x00000003980d20'>' (NoMethodError)
The clear (and set) method are not defined for generic input elements - ie Watir::Input. They are only defined for the specific input types - text field, checkbox, etc.
To make the code work, you would need to convert the input into the more specific type, which is likely a text field. You can do this using the to_subtype method:
test_input_field.when_present.to_subtype.clear
test_input_field.when_present.to_subtype.set(search_entry)
As #SaurabhGaur mentions, set already starts by clearing the existing value, so you could just do:
test_input_field.when_present.to_subtype.set(search_entry)
Unless the input type changes, it would make more sense to define the element as a text_field so you do not need to convert it. It might depend on which page object library you are using, but I would expect you could do:
element(:test_input_field) { |b| b.text_field(class: "search-field") }
def set_search_value(search_entry)
test_input_field.when_present.clear
test_input_field.when_present.set(search_entry)
end

Set div value of WebElement with Selenium Webdriver

I want to set new value of div element by using Selenium FirefoxDriver in Java:
<div my-div-name="lastname">
<p>Smith</p>
</div>
I have successfully retrieved the div element (as WebElement) by use of XPATH expression and have also been able to get current value Smith by use of getText() method. However, there is no setText() method of an WebElement. So I have in stead tried to execute JavaScript:
driver.executeScript("arguments[0].value = 'Foo Bar'", element);
but nothing happens. New getText() call still returns Smith.
Any tip on how to set the value successfully?
Solution is to set innerHTML property like this:
driver.executeScript("arguments[0].innerHTML = arguments[1]", element, text);
I have tried to do this several times, but i wrote innerHtml and not innerHTML so be aware of casing when setting the property.

evaluate string to modify data model in ruby

I seem to be running into a problem with Rails 3 and I can't seem to figure it out.
Here's what I am trying to do:
att1 = "column"
att2 = "1"
final_column = "#{att1}_#{att2}"
obj.final_column = 4
====> Error
-----> NoMethodError: undefined method `final_column=' for class....
If I do this it works though:
obj.column1=4
What can I do to my final_column to make it work?
Thanks!
You want to do this:
obj.send("#{final_column}=", 4)
If you want to respect the private/protected visibliy, use public_send instead of send.

Nokogiri Error: undefined method `radiobutton_with' - Why?

I try to access a form using mechanize (Ruby).
On my form I have a gorup of Radiobuttons.
So I want to check one of them.
I wrote:
target_form = (page/:form).find{ |elem| elem['id'] == 'formid'}
target_form.radiobutton_with(:name => "radiobuttonname")[2].check
In this line I want to check the radiobutton with the value of 2.
But in this line, I get an error:
: undefined method `radiobutton_with' for #<Nokogiri::XML::Element:0x9b86ea> (NoMethodError)
The problem occured because using a Mechanize page as a Nokogiri document (by calling the / method, or search, or xpath, etc.) returns Nokogiri elements, not Mechanize elements with their special methods.
As noted in the comments, you can be sure to get a Mechanize::Form by using the form_with method to find your form instead.
Sometimes, however, you can find the element you want with Nokogiri but not with Mechanize. For example, consider a page with a <select> element that is not inside a <form>. Since there is no form, you can't use the Mechanize field_with method to find the select and get a Mechanize::Form::SelectList instance.
If you have a Nokogiri element and you want the Mechanize equivalent, you can create it by passing the Nokogiri element to the constructor. For example:
sel = Mechanize::Form::SelectList.new( page.at_xpath('//select[#name="city"]') )
In your case where you had a Nokogiri::XML::Element and wanted a Mechanize::Form:
# Find the xml element
target_form = (page/:form).find{ |elem| elem['id'] == 'formid'}
target_form = Mechanize::Form.new( target_form )
P.S. The first line above is more simply achieved by target_form = page.at_css('#formid').

Resources