Getting text from a form and evaluate in selenium using ruby - ruby

I am testing a registration form and one of the questions before submitting is:
"What is 8 + 4?"
The values will be different every time. I am using Selenium 2 with Ruby and want to to see if anyone knows how to a) get the text from the question and then b) compute the addition and return the correct answer in the field.
Thanks!

Using ruby code try to get the values in between is and ? Store this in Variable and now split the text with deliminator + now you will get the Values in array in different location. Now you can add these two and use for your evaluation. I don't have idea about ruby but its possible in PHP as I have done the same. Its should be in Ruby too.
Getting text between is and ? can be done strpos() in php that gives the position of the string see similar function for ruby.

Related

How to store dynamic value and reuse in Webdriver Ruby test

I'm writing a ruby webdriver test that needs to store a dynamic value such as an order ID to use later on in the text. I think I need to extract the value from the string and then store it as a variable to call for future use.
The string looks like this and I just need to extract/store the numeric value.
<span class="receiptNum hidden-xs">Receipt #: 12303430</span>
Any tips or examples on how to extract that value and create a variable for future use would be great!
To extract the text (only numbers) out of the this element, try using the following code:
#numbers = #driver.find_element(:css=>'receiptNum').text.scan(\d+)
Currently I am saving this number in an instance variable which can be used again in the same test as it will flow around with the test object till the test finishes.
Other option include saving it in a temp txt file and reading from it when required.
Note: Fetching data now and using it later is not a good practice, try not to use this very frequently.
Hope it helps!!!

Mockaroo Formulas for random date range using Ruby

I am trying to create a data field using Mockaroo and they say they have Ruby support but I know nothing about Ruby so I am trying to find out how to do a field that will randomly choose between the 3 options.
now() or
now()+days(-1) or
now()+days(-2) or
now()+days(-3)
Idea 1
I was initially thinking something using random like now()+days(this.rand(-3))
Idea 2
I also thought of using or logic like now() or now()+days(-3) or etc...
This answer may end up being different than a typical ruby solution since Mockaroo has their own little API to use too... Appreciate any help that can be given.
Turns out I had to use the random function first and pass in min and max date parameters.
random(now()+days(2), now()+days(-3))

activerecord update wrong number of arguments

Sorry if this is a simple problem that has been explained before. I've done some research about my problem. I'm completely new to ruby and active record and I find the examples other have had with the wrong number of arguments too complicated for me to follow. So here is my simple one.
I'm trying to do a simple update using activerecord to a db. All I'm trying to do add a value to the title attribute that I left as nil when I created it in the first place.
vertigo is the variable I assigned using the .find method.
I'm typing in vertigo.update(title: 'Vertigo')
But I'm getting an error message saying
wrong number of arguments (1 for 2).
Here is more of session. I'm using Sinatra-tux >> vertigo = Movie.all
D, [2015-04-20T11:11:38.890714 #3741] DEBUG -- : Movie Load (0.4ms) SELECT "movies".* FROM "movies"
=> #]>
vertigo.update title: "Vertigo"
ArgumentError: wrong number of arguments (1 for 2)
/home/michael/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.0.4/lib/active_record/relation.rb:330:in update'
(ripl):4:in'
find can return an array. It's entirely possible that your vertigo variable is actually an array.
Try to run
vertigo[0].update(title: 'Vertigo')
assuming you only want to change the first one.

How to trim text and use it as parameter for next step in watir using ruby

This may be very simple question but I am very new to ruby or any programming language. I want to trim some text and use it as parameter for next step. Can any one please write me code for doing this. I am testing a web application which is used in financial domain. I need to use the cvv2 and expiry date of card number which is generated in next step as parameter. The text which gets displayed on html is
CVV2 - 657  Expiry - 05/12 (mm/yy)
Now from the above text I should some how get only '657' and '0512' as value to use is in next step.
Request for urgent assistance.
If all your strings will be formatted like this, I suggest using regexp, using String#gsub. There's lots of places to learn about regexp if you don't already, and rubular allows you to test it in your browser, with a short cheat sheet.
To do it quickly you could use
card_details = "CVV2 - 657 Expiry - 05/12 (mm/yy)"
card_details = card_details.scan(/\d{2,}/)
cvv2 = card_details[0]
expiry = card_details[1] + card_details[2]
Probably better ways of doing it as I'm no expert, but you said urgent, so.
For getting the text out of the cell you could try (I don't use the original watir anymore, so I might not be able to remember this):
card_details = browser.td(:text => /CVV2/).text
If that doesn't work give this a try (actually on second thought TRY THIS ONE FIRST)
card_details = browser.cell(:text => /CVV2/).text
For these examples I'm assuming your browser object is called "browser".
We can use regular expression to achieve the same,
> "CVV2 - 657 Expiry - 05/12 (mm/yy)".match(/\d{3}/)
=> "657"
>"CVV2 - 657 Expiry - 05/12 (mm/yy)".match(/\d+\/\d+/)
=> "05/12"

how to Clear or replace contents of input field using selenium?

How can i clear the contents of an input field using ruby selenium?
i.e when page loads up there are values in the input filed but i want to replace them with new ones.
thank you
Come on guys... You can't be serious...
With selenium to clear an input field do this: selenium.type("") in ruby too.
And backspacing as many times as there are letters... Come on.. you don't do stuff like that.
Or in Ruby syntax: #browser.type "idofthefield", ""
This is how i cleared and replaced with new text in my Ruby based web portal test
#driver.find_element(:xpath, "//input[#name='ip-address']").clear
#driver.find_element(:xpath, "//input[#name='ip-address']").send_keys(new_ip_address)

Resources