How to pass variables to page.executescript() in capybara - ruby

I need to pass the variable in the javascript to be executed through excute_script method in capybara.
I am unable to pass variable to it.
Please anyone help me.
Example:
#idd="sample"
txt=page.execute_script('var user_id = ${#idd}; return user_id;')
puts txt
I am expecting the text sample to be printed but i'm getting java script error.

I think the problem is with ${}; you have to use #{}; try with:
page.execute_script("var user_id = '#{#idd}'; return user_id;")

Related

Ruby string interpolation not working when called from an included file

I have two files: urls.rb and testcase.rb. urls.rb has the following:
$event = "/sendmessage/#{id}"
The id in the url is generated in testcase.rb. This is the code in testcase.rb:
require 'urls.rb'
id = 100
puts $event
I am seeing the following error:
undefined local variable or method `id' for main:Object (NameError)
How do I resolve this error?
Local variables like id cannot be called across files. Their scopes are limited within a file. In order to use it, you have to assign id in the same file as it is used. Furthermore, your assignment of id after requiring 'urls.rb' is meaningless.
The problem is that you are attempting to use id in urls.rb before it has been defined by testcase.rb. Your code is functionally equivalent to the following:
$event = "/sendmessage/#{id}"
id = 100
puts $event
This does not work because you are attempting to call id before it has been defined. You must define the variable first:
id = 100
$event = "/sendmessage/#{id}"
puts $event
Here it works because you have defined id before you're trying to call it.
When troubleshooting a problem like this, consider how it would have to work if you were not separating it into different files.
So without knowing more about what you're trying to accomplish, just make sure you have defined a variable before you try to call it.

Can't pass the variable to subject in email laravel 5

Mail::send('emails.cont',['name'=>$n,'email'=>$email],function($message){
$message->to('abc#gmail.com','efe')->from('cde#gmail.com')->subject($s);
});
There for the subject, I am trying to pass a variable called $s which contains a value which defined there. But, it will underlying in red and saying undefined variable called $s. How to solve that?
can you please try the following code
Mail::send('emails.cont',['name'=>$n,'email'=>$email],function($message) use ( $s ) {
$message->to('abc#gmail.com','efe')->from('cde#gmail.com')->subject($s);
});

Magento registry variables in controller

I have stored a variable in register by Mage::register('captcha', $var); in helper. And in the controller i tried to retrieve the variable by using Mage::registry('captcha'); But i dont getting any values here. Please help me to solve this.
In your helper file create a function like below :
public function getCaptcha(){
$var = 'myValue123';
Mage::register('varun', $var);
return Mage::registry('varun');
}
In your controller function:
$registryValue = Mage::helper('yourModule')->getCaptcha();
echo registryValue ; //prints myValue123
Hope it helps !!!
It's look like syntax is right.
Please first try to set some static value like $var="test"
Mage::register('captcha', $var);
after that got this value in controller.
Mage::registry('captcha');
if you got this value test then i think you have problem with $var in your helper.
Let me know if you have any problem
'captcha' is already in use, so magento never set your data in registry. Change the name, for example 'captcha1'
Mage::register('captcha1', $var);

How to pass special parameters in a function in Ruby Selenium Webdriver

I'm using Selenium Webdriver Ruby to create a function in order to wait for an element displayed within a Time that I set in this function
My function will have two parameters (one Optional parameter & one parameter is locator name of an object)
So, I have my function with two parameters like below:
def wait_for_element_displayed(locator, options={})
if options[:time]
settime = options[:time]
else
settime= 60
end
#driver.find_element(locator).click
end
So, I will call this function like: wait_for_element_displayed(:id=>"unique1", :time => "20")
However, I got the error when trying to calling this function with this way, the error is: "TypeError: can't convert String into an exact number".
Anybody please help me how to pass two parameters like my scenario to a function? Any help appreciated.
A few suggestions:
You can use hash#fetch to set a default value if the key you are looking up is not present:
time = options.fetch(:time) { 60 }
Ruby will have a hard time parsing your input when both arguments are hashes and you don't differentiate between them. Also, your error makes me think you should provide 20 as an integer instead of a string. Try calling the method like this:
wait_for_element_displayed({:id => "unique1"}, { :time => 20 })
Lastly, I don't know much about Selenium, so maybe someone can fill me in on the last point. Is setting a settime variable sufficient to configure timeouts?

How to create XML object from string using xml-mapping in Ruby

I'm using xml-mapping in Ruby (on Sinatra) for some XML stuff. Generally I follow this tutorial: http://xml-mapping.rubyforge.org/. I can create objects and write them to XML strings using
login.save_to_xml.to_s
But when I try
login = Login.load_from_xml(xml_string)
I get the following error:
XML::MappingError - no value, and no default value: Attribute username not set (XXPathError: path not found: username):
Here is the XML string I receive:
<login><username>ali</username><password>baba</password></login>
This is what the class looks like:
class Login
include XML::Mapping
text_node :username, "username"
text_node :password, "password"
end
So the class name is the same, the nodes are named the same. I actually get the exact same string when I create an instance of my object and fill it with ali/baba:
test = Login.new
test.username = "ali"
test.password = "baba"
p test.save_to_xml.to_s
<login><username>ali</username><password>baba</password></login>
What am I missing?
Thanks,
MrB
EDIT:
When I do
test = login.save_to_xml
And then
login = Login.load_from_xml(test)
it works. So the problem seems to be that I'm passing a string, while the method is expecting.. well, something else. There is definitely a load_from_xml(string) method in the rubydocs, so not sure what to pass here. I guess I need some kind of reverse to_s?
It looks like you save_to_xml creates a REXML::Element. Since that works, you may want to try:
Login.load_from_xml(REXML::Document.new(xml_string).root)
See the section on "choice_node" for a more detailed example http://xml-mapping.rubyforge.org/

Resources