Capture OTP while Sign in/Sign up

@Chris_Trevarthen
I have attached the Log Viewer of the executed test case. The newly created object does not get identified to capture otp.

Kindly help out!

Thanks in advance.
N.Aishwarya Janani

Hi @janani0515,

I think we can change your xpath to make it look at the text itself instead of trying to find by the full path. So if you change the xpath for your OTP/Capture element to:

//*[@class = 'android.widget.TextView' and contains(@text, 'gin OTP')]

Where gin OTP is that part of the notification that you can see in your screenshots.

Can you try that and see if you are able to get the text out of the notification?

– Chris

@Chris_Trevarthen

Thank You. I tried but still no luck. One doubt that arises is every time my otp gets generated , the number of the OTP changes. If I do not mention that number, will the Katalon identify it?

Sample xpath of the otp that was captured earlier,

//*[@class = ‘android.widget.TextView’ and @instance = ‘1’ and (text() = ‘…gin OTP : 818099’ or . = '…gin OTP : 818099 ') and @resource-id = ‘android:id/text’]

Xpath of my manually created object while running test case as I may not know what otp number would come when I login the application :
//*[@class = ‘android.widget.TextView’ and (text() = '…gin OTP : ') and @resource-id = ‘android:id/text’]

Output:

Hi @janani0515

Since your OTP will change, I recommend trying the contains() function of path, so based on your manually created object, it could be:

//*[@class = ‘android.widget.TextView’ and contains(@text, '…gin OTP : ') and @resource-id = ‘android:id/text’]

Once you get the text of the object, you can use some string functions to pull out the OTP:

String notificationText = Mobile.getText(notification, 10)
String otp = notificationText.reverse().take(6).reverse()

Hope this helps,

Chris

Hi @Chris_Trevarthen,

Finally my manually created object has been detected successfully but when I try to set the text to the required field, it throws error saying ‘object is null’.

Hi,
You miss the folder name
findTestObject('Login/android.widget.Button1 - Sign In')

Hi @HeleneB
Thanks for that! Even then I am unable to set text since it keeps loading

Did you try with a timeout ?
Mobile.setText(findTestObject('Login/android.widget.Button1 - Sign In'), code, 10)

Hi @janani0515,

Based on the name of your element, android.widget.Button1 are you sure it’s a field that can have text set in it? Is it possible that the captured object isn’t the form field itself but rather the submit button?

– Chris

Hi @Chris_Trevarthen,
Thanks for that. Yes I did change my object name of my text field where the text has to be entered… I have finally succeeded until text capture part like you said by modifying the object properties.

Now my text captured is : App Login OTP : 456738

But when I use your code, the Katalon is unable to fetch the required number alone.

String notificationText = Mobile.getText(notification, 10)

Here my string notificationText contains this message :
App Login OTP : 456738

String otp = notificationText.reverse().take(6).reverse()

The above line does not extract the six digit number and gives me a null value.

Please assist me.

Thanks in advance.

Hi @janani0515,

Could you also try the drop() string function to remove all the non-necessary text?

String otp = notificationText.drop(16)

Finally, if that doesn’t work, you can try substring(), which grabs from the 17th character to the 22nd character (my math might be off at the end, so you might have to try 21 or 23 if 22 doesn’t work):

String otp = notificationText.substring(16, 22)

– Chris

Hi @Chris_Trevarthen,

Neither of the below commands work.
String otp = notificationText.drop(16)
(or)
String otp = notificationText.drop(16)

Is there any other alternative syntax?

Hi @janani0515,

When you say that the commands don’t work, are they showing an error or is it just not giving you the text you expect?

I’ve tried that code in a sample test that I wrote and it appears to be working. Could you try adding some logging to see what is actually happening?

You can do this with something like this after you get the text from the Test Object:

KeywordUtil.logInfo("original text: " + notificationText)

and then after you set the otp value:

KeywordUtil.logInfo("new otp text: " + otp)

These values should show as lines in the Log Viewer. You can expand the logInfo lines in the viewer to see the output.

Please let me know what you see when you do that.

Thanks,

Chris

Hi @Chris_Trevarthen,

To say thank you is just not enough for guiding me throughout my learning journey since day one. I have finally succeeded in capturing OTP in my application and I proceeded with your drop string operation only. I have given the object location of the ‘Set text’ incorrectly hence it failed earlier.
Now, it did extract my required OTP number. Thanks alot for your patience and time in paying attention to all of my minute issues.

Thanks alot once agin!!

Regards,
N.Aishwarya Janani

1 Like

@janani0515 That’s great news! I’m so glad it’s working for you.

Please let me know if you have any other questions.

– Chris

Hi Janani
Can you send me the code lines which you have followed to read auto otp

Hi Chris
Why App is not reading auto OTP when running script from katalon
If I install directly then auto read OTP is working
Can you give me solution

Hi @Prakash,

Can you confirm that your are receiving the OTP notification when you run from the script?

Is the Notifications window is being opened when you run the test script through Katalon?

Are you seeing any errors in the logs?

– Chris

Hi @Chris_Trevarthen,

Can you confirm that your are receiving the OTP notification when you run from the script? - Yes Im receiving the OTP

Is the Notifications window is being opened when you run the test script through Katalon? _ yes Its Opening

Are you seeing any errors in the logs? - No Errors

Hi @Prakash,

Can you share the properties of the Test Object for the OTP text?

– Chris