webdriverio: [🐛 Bug]: Certain methods (waitForDisplayed, isElementDisplayed, etc...) are not working after switching to NATIVE_APP context

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

9.5.7

Node.js Version

20.11.1

Mode

WDIO Testrunner

Which capabilities are you using?


What happened?

I came across the following problem after upgrading to WDIO 9 from WDIO 7.

Starting the automation of a mobile device (what I tried were Android Emulator and iOS Simulator) with “browserName” specified in capabilities, then switching to the NATIVE_APP context with driver.switchContext('NATIVE_APP') then trying to explicitly wait for components with element.waitForDisplayer() or element.isDisplayed(), etc will display an error message and won’t run properly. (See Relevant Log Output section for the message.)

However, starting the automation in the NATIVE_APP context (by not specifying a “browserName” in the capabilities) will run properly, even if you switch to a WEBVIEW context and then back to NATIVE_CONTEXT.

What is your expected behavior?

The test should be able to call the methods as it did in WDIO 7 and execute without an issue.

How to reproduce the bug.

The issue can be easily reproduced. I have used the latest appium-boilerplate and stripped it down to a smaller “project”, so I can share it: https://github.com/FieryHammer/wdio9-native-app-context-issue-demo

Clone the repository, and install the dependencies with npm install (you can see, everything should be the latest version for the time being). Set up emulators as you would with appium-installer.

You can find 4 WDIO configs:

  • wdio.android.browser.conf.ts Sets up an Android emulator with Chrome as the browser so that the default context will be a WEBVIEW context
  • wdio.android.native.conf.ts Sets up an Android emulator without “browserName” so that the default context will be NATIVE_APP context
  • wdio.ios.browser.conf.ts Sets up an iOS simulator with Safari as the browser so that the default context will be a WEBVIEW context
  • wdio.ios.native.conf.ts Sets up an iOS simulator without “browserName” so that the default context will be NATIVE_APP context

The appropriate package.json scripts to run these tests are android.browser, android.native, ios.browser, and ios.native.

The ‘*.native’ scripts will programmatically open a browser app (Safari on iOS and Chrome on Android), then navigate to https://webdriver.io, select the ‘Get Started’ button, wait for it, and click it. These tests should run as intended.

The ‘*browser’ scripts will automatically open their configured browsers, navigate to https://webdriver.io once again, switch to the ‘NATIVE_APP’ context, try to select the ‘Get Started’ button, wait for it, and click it. This will throw error messages when the waitForDisplayed is called.

Relevant log output

