How to switch to web-view in IOS simulator using Javascript - Appium

I am trying to switch to webview from nativeapp using Javascript. How do I perform this?. Below is what I have tried.

`let contexts = browser.contexts()
console.log(contexts)`

When I perform the above line of code, it correctly gives me the below output

 `{ value: [ 'NATIVE_APP', 'WEBVIEW_40618.1' ],
  sessionId: '90908a5e-8b80-4de1-aeaf-b881c10ad647',
  _status: 0 }`

Now, when I perform this:

` browser.context(contexts[1])
opn("www.google.com").then(() => {
}).catch((err) => {
 console.log("Unable to open the URL specified")
console.log(err)
})`

Please note: from the above, opn() is a NPM module used to open a URL

It doesn’t switch to the webview. Can anyone please help me out with this. Everytime I try, it gives the below error

{ Error: Expected atarget[Symbol(originalCallSite)]: [ CallSite {}, CallSite {}, CallSite {}, CallSite {}, CallSite {}, CallSite {}, CallSite {} ], [Symbol(mutatedCallSite)]: [ CallSite {}, CallSite {}, CallSite {}, CallSite {}, CallSite {}, CallSite {}, CallSite {} ] }

I resolved this with some research on google. Below steps to be followed for anyone stuck with this

  1. In the desired capabilities, give the below:

“noReset” : true

Reason: This is done to open the browser in the same session instead of creating a new session

  1. To launch safari browser in IOS simulator, give the below:

driver.execute(‘mobile: launchApp’, {‘bundleId’:‘com.apple.mobilesafari’})

Please Note: Search in google as “Bundle Id’s for native app in IOS”, see the list and you can add them as arguments

  1. To launch the URL, give the below:

driver.url(“http://www.google.com”)

Hope this will help someone trying to switch to a browser/web view in IOS simulator