Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait until device is idle before starting script #337

Open
tenzap opened this issue Apr 30, 2023 · 0 comments
Open

Wait until device is idle before starting script #337

tenzap opened this issue Apr 30, 2023 · 0 comments

Comments

@tenzap
Copy link

tenzap commented Apr 30, 2023

With latest api (31-33…) tests are much more flaky.

I wanted to share the way I get around this. Maybe this could be added to your script.

The idea is to close any anr that may appear on screen and also to wait until device is idle. This is detected by checking cpu usage. When below 0.9 (chosen arbitrarily), we consider it is idle

Below my script in ruby and uses fastlane's functions

desc "Wait until device is considered idle"
  lane :wait_for_idle do
    start_time = Time.now
    load_threshold = 0.9
    puts "Start waiting until device is idle (" + Time.now.to_s + ")"
    command = 'shell uptime | cut -d , -f 3 | cut -f 2 -d :'
    load = retry_adb_command(command).strip.to_f
    end_time = start_time + 1800
    while load > load_threshold && Time.now < end_time do
      if load < 4
        adb(command: 'shell dumpsys window | grep -E "mCurrentFocus.*Application Not Responding" || echo ')
        anr_package = adb(command: 'shell dumpsys window | grep -E "mCurrentFocus.*Application Not Responding" | cut -f 2 -d : | sed -e "s/}//" -e "s/^ *//" ').strip
        if anr_package != ""
            puts "ANR on screen for: " + anr_package + ". Restarting it."
            # Some suggest that restarting the service with 'am startservice' should restart it,
            # but it doesn't seem to work. So using killall.
            # We additionally restart it, but with killall, system UI is restarted automatically

            begin
              # Killing
              adb(command: 'shell su 0 killall ' + anr_package)
            rescue => ex
              UI.error(ex)
              # Fallback to click if kill didn't work. This location is for a 1080x1920 screen
              adb(command: 'shell input tap 540 935 || echo')
            end

            # Starting Service
            if anr_package == "com.android.systemui"
              adb(command: 'shell am start-service -n com.android.systemui/.SystemUIService || echo')
            end
            #cmd = 'shell pm dump ' + anr_package + ' | grep Services: -A1 | tail -n 1 | sed -e "s/.*{.* .* \(.*\)}.*/\1/"'
            #anr_package_service1 = adb(command: cmd).strip
            #adb(command: 'shell am start-service -a ' + anr_package_service1 + ' || echo')
        end
      end
      sleep 15
      load = retry_adb_command(command).strip.to_f
    end
    if load > load_threshold
      puts "Reached timeout before device is idle."  
    end
    puts "Waited until device is idle for " + (Time.now - start_time).to_i.to_s + " seconds."
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant