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

how to launcher other App in AndroidJUnit4 #421

Closed
wyj3531 opened this issue Dec 22, 2021 · 1 comment
Closed

how to launcher other App in AndroidJUnit4 #421

wyj3531 opened this issue Dec 22, 2021 · 1 comment

Comments

@wyj3531
Copy link

wyj3531 commented Dec 22, 2021

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@test
fun useAppContext() {
val pkgName = "com.qin.demo"
val deviceContext = ApplicationProvider.getApplicationContext()
val intent = deviceContext.packageManager.getLaunchIntentForPackage(pkgName)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
deviceContext.startActivity(intent)
}
}

@mrk-han
Copy link
Contributor

mrk-han commented Aug 25, 2023

To launch other apps, you will need to use uiautomator. androidjunit4 is just the runner.

Per the docs:

  // Launch the app
  val context = ApplicationProvider.getApplicationContext<Context>()
  val intent = context.packageManager.getLaunchIntentForPackage(
  BASIC_SAMPLE_PACKAGE).apply {
    // Clear out any previous instances
    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
  }
  context.startActivity(intent)

  // Wait for the app to appear
  device.wait(
    Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
    LAUNCH_TIMEOUT
    )
  }
}

https://developer.android.com/training/testing/other-components/ui-automator

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

3 participants