Working with Android OS. Kaspresso Device abstraction.
Device is a provider of managers for all off-screen work.
Structure
All examples are located in device_tests. Device provides these managers:
appsallows to install or uninstall applications. Usesadb installandadb uninstallcommands. See the example DeviceAppSampleTest.activitiesis an interface to work with currently resumed Activities. AdbServer not required. See the example DeviceActivitiesSampleTest.filesprovides the possibility of pushing or removing files from the device. Usesadb pushandadb rmcommands and does not requireandroid.permission.WRITE_EXTERNAL_STORAGEpermission. See the example DeviceFilesSampleTest.internetallows toggling WiFi and network data transfer settings. Be careful of using this interface, WiFi settings changes could not work with some Android versions. See the example DeviceNetworkSampleTest.keyboardis an interface to send key events via adb. Use it only when Espresso or UiAutomator are not appropriate (e.g. screen is locked). See the example DeviceKeyboardSampleTest.locationemulates fake location and allows to toggle GPS setting. See the example DeviceLocationSampleTest.phoneallows to emulate incoming calls and receive SMS messages. Works only on emulators since usesadb emucommands. See the example DevicePhoneSampleTest.screenshotsis an interface screenshots of currently resumed activity. Requiresandroid.permission.WRITE_EXTERNAL_STORAGE permission. See the example DeviceScreenshotSampleTest.accessibilityallows to enable or disable accessibility services. Available since api 24. See the example DeviceAccessibilitySampleTest.permissionsprovides the possibility of allowing or denying permission requests via default Android permission dialog. See the example DevicePermissionsSampleTest.hackPermissionsprovides the possibility of allowing any permission requests without default Android permission dialog. See the example DeviceHackPermissionsSampleTest.exploitallows to rotate device or press system buttons. See the example DeviceExploitSampleTest.languageallows to switch language. See the example DeviceLanguageSampleTest.logcatprovides access to adb logcat. See the example DeviceLogcatSampleTest.
The purpose oflogcat:
If you have not heard about GDPR and high-profile lawsuits then you are lucky. But, if your application works in Europe then it's so important to enable/disable all analytics/statistics according to acceptance of the agreements. One of the most reliable ways to check analytics/statistics sending is to verify logcat where all analytics/statistics write their logs (in debug mode, sure). That's why we have created a specialLogcatclass providing a wide variety of ways to check logcat.uiDevicereturns an instance ofandroid.support.test.uiautomator.UiDevice. We don't recommend to use it directly because there is Kautomator that offers a more readable, predictable and stable API to work outside your application.
Also Device provides application and test contexts - targetContext and context.
Usage
Device instance is available in BaseTestContext scope and BaseTestCase via device property.
@Test
fun test() =
run {
step("Open Simple Screen") {
activityTestRule.launchActivity(null)
======> device.screenshots.take("Additional_screenshot") <======
MainScreen {
simpleButton {
isVisible()
click()
}
}
}
// ....
}
Restrictions
Most of the features that Device provides use of adb commands and requires AdbServer to be run.
Some of them, such as call emulation or SMS receiving, could be executed only on emulator. All such methods are marked by annotation @RequiresAdbServer.
All the methods which use ADB commands require android.permission.INTERNET permission.
For more information, see AdbServer documentation.