Resuming an app

837 views
Skip to first unread message

Mark Reid

unread,
Dec 9, 2014, 11:57:21 AM12/9/14
to calabash...@googlegroups.com
Is that possible to write a test that resumes an app that has been put into the background?

Jani Jegoroff

unread,
Dec 10, 2014, 5:34:41 AM12/10/14
to calabash...@googlegroups.com
Yes, you can use adb command for that.

# send to background
adb shell input keyevent 3

# bring to foreground
adb shell am start -n <PACKAGE-NAME>/<PATH-TO-ACTIVITY>

Check your <PACKAGE-NAME> and <PATH-TO-ACTIVITY> from source code AndroidManifest.xml

Paul Lonean

unread,
Dec 16, 2014, 8:54:39 AM12/16/14
to calabash...@googlegroups.com
First of all Thanks Jani for the help, but I have a question:
After I use:
And I wait for 2 seconds
And I tap the Home button (using: adb shell input keyevent 3 )
        And I resume the app (using: adb shell am start -n <PACKAGE-NAME>/<PATH-TO-ACTIVITY>)
And I enter "plonean" into input field number 1

The following scenario happened to me. The app opens, and i wait for 2 seconds (as my first step says), but after the app is in background since I tapped the Home button from the step 2, the 3rd step isn't starting. The calabash test's aren't working anymore and the strange thing is that no error were shown in the console. Do you have any ideas how to solve this?

Thanks!

Paul Lonean

unread,
Dec 16, 2014, 9:31:02 AM12/16/14
to calabash...@googlegroups.com
As a update:
Initially I used:

Given /^I resume the app$/ do 
%x(adb shell am start -n com.voalte.voalteone/.activity.MainVoalteOneActivity)
end

And after some researches I understood that i should use:

Given /^I resume the app$/ do 
system('adb shell am start -n com.voalte.voalteone/.activity.MainVoalteOneActivity &')
end.


But now the problem is that after the resume is made this error is displayed in the cmd console:

    And I enter "test" into input field number 2 # calabash-android-0.5.5/lib/ca
labash-android/steps/enter_text_steps.rb:9
      No connection could be made because the target machine actively refused it
