# Capture OTP while Sign in/Sign up

**URL:** <https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829>\
**Category:** Mobile Testing\
**Tags:** mobile-testing, katalon-studio\
**Created:** [February 4, 2019, 1:04pm UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829 "2019-02-04T13:04:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nazneen](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nazneen](https://forum.katalon.com/u/nazneen)\
**Post date:** [February 4, 2019, 1:04pm UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/1 "2019-02-04T13:04:02Z")

</div>

How to capture otp from android mobile during registration/login in a app

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [February 6, 2019, 7:04pm UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/2 "2019-02-06T19:04:50Z")

</div>

Hi @nazneen,

Check out this post and video for a walkthrough on how to capture One Time Password (OTP):

> [@How to read OTP (one time password) from mobile phone or how to read and message from mobile phone](http://forum.katalon.com/t/how-to-read-otp-one-time-password-from-mobile-phone-or-how-to-read-and-message-from-mobile-phone/13607/3):
>
> from this video smile

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [June 10, 2019, 10:51am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/3 "2019-06-10T10:51:04Z")

</div>

@Chris_Trevarthen Could you please help me with the same OTP reading when I try to sign in using android mobile app ?

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [June 11, 2019, 11:43am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/4 "2019-06-11T11:43:02Z")

</div>

Hi @janani0515,

I haven’t worked with OTP testing, but if you are able to share screenshots or videos of the process, I might be able to help figure out how to inspect some elements.

– Chris

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [June 13, 2019, 8:23am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/5 "2019-06-13T08:23:26Z")

</div>

Hi @Chris_Trevarthen,

As soon as I enter my login credentials, the OTP will get displayed as a notification. It will also be in my ‘Text Messages’ folder.  
I need to read this otp from either notification or sms folder and enter it to login my application. There are several ways to do it for a web application but for a mobile app there are no procedures.

Please help me.  
Refer attached images.

 ![otp](https://europe1.discourse-cdn.com/katalon/original/2X/d/d48c6d8f5d40e6ce67006d3d1ba59d3ca6060024.jpeg)

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [June 17, 2019, 1:54am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/6 "2019-06-17T01:54:08Z")

</div>

Hi @janani0515,

Thanks for the screenshot, that helps me understand a bit better what you’re trying to do. I don’t think you’ll be able to access the notification popup message with Katalon Studio/Appium, but I think you might be able to switch to the Notifications application and read the latest notification to get the OTP code.

1. Use Katalon Studio to Spy Mobile using an apk file that you have on your machine
2. Trigger the OTP notification on your emulator or device
3. In the emulator or device, switch to the Android Notifications app
4. Capture the object for the corresponding notification message in the Notifications app - this is probably the latest one - name this something like “First Notification Item”
5. Capture the text object for the notification that contains the OTP - name this something like “OTP Text”
6. Save the text object in the Test Object repository
7. Stop the spying process
8. Open your test case
9. Switch to the “Script” view of the test case
10. After the spot in your test case that you trigger the OTP request and the notification appears, add a line for:  
`Mobile.openNotifications()`  
This should open the Android Notifications app programmatically
11. Add code to tap the latest notification object, which should be something like:

```auto
TestObject notification = findTestObject('Object Repository/First Notification Item')
Mobile.tap(notification, 10)

```

Where “First Notification Item” is what you named the captured first notification element when you were spying
12. Add code to get the text of the OTP:

```auto
TestObject otp = findTextObject('Object Repository/OTP Text')
String otpText = Mobile.getText(otp, 10)

```

13. You can then switch back to the main app using  
`Mobile.closeNotifications()`
14. Use the OTP text you captured above and set it in to the password field in your app using `Mobile.sendKeys()` or `Mobile.setText()`.

It’s a bit of a round-about process, but I think the concept should work for what you want to do.

Hope this helps,

Chris

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [June 26, 2019, 7:31am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/7 "2019-06-26T07:31:02Z")

</div>

@Chris_Trevarthen  
Thanks for the detailed explanation. But still I get struck in between the process.Whenever I start recording my actions, the notification pops up, but I am unable to capture the notification screen.The whole layout of my screen gets captured and I cannot capture the OTP notification section alone.

Kindly help me out with how can I switch to Android notifications app as the back button will not help moving back to my mobile screen.

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [July 10, 2019, 5:15am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/8 "2019-07-10T05:15:54Z")

</div>

@Chris_Trevarthen  
Hi Chris! Any updates on the above issue?

Thanks in Advance.

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [July 11, 2019, 3:22am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/9 "2019-07-11T03:22:52Z")

</div>

Hi @janani0515,

I’m not sure if you’ll be able to capture the notification as it pops up, but if you add the line `Mobile.openNotifications()` to your test, it should pull down the Notifications drawer.

From there, you’ll need to find the notification object that you want to grab the OTP from (this is probably the latest/top notification). To do this, I [manually created a test object in the Object Repository](https://docs.katalon.com/katalon-studio/docs/manage-test-object.html#create-a-test-object) with the name “notification”. It has an `xpath` value of:

`//*[@class = 'android.widget.FrameLayout' and @resource-id = 'android:id/status_bar_latest_event_content'][1]/*[@class='android.widget.LinearLayout' and @resource-id='android:id/notification_main_column']/*[@class='android.widget.TextView' and @resource-id='android:id/text']`

That xpath gets the first notification object, then the `LinearLayout` within that, and finally the `TextView` within that.

In my test, I can then find that Test Object and inspect its contents.

All put together, it’s like this:

```auto
// Do the work to trigger a OTP

Mobile.openNotifications()
TestObject notification = findTestObject('Object Repository/notification')
String otp = Mobile.getText(notification, 10)
Mobile.closeNotifications()

// Use the otp String in the appropriate place

```

Hope this helps,

Chris

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [July 12, 2019, 7:04am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/10 "2019-07-12T07:04:00Z")

</div>

@Chris_Trevarthen  
Hi Chris,Thanks for the detailed insight.I did the above by creating an object with Xpath value, but when Katalon executes the following step,

**String otp = Mobile.getText(notification, 10)**

it keeps loading and would not give me any status so I am unable to find out what is the error.

Also you have used resource id value in the xpath:  
_[@class = ‘android.widget.FrameLayout’ and @resource-id = **‘android:id/status\_bar\_latest\_event\_content’][1]**/_[@class=‘android.widget.LinearLayout’ and **@resource-id=‘android:id/notification\_main\_column’]**/\*[@class=‘android.widget.TextView’ and @resource-id=‘android:id/text’]

In my object property, only the class Textview has resource id whereas the other two classes FrameLayout and LinearLayout does not have any resource id.

Is resource id must when creating xpath or can any other alternative id be added?

Could you please share your thoughts?

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [July 12, 2019, 4:08pm UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/11 "2019-07-12T16:08:44Z")

</div>

Hi @janani0515,

The `resource-id` isn’t strictly necessary, but it helps to narrow down exactly which element that Appium should look for. Since there are so many `LinearLayout` or `TextView` objects on a given screen, it’s unlikely that just putting the classes will return the element you want.

To try to find a good xpath for you to use, let’s go through some steps/questions:

1. Are you able to use `Mobile.showNotifications()` in your test to open the Android Notifications widget?
2. Can you start the Spy Mobile feature of Katalon with your app and then slide open the Android Notifications widget manually (not the notification as it comes in, but the list of all notifications you’ve received on the device)? If so, can you send a screenshot of what the notifications look like? I’d imagine it looks something like this:

 ![15%20AM](https://europe1.discourse-cdn.com/katalon/original/2X/7/7b9f7ca75e336b8189c3e52494376652ecf6f7c8.png)

1. Using Spy Mobile, can you find the OTP text in the Android Notification widget and click the checkbox next to the `TextView` element (I assume it’s a `TextView`). This should be similar to the screenshot I took above of some notification text.
2. Once the checkbox is selected, you should see all of the properties of that text element. Can you copy and share with me the full `xpath` value of the element?

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [July 22, 2019, 10:25am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/12 "2019-07-22T10:25:38Z")

</div>

@Chris_Trevarthen

Thank alot for the explanation. I have attached screenshot for your reference. The following are the captured elements.

1. **Frame Layout**

 ![FL](https://europe1.discourse-cdn.com/katalon/original/2X/d/dae9da99caceb7c7e64bb51bd99f9cc2662eb80a.jpeg)

**2.Linear Layout**

 ![LL](https://europe1.discourse-cdn.com/katalon/original/2X/4/44dc41872b8d71f3d6b393c12201524348b04464.jpeg)

**3.Text View**

 ![TV](https://europe1.discourse-cdn.com/katalon/original/2X/7/7e4ff94cc3fe800df3b18e2a7fb31340b9e462c7.jpeg)

The Xpaths of the captured elements are as follows:

**1.Frame Layout:**  
//hierarchy/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.ScrollView[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]

 ![FrameLayout](https://europe1.discourse-cdn.com/katalon/original/2X/4/46e087a3dd6f541fbb7842ee0b6324853b510e52.png)

**2.Linear Layout:**  
//hierarchy/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.ScrollView[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]

 ![LinearLayout](https://europe1.discourse-cdn.com/katalon/original/2X/0/0ede77009944f624a557e8369904b8829e4874a9.jpeg)

**3.TextView:**  
//hierarchy/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.ScrollView[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.TextView[1]

 ![TextView](https://europe1.discourse-cdn.com/katalon/original/2X/1/1005407a3b31d85fa94837a5ffc271351ca9551f.jpeg)

**Manual Object Creation:**  
I have manually created an object and set xpath value as per the guidance.

 ![Manual](https://europe1.discourse-cdn.com/katalon/original/2X/7/7ea1ee42feaf2cca83f1d5ae774dbdfda1296a68.jpeg)

**My code snippet:**

 ![Code](https://europe1.discourse-cdn.com/katalon/original/2X/8/802c66cbd0acfc3cf540b10e79bee97d91d5778a.jpeg)

When I run my test case, the line , **TestObject notification = findTestObject(‘OTP/Capture’)** , does not get executed and the line gets skipped.

Kindly guide me on what goes wrong!

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [July 24, 2019, 8:03am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/13 "2019-07-24T08:03:56Z")

</div>

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

 ![error](https://europe1.discourse-cdn.com/katalon/original/2X/c/c66d10634b8941d94825bc38d6a00cd90d6fc3d2.jpeg)

Kindly help out!

Thanks in advance.  
N.Aishwarya Janani

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [July 25, 2019, 2:47am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/14 "2019-07-25T02:47:44Z")

</div>

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:

```auto
//*[@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

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [July 25, 2019, 6:53am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/15 "2019-07-25T06:53:36Z")

</div>

@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:

 ![error](https://europe1.discourse-cdn.com/katalon/original/2X/0/0093ad7080f1783faff262226f67d2e53dfe2611.jpeg)

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [July 26, 2019, 2:18am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/16 "2019-07-26T02:18:48Z")

</div>

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:

```auto
//*[@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:

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

```

Hope this helps,

Chris

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [July 29, 2019, 7:42am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/17 "2019-07-29T07:42:10Z")

</div>

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’.

 ![First](https://europe1.discourse-cdn.com/katalon/original/2X/5/5fb90e3451c0a41fd474529f72c867021b69fbb0.jpeg)

---

<div class="post-metadata">

**Author:** ![HeleneB](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/heleneb/32/2775_2.png) [@HeleneB](https://forum.katalon.com/u/HeleneB)\
**Post date:** [July 29, 2019, 10:02am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/18 "2019-07-29T10:02:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![janani0515](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@janani0515](https://forum.katalon.com/u/janani0515)\
**Post date:** [July 29, 2019, 10:23am UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/19 "2019-07-29T10:23:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![HeleneB](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/heleneb/32/2775_2.png) [@HeleneB](https://forum.katalon.com/u/HeleneB)\
**Post date:** [July 29, 2019, 12:02pm UTC](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829/20 "2019-07-29T12:02:00Z")

</div>

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

[Next page](https://forum.katalon.com/t/capture-otp-while-sign-in-sign-up/18829.md?page=2)
