Could not able to fetch the id when switch back from webview to native view of app

Hi All,

I am facing one challenge to automate the navigation back scenario from login webview screen to native view. So, flow will be like:

  1. Native_View of app has one Login id
  2. click on that login id
  3. navigate to webview of login page
  4. Click on the back button of webview
    5 Navigates back to Native_View of app has one Login id

So, the error occurs at step #5. Though I have switched to native_view successfully

[0-1] 2022-06-14T11:18:14.810Z INFO webdriver: DATA { name: ‘NATIVE_APP’ }
[0-1] 2022-06-14 11:18:15.002 INFO [src/pageObjects/common.page.ts:43 switchContextInApp] Switched to NATIVE_APP

**I could not able to fetch the same Login id which I was doing in step#1 and #2. **
Doing this again is causing me error:

await browser.findElement(‘xpath’, ‘//*[@id=“login-back-btn”]’);

[0-1] 2022-06-14T11:18:25.006Z INFO webdriver: DATA { using: 'accessibility id', value: 'Login' }
[0-1] 2022-06-14T11:18:25.080Z INFO webdriver: RESULT {
[0-1]   error: 'no such element',
[0-1]   message: 'An element could not be located on the page using the given search parameters.',
[0-1]   stacktrace: 'NoSuchElementError: An element could not be located on the page using the given search parameters.\n' +
[0-1]     '    at AndroidUiautomator2Driver.findElOrEls (/Users/harsha.sharmagelato.com/Dev/work/repos/gelato-api-mobile/integration-tests/node_modules/appium/node_modules/appium-android-driver/lib/commands/find.js:75:11)\n' +
[0-1]     '    at runMicrotasks (<anonymous>)\n' +
[0-1]     '    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n' +
[0-1]     '    at AndroidUiautomator2Driver.findElOrElsWithProcessing (/Users/harsha.sharmagelato.com/Dev/work/repos/gelato-api-mobile/integration-tests/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/find.js:33:12)\n' +
[0-1]     '    at AndroidUiautomator2Driver.findElement (/Users/harsha.sharmagelato.com/Dev/work/repos/gelato-api-mobile/integration-tests/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/find.js:53:10)'
[0-1] }

Can someone please suggest me what I am missing at step #5. Why only after context switching from webview to native app is causing this error though the element is visible?

Could be a couple of things:

  1. timing issue, you need to add a wait
  2. state issue. App is in different state and has different id/hierarchy

Might do try/catch for the NoSuchElementError and spit out the app source in catch block. See what is there.

https://appium.io/docs/en/commands/session/source/