背景后葫芦没有正确触摸按钮

Calabash not touching buttons properly after background

我的环境:Xcode:7.2.1 模拟器:iOS iPad Air 9.1 葫芦:0.17.1

目前,我的 iOS 应用程序在后台运行后无法正确触摸按钮。以下是发生问题的一般黄瓜步骤:

When I send the app to the background for 2 seconds
And I skip logging in to MyApp
And I wait and wait
And I should see "Speed"
And I wait for 10 seconds
And I touch "Speed"

以下是将应用程序发送到后台步骤的代码:

Given /^I send the app to the background for ([\d\.]+) second(?:s)?$/ do |num_seconds|
  num_seconds = num_seconds.to_f
  send_app_to_background(num_seconds)
end

基本上,只有在我将应用程序发送到后台后,触摸才会遇到障碍。如果我迈出这一步,Calabash 会像预期的那样触摸按钮。我包括它看到 "Speed" 的步骤,只是为了确保 Calabash 实际上可以看到按钮,它可以看到,因为它在该步骤和等待步骤中变为绿色。但是在触摸 "Speed" 时,Calabash 进入了一种状态,它一直试图触摸它,但由于它没有,它会无限期地挂在那个步骤上。我怀疑这可能是 Calabash 或 UIAutomation 错误 (https://github.com/calabash/calabash-ios/issues/836),因为一切都按预期工作,直到或除非我将我的应用程序发送到后台,但我想 100% 确定,以防万一我没有看到错误。

有人知道问题出在哪里吗?

更新:

根据https://groups.google.com/forum/#!topic/calabash-ios/NZAmTp6ckrk,将应用程序发送到后台仍未实现,当我检查send_app_to_background的代码时,它确实看起来像:

def send_app_to_background(secs)
  raise 'Not implemented when running without instruments / UIA'
end

有没有人有办法在他们的测试中解决这个问题? 运行 这对我来说不是物理设备上的选项,必须使用模拟器来完成。

更新:

https://github.com/calabash/calabash-ios/issues/556 之后,我更改了将应用程序发送到后台代码以遵循解决方法:

Given /^I send the app to the background for ([\d\.]+) second(?:s)?$/ do |num_seconds|
  num_seconds = num_seconds.to_f
  uia_send_app_to_background(num_seconds)
end

而uia_send_app_to_background的实现是:

def uia_send_app_to_background(secs)
  #uia_handle_command(:deactivate, secs)
  #Temporary workaround: https://github.com/calabash/calabash-ios/issues/556
  js_deactivate = %Q[var x = target.deactivateAppForDuration(#{secs}); var MAX_RETRY=5, retry_count = 0; while (!x && retry_count < MAX_RETRY) { x = target.deactivateAppForDuration(#{secs}); retry_count += 1}; x]
  uia(js_deactivate)
end

但是,对我来说,此解决方法似乎不起作用。该应用程序被推送到后台,但永远不会回到前台。等待几分钟后,当我在 irb 中停止该步骤时,出现此错误:

When I send the app to the background for 2 seconds        # features/step_definitions/common.rb:91
  Could not parse response ''; the app has probably crashed (RuntimeError)
  ./features/step_definitions/common.rb:93:in `/^I send the app to the background for ([\d\.]+) second(?:s)?$/'
  features/display_mapping_units_from_2630.feature:216:in `When I send the app to the background for 2 seconds'

更新

我尝试了这些变通步骤,但后台后触摸仍然不起作用,我也在8.4模拟器上尝试过,它也不起作用,这让我更加怀疑它是Xcode 7个以上需要解决的问题:

When(/^I background the app$/) do
  open_app_via_simctl("com.apple.mobilesafari")
  sleep(2)
end

When(/^I launch the app$/) do
  open_app_via_simctl("com.me.MyApp-cal")
  sleep(2)
end

步骤:

And I background the app
And I wait
And I launch the app
And I wait
And I should see "Button1"
And I wait for 10 seconds
And I touch "Button1"

Calabash 成功看到 Button1,但是当它试图触摸 Button1 时,在 9.1 模拟器中,Calabash 无限期挂起,试图找到按钮,而在 8.4 模拟器中,我立即得到一个 Calabash 找不到的错误视图 "Button1".

问题 #836 已于 11 月关闭并修复。 Google 论坛 post 来自 2013 年,第 556 期来自 2014 年。任何晚于几个月前的事情都应该受到怀疑。

不要调用 uia_send_app_to_background,API 已损坏(由 Apple)。通常坚持使用非 UIA API(除非绝对必要,否则不要调用 uia_* 方法)。

将 Calabash 更新到最新发布的版本(截至今天为 0.18.1)并再次尝试测试。

$ be calabash-ios console
> start_test_server_in_background

# Can touch before background
> touch("view marked:'my view'")

> send_app_to_background(1)

# Can touch after resuming from background
> touch("view marked:'my view'")

以下解决方案无效。如果您打开另一个应用程序,您的应用程序将进入后台。但是,如果您使用 simctl 打开您的应用程序,仪器将不再与该应用程序建立连接并且手势将失败。

When(/^I background the app$/) do
  open_app_via_simctl("com.apple.mobilesafari")
  sleep(2)
end

When(/^I launch the app$/) do
  open_app_via_simctl("com.me.MyApp-cal")
  sleep(2)
end