WARN webdriver: WebDriverError: Method is not implemented when running "execute/sync" with method "POST" and args "function isElementDisplayed(element) {
[0-0]   function nodeIsElement(node) {
[0-0]     if (!node) {
[0-0]       return false;
[0-0]     }
[0-0]     switch (node.nodeType) {
[0-0]       case Node.ELEMENT_NODE:
[0-0]       case Node.DOCUMENT_NODE:
[0-0]       case Node.DOCUMENT_FRAGMENT_NODE:
[0-0]         return true;
[0-0]       default:
[0-0]         return false;
[0-0]     }
[0-0]   }
[0-0]   function parentElementForElement(element2) {
[0-0]     if (!element2) {
[0-0]       return null;
[0-0]     }
[0-0]     return enclosingNodeOrSelfMatchingPredicate(element2.parentNode, nodeIsElement);
[0-0]   }
[0-0]   function enclosingNodeOrSelfMatchingPredicate(targetNode, predicate) {
[0-0]     for (let node = targetNode; node && node !== targetNode.ownerDocument; node = node.parentNode) {
[0-0]       if (predicate(node)) {
[0-0]         return node;
[0-0]       }
[0-0]     }
[0-0]     return null;
[0-0]   }
[0-0]   function enclosingElementOrSelfMatchingPredicate(targetElement, predicate) {
[0-0]     for (let element2 = targetElement; element2 && element2 !== targetElement.ownerDocument; element2 = parentElementForElement(element2)) {
[0-0]       if (predicate(element2)) {
[0-0]         return element2;
[0-0]       }
[0-0]     }
[0-0]     return null;
[0-0]   }
[0-0]   function cascadedStylePropertyForElement(element2, property) {
[0-0]     if (!element2 || !property) {
[0-0]       return null;
[0-0]     }
[0-0]     if ("ShadowRoot" in window && element2 instanceof window.ShadowRoot) {
[0-0]       element2 = element2.host;
[0-0]     }
[0-0]     const computedStyle = window.getComputedStyle(element2);
[0-0]     const computedStyleProperty = computedStyle.getPropertyValue(property);
[0-0]     if (computedStyleProperty && computedStyleProperty !== "inherit") {
[0-0]       return computedStyleProperty;
[0-0]     }
[0-0]     const parentElement = parentElementForElement(element2);
[0-0]     return cascadedStylePropertyForElement(parentElement, property);
[0-0]   }
[0-0]   function elementHasBoundingBox(element2) {
[0-0]     const boundingBox = element2.getBoundingClientRect();
[0-0]     return boundingBox.width > 0 && boundingBox.height > 0;
[0-0]   }
[0-0]   function elementSubtreeHasNonZeroDimensions(element2) {
[0-0]     if (elementHasBoundingBox(element2)) {
[0-0]       return true;
[0-0]     }
[0-0]     const boundingBox = element2.getBoundingClientRect();
[0-0]     if (element2.tagName.toUpperCase() === "PATH" && boundingBox.width + boundingBox.height > 0) {
[0-0]       const strokeWidth = cascadedStylePropertyForElement(element2, "stroke-width");
[0-0]       return !!strokeWidth && parseInt(strokeWidth, 10) > 0;
[0-0]     }
[0-0]     const cascadedOverflow = cascadedStylePropertyForElement(element2, "overflow");
[0-0]     if (cascadedOverflow === "hidden") {
[0-0]       return false;
[0-0]     }
[0-0]     return [].some.call(element2.childNodes, function(childNode) {
[0-0]       if (childNode.nodeType === Node.TEXT_NODE) {
[0-0]         return true;
[0-0]       }
[0-0]       if (nodeIsElement(childNode)) {
[0-0]         return elementSubtreeHasNonZeroDimensions(childNode);
[0-0]       }
[0-0]       return false;
[0-0]     });
[0-0]   }
[0-0]   function elementOverflowsContainer(element2) {
[0-0]     const cascadedOverflow = cascadedStylePropertyForElement(element2, "overflow");
[0-0]     if (cascadedOverflow !== "hidden") {
[0-0]       return false;
[0-0]     }
[0-0]     return true;
[0-0]   }
[0-0]   function isElementSubtreeHiddenByOverflow(element2) {
[0-0]     if (!element2) {
[0-0]       return false;
[0-0]     }
[0-0]     if (!elementOverflowsContainer(element2)) {
[0-0]       return false;
[0-0]     }
[0-0]     if (!element2.childNodes.length) {
[0-0]       return false;
[0-0]     }
[0-0]     return [].every.call(element2.childNodes, function(childNode) {
[0-0]       if (childNode.nodeType === Node.TEXT_NODE) {
[0-0]         return false;
[0-0]       }
[0-0]       if (!nodeIsElement(childNode)) {
[0-0]         return true;
[0-0]       }
[0-0]       if (!elementSubtreeHasNonZeroDimensions(childNode)) {
[0-0]         return true;
[0-0]       }
[0-0]       return isElementSubtreeHiddenByOverflow(childNode);
[0-0]     });
[0-0]   }
[0-0]   function isElementInsideShadowRoot(element2) {
[0-0]     if (!element2) {
[0-0]       return false;
[0-0]     }
[0-0]     if (element2.parentNode && element2.parentNode.host) {
[0-0]       return true;
[0-0]     }
[0-0]     return isElementInsideShadowRoot(element2.parentNode);
[0-0]   }
[0-0]   if (!isElementInsideShadowRoot(element) && // IE doesn't support document.contains, therefor check before using
[0-0]   (typeof document.contains === "function" ? !document.contains(element) : !document.body.contains(element))) {
[0-0]     return false;
[0-0]   }
[0-0]   switch (element.tagName.toUpperCase()) {
[0-0]     case "BODY":
[0-0]       return true;
[0-0]     case "SCRIPT":
[0-0]     case "NOSCRIPT":
[0-0]       return false;
[0-0]     case "OPTGROUP":
[0-0]     case "OPTION": {
[0-0]       const enclosingSelectElement = enclosingNodeOrSelfMatchingPredicate(element, function(e) {
[0-0]         return e.tagName.toUpperCase() === "SELECT";
[0-0]       });
[0-0]       return isElementDisplayed(enclosingSelectElement);
[0-0]     }
[0-0]     case "INPUT":
[0-0]       if (element.type === "hidden") {
[0-0]         return false;
[0-0]       }
[0-0]       break;
[0-0]     // case 'MAP':
[0-0]     // FIXME: Selenium has special handling for <map> elements. We don't do anything now.
[0-0]     default:
[0-0]       break;
[0-0]   }
[0-0]   if (cascadedStylePropertyForElement(element, "visibility") !== "visible") {
[0-0]     return false;
[0-0]   }
[0-0]   const hasAncestorWithZeroOpacity = !!enclosingElementOrSelfMatchingPredicate(element, function(e) {
[0-0]     return Number(cascadedStylePropertyForElement(e, "opacity")) === 0;
[0-0]   });
[0-0]   const hasAncestorWithDisplayNone = !!enclosingElementOrSelfMatchingPredicate(element, function(e) {
[0-0]     return cascadedStylePropertyForElement(e, "display") === "none";
[0-0]   });
[0-0]   if (hasAncestorWithZeroOpacity || hasAncestorWithDisplayNone) {
[0-0]     return false;
[0-0]   }
[0-0]   if (!elementSubtreeHasNonZeroDimensions(element)) {
[0-0]     return false;
[0-0]   }
[0-0]   if (isElementSubtreeHiddenByOverflow(element) && !elementHasBoundingBox(element)) {
[0-0]     return false;
[0-0]   }
[0-0]   return true;
[0-0] }"

