简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM