anagraj
anagraj

Reputation: 109

how to read a value and pass it as parameter to next step in watir

I am new to watir, and Need to know how to capture a value displayed on browser and pass the same as parameter to next step. here is how the html looks like.

Text -   xxxx yyyy zzzz aaaa

The text displayed is a card number. I need to capture the card number dipslayed and pass it as parameter to next step in my test

Upvotes: 1

Views: 491

Answers (2)

adam reed
adam reed

Reputation: 2024

If this cardnumber is contained in an HTML element like a div/span/table cell, then use that to locate the bit of text you need.

For example if your HTML was like this:

<div class="CC_Number">1234 5678 9012 3456</div>

Then you could use code like this:

cardnumber = browser.div(:class => "CC_Number").text
other_function(cardnumber)

You can also use regular expressions or other string manipulation to separate the number from other text on the page.

"Your credit card number is: 1234 5678 9012 3456."

cardnumber = browser.text              #get all browser text
     #"Your credit card number is: 1234 5678 9012 3456."
cardnumber = cardnumber.split(":")     #split the text at every colon
     #[0] = "Your credit card number is"
     #[1] = "1234 5678 9012 3456."
cardnumber[1].gsub(".", "")            #replace all instances of "." with ""
     #cardnumber = "1234 5678 9012 3456"

for an example of a regular expression to find the card number, see @Dave McNulla's answer below.

Upvotes: 2

Dave McNulla
Dave McNulla

Reputation: 2016

cardnumber = browser.text.scan /\d{4} \d{4} \d{4} \d{4}/
other_function(cardnumber)

Upvotes: 4

Related Questions