简体   繁体   中英

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"

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