javascript - Appium iOS 获取上下文不工作

标签 javascript ios selenium react-native appium

我正在开发一个 iOS React-Native 应用程序。现在我正在为登录功能进行集成测试。为了登录到我们的应用程序,用户需要打开电子邮件并单击链接。

我想用 Appium 集成测试做什么: - 打开 native 应用程序。 - 按应用程序中的按钮发送电子邮件 - 打开浏览器和gmail - 查找邮箱并点击链接登录 - 返回应用程序进行其余测试

但是我在打开电子邮件时遇到问题,我认为这是因为驱动程序的上下文错误。但是,当我尝试使用 driver.contexts() 函数获取可用的上下文时,我只是收到一个错误,根本没有任何上下文。

为什么 driver.contexts() 函数不起作用?我尝试做的事情合理吗?

我的测试 stub 如下所示:

import wd from 'wd'
import { config, port } from '../appiumConfig'

const driver = wd.promiseChainRemote('localhost', port)

describe('Login', () => {
  beforeAll(async () => await driver.init(config))
  afterAll(async () => await driver.quit())

  it('Should get contexts', async () => {
    await driver.contexts((err, handle) => {
      if (err) { // Error!
        console.log(err)
      }
      console.log(handle)
    })
  })
})

Appium 配置如下:

"ios-emulator": {
    "platformName": "iOS",
     "platformVersion": "11.3",
     "deviceName": "iPhone Simulator",
     "app": "./ios/build/Build/Products/Release-iphonesimulator/myApp.app"
   }

我得到的错误是:

{"message":"[contexts()] Error response status: 13, , UnknownError - An unknown server-side error occurred while processing the command. Selenium error: An unknown server-side error occurred while processing the command. Original error: connect ECONNREFUSED ::1:27753","status":13,"cause":{"status":13,"value":{"message":"An unknown server-side error occurred while processing the command. Original error: connect ECONNREFUSED ::1:27753"},"sessionId":"a55f921f-b98c-4304-bc8d-cf3af1a48cd4"},"jsonwire...

函数文档:https://appium.io/docs/en/commands/context/get-contexts/

Appium:1.7.2

苹果电脑:10.13.4

平台版本:11.3

驱动程序:XCUITest

节点:8.10.0

npm: 3.8.6

编辑:这也是我在 Appium 服务器上遇到的错误:

[debug] [MJSONWP] Responding to client with driver.createSession() result: {"webStorageEnabled":false,"locationContextEnabled":false,"browserName":"","platform":"MAC","javascriptEnabled":true,"databaseEnabled":false,"takesScreenshot":true,"networkConnectionEnabled":false,"platformName":"iOS","platformVersion":"11.3","deviceName":"iPhone Simulator","app":"./ios/build/Build/Products/Release-iphonesimulator/MeruHealth.app","udid":"DD7881BD-3D0A-4C5B-8774-993954C6D349"}
[HTTP] <-- POST /wd/hub/session 200 8543 ms - 467
[HTTP] 
[HTTP] --> GET /wd/hub/session/f43492eb-baa3-40b3-88b4-9aea2745ff7b/contexts
[HTTP] {}
[debug] [MJSONWP] Calling AppiumDriver.getContexts() with args: ["f43492eb-baa3-40b3-88b4-9aea2745ff7b"]
[debug] [XCUITest] Executing command 'getContexts'
[debug] [iOS] Getting list of available contexts
[debug] [iOS] Retrieving contexts and views
[debug] [iOS] Selecting by url: false 
[debug] [RemoteDebugger] Received app, but no match was found. Trying again.
[debug] [RemoteDebugger] Current applications available:
[debug] [RemoteDebugger]     Application: 'PID:27890'
[debug] [RemoteDebugger]         id: "PID:27890"
[debug] [RemoteDebugger]         isProxy: false
[debug] [RemoteDebugger]         name: "Meru Health"
[debug] [RemoteDebugger]         bundleId: "com.meruhealth.MeruHealthApp"
[debug] [RemoteDebugger]         hostId: undefined
[debug] [RemoteDebugger]         isActive: 2
[debug] [RemoteDebugger]         isAutomationEnabled: false
[debug] [RemoteDebugger]         pageDict: []
[debug] [RemoteDebugger] Found app id key 'PID:27890' for bundle 'com.meruhealth.MeruHealthApp'
[debug] [RemoteDebugger] Trying out the possible app ids: PID:27890
[debug] [RemoteDebugger] Selecting app PID:27890 (try #20 of 20)
[debug] [RemoteDebugger] Sending '_rpc_forwardGetListing:' message to remote debugger
[debug] [RemoteDebugger] Receiving data from remote debugger: '{"__argument":{"WIRApplicationIdentifierKey":"PID:27890","WIRListingKey":{"1":{"WIRPageIdentifierKey":1,"WIRTitleKey":"JSContext","WIRTypeKey":"WIRTypeJavaScript"}}},"__selector":"_rpc_applicationSentListing:"}'
[debug] [RemoteDebugger] Received response from socket send: '["PID:27890",{"1":{"WIRPageIdentifierKey":1,"WI...'
[debug] [RemoteDebugger] Page changed: {"1":{"WIRPageIdentifierKey":1,"WIRTitleKey":"JSContext","WIRTypeKey":"WIRTypeJavaScript"}}
[debug] [RemoteDebugger] Received app, but no match was found. Trying again.
[RemoteDebugger] Error: Could not connect to a valid app after 20 tries.
[RemoteDebugger]     at Object.wrappedLogger.errorAndThrow (../../lib/logging.js:78:13)
[RemoteDebugger]     at RemoteDebugger.selectApp$ (../../lib/remote-debugger.js:277:6)
[RemoteDebugger]     at tryCatch (/Users/valtterilaine/meruhealth-app/node_modules/appium-remote-debugger/node_modules/babel-runtime/regenerator/runtime.js:67:40)
[RemoteDebugger]     at GeneratorFunctionPrototype.invoke [as _invoke] (/Users/valtterilaine/meruhealth-app/node_modules/appium-remote-debugger/node_modules/babel-runtime/regenerator/runtime.js:315:22)
[RemoteDebugger]     at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Users/valtterilaine/meruhealth-app/node_modules/appium-remote-debugger/node_modules/babel-runtime/regenerator/runtime.js:100:21)
[RemoteDebugger]     at GeneratorFunctionPrototype.invoke (/Users/valtterilaine/meruhealth-app/node_modules/appium-remote-debugger/node_modules/babel-runtime/regenerator/runtime.js:136:37)
[RemoteDebugger]     at <anonymous>

最佳答案

它非常适合我的原生应用

wd.js v1.6.1

appium 服务器 v1.7.2

节点 v7.9.0

npm v4.2.0

Mac 操作系统 Sierra 10.12.6

代码与你的相似:

const wd = require('wd')

const driver = wd.promiseChainRemote(server)
await driver.init(desired)
const contexts = await driver.contexts()

服务器日志:

> CALL init({"platformName":"iOS","platformVersion":"11.1","deviceName":"iPhone SE","automationName":"XCUITest","noReset":true,"app":"/Users/dmitry/Downloads/Nigeria-TEST.app"}) 
> POST /session {"desiredCapabilities":{"platformName":"iOS","platformVersion":"11.1","deviceName":"iPhone SE","automationName":"XCUITest","noReset":true,"app":"/Users/dmitry/Downloads/Nigeria-TEST.app"}}
Driving the web on session: 425b04c9-3f26-4f67-bd2e-f955ada0abbd
> RESPONSE init({"platformName":"iOS","platformVersion":"11.1","deviceName":"iPhone SE","automationName":"XCUITest","noReset":true,"app":"/Users/dmle/Downloads/test.app"}) "425b04c9-3f26-4f67-bd2e-f955ada0abbd",{"webStorageEnabled":false,"locationContextEnabled":false,"browserName":"","platform":"MAC","javascriptEnabled":true,"databaseEnabled":false,"takesScreenshot":true,"networkConnectionEnabled":false,"platformName":"iOS","platformVersion":"11.1","deviceName":"iPhone SE","automationName":"XCUITest","noReset":true,"app":"/Users/dmle/Downloads/test.app","udid":"2CA85561-B4B9-4C59-A83A-E197ADD4E0C8"}
> CALL contexts() 
> GET /session/:sessionID/contexts 
> RESPONSE contexts() ["NATIVE_APP"]

您的日志状态中的错误 ECONNREFUSED 表示您的 appium 服务已关闭,可能是在处理请求时崩溃了 -> 检查完整日志以了解发生了什么。

我会说这个问题很可能是因为您正试图在同一个 session 中执行所有步骤(重用同一个驱动程序实例)。 尝试在单独的 session 中将浏览器导航到 gmail,然后返回使用第一个继续进行应用程序测试。多个应用程序 session 开始使用 XCode 9,因此它可能会有所帮助。

关于javascript - Appium iOS 获取上下文不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49943098/

相关文章:

python - selenium.common.exceptions.TimeoutException : Message: script timeout: result was not received in 30 seconds

javascript - 为给定的 div 结构强制驱逐库

java webapp 中的 javascript 预处理器

javascript - 按钮点击调用PHP函数

javascript - Angular.js中如何区分null和 '0'?

ios - 从不是 viewController 的 swift 文件更新时进度条崩溃

ios - 无法将 (Timer!) -> Void 转换为 ((CFRunLoopTimer?) ->Void)! - 将 NSTimer 扩展转换为 Swift 3

ios - 无法在 iOS 可执行文件中获取 Mach-O 文本段大小

python - Phantomjs Selenium WebDriver 中的自定义 header

java - 如何使用 Selenium Java 设置 <pre> html 标签的值?