You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby Appium(Android)使用Webdriver Actions报perform_actions未定义错误

Appium双指触控报错解决及简化实现方案

错误根因排查

你遇到的NoMethodError: undefined method 'perform_actions' for nil:NilClass报错和触控逻辑本身无关,核心问题出在驱动实例上,常见触发原因如下:

  • @driver实例变量未完成初始化就被调用,检查驱动启动代码是否执行成功、是否正确赋值给@driver
  • 调用perform_actions时已执行过驱动销毁逻辑,或者跨作用域调用导致@driver作用域失效
  • Appium Ruby客户端版本过低,旧版本未集成perform_actions接口,建议升级到v12及以上稳定版本

同时你提供的代码存在拼写错误:f2的移动逻辑中::Selenium::Web@Driver多写了一个@符号,需要修正为::Selenium::WebDriver才能正常运行。

修正后的原生WebDriver Actions实现

以下是修复后可正常运行的双指触控代码:

# 确保@driver已正确初始化后再执行以下逻辑
f1 = ::Selenium::WebDriver::Interactions.pointer(:touch, name: 'finger1')
f1.create_pointer_move(duration: 1, x: 200, y: 500, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f1.create_pointer_down(:left)
f1.create_pause(0.5)
f1.create_pointer_move(duration: 1, x: 200, y: 200, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f1.create_pointer_up(:left)

f2 = ::Selenium::WebDriver::Interactions.pointer(:touch, name: 'finger2')
f2.create_pointer_move(duration: 1, x: 200, y: 500, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f2.create_pointer_down(:left)
f2.create_pause(0.5)
# 修复此处的@拼写错误
f2.create_pointer_move(duration: 1, x: 200, y: 800, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f2.create_pointer_up(:left)

@driver.perform_actions [f1, f2]

Ruby环境下双指旋转简化实现方案

针对谷歌地图这类场景的双指旋转需求,无需手动编写底层Pointer逻辑,直接调用Appium封装的移动端专属手势命令即可实现,代码更简洁易维护:

# 初始化驱动示例,确保@driver实例正常
require 'appium_lib'
caps = {
  platformName: 'Android',
  deviceName: '你的设备名',
  appPackage: 'com.google.android.apps.maps',
  appActivity: 'com.google.android.maps.MapsActivity',
  automationName: 'UiAutomator2'
}
@driver = Appium::Driver.new({caps: caps}, true).start_driver

# 双指旋转手势调用
@driver.execute_script 'mobile: rotateGesture', {
  elementId: target_map_element.id, # 可选参数,指定操作的元素,不传默认操作整个屏幕
  angle: 90, # 旋转角度,正数为顺时针旋转,负数为逆时针旋转
  speed: 1000 # 旋转动作执行时长,单位为毫秒
}

内容的提问来源于stack exchange,提问作者RahlMCD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 17:06:04