Code of Conduct

  • I agree to follow this project’s Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

@wswebcreation created the issue now, unfortunately cannot share much form my repo but happy to help you with testing the solution, thanks 😃 https://github.com/webdriverio/webdriverio/issues/14258

In case this is helpful: After bumping the versions to 9.6.0, the tests still break, but the error message is now different. Instead of indicating that ‘waitForDisplayed’ is unimplemented, it now shows ‘checkVisibility’ being unimplementd (the code is still calling waitForDisplayed):

[0-0] 2025-01-22T08:34:01.589Z ERROR webdriver: WebDriverError: Method is not implemented when running "execute/sync" with method "POST" and args "function checkVisibility(elem, params) {
[0-0]     return elem.checkVisibility(params);
[0-0]   }"
[0-0] 2025-01-22T08:34:01.592Z INFO webdriver: COMMAND executeScript("
[0-0]             function webdriverioPolyfill() {
[0-0]   const __defProp2 = Object.defineProperty;
[0-0]   const __name = function(target, value) {
[0-0]     return __defProp2(target, "name", { value, configurable: true });
[0-0]   };
[0-0]   const __globalThis = typeof globalThis === "object" && globalThis || typeof window === "object" && window;
[0-0]   __globalThis.__name = __name;
[0-0] }
[0-0]             webdriverioPolyfill()
[0-0]             return (function checkVisibility(elem, params) {
[0-0]     return elem.checkVisibility(params);
[0-0]   }).apply(null, arguments)
[0-0]         ", <object>)
[0-0] 2025-01-22T08:34:01.592Z INFO webdriver: [POST] http://127.0.0.1:4723/session/b73334af-ef02-417f-a91f-e5c30c8e8ecb/execute/sync
[0-0] 2025-01-22T08:34:01.593Z INFO webdriver: DATA {
[0-0]   script: 'checkVisibility(...) [523 bytes]',
[0-0]   args: [
[0-0]     {
[0-0]       'element-6066-11e4-a52e-4f735466cecf': '17000000-0000-0000-585C-000000000000',
[0-0]       ELEMENT: '17000000-0000-0000-585C-000000000000'
[0-0]     },
[0-0]     { withinViewport: false }
[0-0]   ]
[0-0] }