2
The methods setOrientationLeft() and setOrientationRight() below simply do not work, both on emulated devices and on physical devices. I would like to know if there are any rules for using sponges or specific scenarios.
Exampleinstrumentedtestkotlin.kt
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 16)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class ExampleInstrumentedTestKotlin {
private var mDevice: UiDevice? = null
private val launcherPackageName: String
get() {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_HOME)
val pm = getApplicationContext<Context>().packageManager
val resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
return resolveInfo!!.activityInfo.packageName
}
companion object {
private val BASIC_SAMPLE_PACKAGE = "xxx.xxxx.xxxx"
private val LAUNCH_TIMEOUT = 5000
private val STRING_TO_BE_TYPED = "UiAutomator"
}
@Before
fun openApp() {
mDevice = UiDevice.getInstance(getInstrumentation())
mDevice!!.pressHome()
val launcherPackage = launcherPackageName
assertThat(launcherPackage, notNullValue())
mDevice!!.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT.toLong())
val context = getApplicationContext<Context>()
val intent = context.packageManager
.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE)
intent!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
context.startActivity(intent)
mDevice!!.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT.toLong())
}
@Test
fun teste(){
load(10) //função para aguardar 10 segundos
mDevice!!.setOrientationLeft()
load(10) //função para aguardar 10 segundos
mDevice!!.setOrientationRight()
}
}
What is not going?? which error appears? Uidevice is null always same?
– Murillo Comino
The code does not present any error, it passes as if it has everything ok, however the orientation of the device does not change as expressed in the documentation, the part of the code that was missing I added, was the function openApp.
– LSA
Have you tried some method
wait
ofUiDevice
? For example,waitForIdle()
– Rafael Tavares
yes, but it doesn’t work.
– LSA