Unable to select WebView element after switching context

I am running into a strange problem with webdriverio/appium when trying to test an iOS app. The app has a WebView with a button labeled “Refresh”. When I try to test for this button, I get a “not displayed” error (see code below).

it('should have the Refresh button', () => {
    WebViewScreen.waitForWebsiteLoaded();

    // Verify that Refresh button is displayed
    WebViewScreen.switchToContext(CONTEXT_REF.WEBVIEW);
    const button = $('button=Refresh');
    expect(button).toBeDisplayed();
    WebViewScreen.switchToContext(CONTEXT_REF.NATIVE);
});

Note that I am switching context to WEBVIEW before testing for the button. Looking at Appium logs, the request to find the button ($('button=Refresh')) is returning a 404. Tried a different selector: ['button[data-test="refresh-button"]'], but no go. I even put a browser.debug() and queried in the REPL - still 404 - although I can physically see the button on the screen!

I know that my webdriverio/appium setup is good because the exact same code works for another iOS screen with a WebView and a button - the only difference is the button name.

I have also ruled out the possibility that my selector is incorrect by going to the standalone webpage and testing for that button - no errors in this case! Here’s the standalone web page code - only difference is no context switching calls:

it('should have the Refresh button', () => {
    browser.url('https://example.site.com/');

    // Verify that Refresh button is displayed
    const button = $('button=Refresh')
    expect(button).toBeDisplayed();
});

Any ideas on how to debug this? Are there any nuances about switching to WEBVIEW context that I am missing?

  1. different iOS versions have different issues how to switch into webView. What your Appium server version? Use latest Appium. 1.17.0-beta.X to avoid latest issues with iOS 13.X.
  2. with some screens it may happen that application have several webViews. Check that you switch into correct that has elements you looking for.
  3. double check that you able to see your elements with Safari debugger. If Safari debugger can’t see it = Appium will not see it also.

Thanks @Aleksei - I think #2 may be my problem. The app has several WebViews, but each screen has atmost one. However it seems that getContexts() is returning more than one WebViews. How do I pick the one that I am interested in? The names are not very helpful - WEBVIEW_60997.3 & WEBVIEW_60997.5!!

P.S. I didn’t understand your comment about Appium server version. I am using v1.17.0 which is the latest. This should be preferred over v1.17.0-beta.X, correct?

Ok, found out about the fullContextList option to get more information about the contexts. So all set there.

Still curious about your version number comment.

ahhh. version should be 1.17 now. beta it was 30 days ago -> https://github.com/appium/appium/tags