diff --git a/lib/commands/context/helpers.js b/lib/commands/context/helpers.js index a2263e2a..bec1f53a 100644 --- a/lib/commands/context/helpers.js +++ b/lib/commands/context/helpers.js @@ -10,6 +10,7 @@ import path from 'node:path'; import http from 'node:http'; import Chromedriver from 'appium-chromedriver'; import {toDetailsCacheKey, getWebviewDetails, WEBVIEWS_DETAILS_CACHE} from './cache'; +import dns from 'node:dns/promises'; // https://cs.chromium.org/chromium/src/chrome/browser/devtools/device/android_device_info_query.cc export const CHROME_BROWSER_PACKAGE_ACTIVITY = /** @type {const} */ ({ @@ -96,9 +97,11 @@ async function getFreePort() { * @returns {Promise} */ async function cdpGetRequest(host, port, endpoint) { + // Workaround for https://github.com/puppeteer/puppeteer/issues/2242, https://github.com/appium/appium/issues/20782 + const compatibleHost = isCompatibleCdpHost(host) ? host : (await dns.lookup(host)).address; return ( await axios({ - url: `http://${host}:${port}${endpoint}`, + url: `http://${compatibleHost}:${port}${endpoint}`, timeout: CDP_REQ_TIMEOUT, // We need to set this from Node.js v19 onwards. // Otherwise, in situation with multiple webviews, @@ -810,6 +813,18 @@ export async function dismissChromeWelcome() { } } +/** + * https://github.com/puppeteer/puppeteer/issues/2242#issuecomment-544219536 + * + * @param {string} host + * @returns {boolean} + */ +function isCompatibleCdpHost (host) { + return ['localhost', 'localhost.localdomain'].includes(host) + || host.endsWith('.localhost') + || Boolean(net.isIP(host)); +} + /** * @typedef {import('appium-adb').ADB} ADB */