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:
apps
allows to install or uninstall applications. Usesadb install
andadb uninstall
commands. See the example DeviceAppSampleTest.activities
is an interface to work with currently resumed Activities. AdbServer not required. See the example DeviceActivitiesSampleTest.files
provides the possibility of pushing or removing files from the device. Usesadb push
andadb rm
commands and does not requireandroid.permission.WRITE_EXTERNAL_STORAGE
permission. See the example DeviceFilesSampleTest.internet
allows 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.keyboard
is 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.location
emulates fake location and allows to toggle GPS setting. See the example DeviceLocationSampleTest.phone
allows to emulate incoming calls and receive SMS messages. Works only on emulators since usesadb emu
commands. See the example DevicePhoneSampleTest.screenshots
is an interface screenshots of currently resumed activity. Requiresandroid.permission.WRITE_EXTERNAL_STORAGE permission
. See the example DeviceScreenshotSampleTest.accessibility
allows to enable or disable accessibility services. Available since api 24. See the example DeviceAccessibilitySampleTest.permissions
provides the possibility of allowing or denying permission requests via default Android permission dialog. See the example DevicePermissionsSampleTest.hackPermissions
provides the possibility of allowing any permission requests without default Android permission dialog. See the example DeviceHackPermissionsSampleTest.exploit
allows to rotate device or press system buttons. See the example DeviceExploitSampleTest.language
allows to switch language. See the example DeviceLanguageSampleTest.logcat
provides 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 specialLogcat
class providing a wide variety of ways to check logcat.uiDevice
returns 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.