. - connect(2) (http://127.0.0.1:34777) (Errno::ECONNREFUSED) (RuntimeError)
      features\test.feature:10:in `And I enter "test" into input field number 2'

      No connection could be made because the target machine actively refused it
. - connect(2) (http://127.0.0.1:34777) (Errno::ECONNREFUSED)
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient/sess
ion.rb:803:in `initialize'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient/sess
ion.rb:803:in `new'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient/sess
ion.rb:803:in `create_socket'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient/sess
ion.rb:752:in `block in connect'
      C:/Ruby193/lib/ruby/1.9.1/timeout.rb:69:in `timeout'
      C:/Ruby193/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient/sess
ion.rb:751:in `connect'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient/sess
ion.rb:609:in `query'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient/sess
ion.rb:164:in `query'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:1
083:in `do_get_block'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:8
87:in `block in do_request'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:9
81:in `protect_keep_alive_disconnected'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:8
86:in `do_request'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:7
74:in `request'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:6
84:in `post'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.5.5/lib/calabash-an
droid/operations.rb:421:in `make_http_request'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.5.5/lib/calabash-an
droid/operations.rb:381:in `http'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.5.5/lib/calabash-an
droid/operations.rb:663:in `shutdown_test_server'
      C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.5.5/lib/calabash-an
droid/operations.rb:129:in `shutdown_test_server'
      C:/Voalte/features/support/app_life_cycle_hooks.rb:12:in `After'

Failing Scenarios:
cucumber features\test.feature:3 # Scenario: Configuration

Any thoughts?

Thanks.



On Tuesday, December 9, 2014 6:57:21 PM UTC+2, Mark Reid wrote:

Jani Jegoroff

unread,
Dec 16, 2014, 1:56:08 PM12/16/14
to calabash...@googlegroups.com
Have you made sure that your commands works from Calabash console?

I just implemented example scenario using my application and it works fine.

Given application is launched
When I send application to background
Then application is sent to background
When I bring application to foreground
Then application is bring to foreground
And Home screen is displayed

1 scenario (1 passed)
6 steps (6 passed)
0m34.126s

Here are example background/foreground step definition implementations:

def when_send_app_to_background
  ADB.send_to_background
end

def when_bring_app_to_foreground
  ADB.bring_to_foreground('APP-PACKAGE-NAME', 'PATH-TO-ACTIVITY')
end
  
def then_app_in_background
  wait_for { app_in_background? }
end

def then_app_in_foreground
  wait_for { !app_in_background? }
end

def app_in_background?
  currentApp = `adb shell dumpsys window windows | grep -E 'mCurrentFocus'`
  !currentApp.include?('APP-PACKAGE-NAME')
end

ADB methods are implemented in my RubyGem. See https://rubygems.org/gems/android-adb-extension

Paul Lonean

unread,
Dec 17, 2014, 2:26:08 AM12/17/14
to calabash...@googlegroups.com

Thanks a lot so far, but I have a question: Where should I define those adb commands? Not in calabash_steps.rb file? And if they should be defined there should I just copy paste your code written bellow and it should work?

 

Thanks

--
You received this message because you are subscribed to a topic in the Google Groups "calabash-android" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/calabash-android/4wevkFdFcC0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to calabash-andro...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jani Jegoroff

unread,
Dec 17, 2014, 4:55:49 AM12/17/14
to calabash...@googlegroups.com
You can define methods where ever you want, depends how you have structured your code. But I would not use calabash_steps.rb.
Just call those methods from your step definitions.

Paul Lonean

unread,
Dec 17, 2014, 5:16:37 AM12/17/14
to calabash...@googlegroups.com

Sorry for bothering you so much but it simply just don’t workL.

 

In another rb file I’ve defined my adb commands like this:

 

def when_send_app_to_background

  ADB.send_to_background

end

def when_bring_app_to_foreground

  ADB.bring_to_foreground('com.voalte.voalteone/.activity.MainVoalteOneActivity')

end

 

def then_app_in_background

  wait_for { app_in_background? }

end

def then_app_in_foreground

  wait_for { !app_in_background? }

end

def app_in_background?

  currentApp = `adb shell dumpsys window windows | grep -E 'mCurrentFocus'`

  !currentApp.include?('APP-PACKAGE-NAME')

end

 

 

After that in the calabash-steps.rb file I’ve defined:

 

Given /^I send app$/ do

                when_send_app_to_background

end

 

Andafter all those things I’e tried to run the Script my_first_feature.feature which contains:

Feature: Successfully Log In

 

                Scenario: Log in

                And I wait for 2 seconds

                Given I send app (the defined step)

 

And I receive in the cmd console the following error message, note that the app and scripts didn’t even start:

 

  undefined method `last' for #<Enumerator:0x2e6bc00> (NoMethodError)

  C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.5.5/lib/calabash-androi

d/operations.rb:306:in `install_app'

  C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.5.5/lib/calabash-androi

d/operations.rb:96:in `install_app'

  C:/Voalte/features/support/app_installation_hooks.rb:24:in `Before'

  Will not start test server because of previous failures. (RuntimeError)

  C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.5.5/lib/calabash-androi

d/operations.rb:579:in `start_test_server_in_background'

  C:/Ruby193/lib/ruby/gems/1.9.1/gems/calabash-android-0.5.5/lib/calabash-androi

d/operations.rb:125:in `start_test_server_in_background'

  C:/Voalte/features/support/app_life_cycle_hooks.rb:5:in `Before'

 

Failing Scenarios:

cucumber features\Configuring servers\Setting_server_to_st675.feature:2 # Scenar

io: Configuration

 

1 scenario (1 failed)

0 steps

0m33.126s

 

Any thoughts?

Paul Lonean

unread,
Dec 17, 2014, 9:16:57 AM12/17/14
to calabash...@googlegroups.com

Sorry again, but do you think that you can give me your skype id? It would be much easier to talk and end this.

 

Thanks.

 

From: Paul Lonean [mailto:paul....@softvision.ro]
Sent: Wednesday, December 17, 2014 12:16 PM
To: 'calabash...@googlegroups.com'
Subject: RE: Resuming an app

 

Sorry for bothering you so much but it simply just don’t workL.

 

In another rb file I’ve defined my adb commands like this:

 

def when_send_app_to_background

  ADB.send_to_background

end

 

def when_bring_app_to_foreground

  ADB.bring_to_foreground('com.voalte.voalteone/.activity.MainVoalteOneActivity')

end

 

def then_app_in_background

  wait_for { app_in_background? }

end

 

def then_app_in_foreground

  wait_for { !app_in_background? }

end

 

def app_in_background?

  currentApp = `adb shell dumpsys window windows | grep -E 'mCurrentFocus'`

  !currentApp.include?('APP-PACKAGE-NAME')

end

 

 

After that in the calabash-steps.rb file I’ve defined:


Sent: Wednesday, December 17, 2014 11:56 AM

Jani Jegoroff

unread,
Dec 17, 2014, 2:13:28 PM12/17/14
to calabash...@googlegroups.com
Sure, no problem. Skype id is firstName.LastName

Paul Lonean

unread,
Dec 18, 2014, 8:45:09 AM12/18/14
to calabash...@googlegroups.com

I’ve added you on skype, but for any edge case my Skype id is en_plonean

Reply all
Reply to author
Forward
0 new messages