Copy OTP and paste in the input field

Hi there, I would like to ask a workaround how do we copy a specific text in the element and paste it in the input field.

For example. I have a banner displaying “OTP has been sent. OTP 3413”.
I want to select the OTP code only and copy it then paste it in the input field.

I have my code here, but I don’t know how to copy the certain OTP code only

WebUI.doubleClick(findTestObject(‘Object Repository/Page_Login/span_OTP has been sent to your email. OTP900381’))

WebUI.getText(findTestObject(‘Object Repository/Page_Login/span_OTP has been sent to your email. OTP900381’))

WebUI.sendKeys(findTestObject(‘Object Repository/Page_Login/span_OTP has been sent to your email. OTP900381’), Keys.chord(
Keys.CONTROL, ‘c’))

WebUI.click(findTestObject(‘Object Repository/Page_Login/Verify_OTP/input_Forgot Password_WebPatterns_wt18blockwtUsernamewtOTP_Input’))

WebUI.sendKeys(findTestObject(‘Object Repository/Page_Login/Verify_OTP/input_Forgot Password_WebPatterns_wt18blockwtUsernamewtOTP_Input’),
Keys.chord(Keys.CONTROL, ‘v’))

Can anyone having the experience with this help?

1 Like

The below tutorial was released a few days ago. Have a read of it.

[Demo & Integration] Extract OTP (one-time password) with Katalon - Product Forums / Tips & Tricks - Katalon Community

1 Like

If this is the actual message type, then you could do something like:

msg = WebUI.getText(findTestObject('Page_Login/span_OTP has been sent to your email. OTP900381')) 
"break up the message into individual words"
String[] arrOfStr = msg.split(" ")
"get the last word, our OTP"
String oneTimePswd = arrOfStr[arrOfStr.length - 1]
WebUI.comment("The One Time Password is ${oneTimePswd}")

WebUI.sendKeys(findTestObject('Page_Login/Verify_OTP/input_Forgot Password_WebPatterns_wt18blockwtUsernamewtOTP_Input'), "${oneTimePswd}")

Edit: If the OTP is always the same length, then you could use:

msg = WebUI.getText(findTestObject('Page_Login/span_OTP has been sent to your email. OTP900381'))   //"OTP has been sent. OTP 3413"
"get the last 4 characters of the message"
String oneTimePswd = msg.substring(msg.length() - 4)
WebUI.comment("The One Time Password is ${oneTimePswd}")

WebUI.sendKeys(findTestObject('Page_Login/Verify_OTP/input_Forgot Password_WebPatterns_wt18blockwtUsernamewtOTP_Input'), oneTimePswd)
2 Likes

Thanks @grylion54. this looks good.
And I just found another way to get the OTP.

I store the the element, then extracted the OTP using this code.

WebElement element = WebUiCommonHelper.findWebElement(findTestObject(‘Object Repository/Page_Login/Verify_OTP/Alert_OTPSent’), 30)

GetOTP = WebUI.executeJavaScript(‘var txt = arguments[0].innerText; var numb = txt.replace(/[^0-9]/g, ''); return numb;’, Arrays.asList(element))

WebUI.setText(findTestObject(‘Object Repository/Page_Login/Verify_OTP/input_OTP_WebPatterns_wt18blockwtUsernamewtOTP_Input’),
GetOTP)

Hi there @john.colis,

Welcome to Katalon Community! :wave:

If grylion’s comment, or even the alternative workaround that you mentioned above, did help you to answer your question, then it would be great if you could mark his, or your, reply as a solution :white_check_mark: by following the video below…

This helps other members who may also be asking the same questions to know where to find the answer they need (especially in a long thread).

Thanks! :smile:

1 Like