I need to detect at runtime from the code if the application is run using an Instrumented Test. I'm looking for a solution that works without knowing the Test class.
You can read logcat logs in launcher activity of your application under test and find the instrumented test launch command in logs.
Related
I have to test my agreement when the app is first launched: if the user do not agree, than the app must close.
Most of the test are already working but I couldn't figure out how to test if the app has closed.
Is there any way to test this behavior and "expect for close app"?
Thanks,
Ian
Run the app
Initialise flutter driver.
Check that flutter driver is not null
Close the app (the app should be closed and not backgrounded)
driver.checkHealth
When you trigger app exit you may verify if the app was exited by its exit code
expect(exitCode, 0);
I am using this approach in my tests and it works great.
usage in a sample test:
https://github.com/maheshmnj/navbar_router/blob/885c68ae974250167df3c1dccee3e6474f5ddd39/test/navbar_router_test.dart#L210
In Android Studio, in the androidTest folder, I have a couple of test cases, like as follows :
Screenshot : Android Studio
After each test class is executed, the app exits and is re-launched for next test. User have to login every time.
Is there any way I can login just once and execute all the test cases (entire test suite) without app exiting every-time ?
You can 'mock' this step before each test method (#Before annotation in JUnit) or before the entire test class (#BeforeClass annotation in JUnit) but it requires external dependency for example Mockito.
I am trying to automate my android application. When I start the test I try to switch environment from the debug drawer. While switching environment the application closed and opens a new instance for that particular environment. The test fails the time the application closes.
Getting this error: Reason: 'Instrumentation run failed due to 'Process crashed.''
IS there any way the script does not fail and wait until the new application opens
You need to use uiautomator along with espresso for this kind of use cases.Use Uiautomator to switch the environment and then let the espresso steps run.
For more details refer-https://developer.android.com/topic/libraries/testing-support-library/index.html#UIAutomator
The UI Automator testing framework provides a set of APIs to build UI tests that perform interactions on user apps and system apps. The UI Automator APIs allows you to perform operations such as opening the Settings menu or the app launcher in a test device. The UI Automator testing framework is well-suited for writing black box-style automated tests, where the test code does not rely on internal implementation details of the target app.
I am having trouble testing native applications (such as Contacts and Settings) using Cucumber-JVM for Android with JUnit. Initially, I got the following message:
"Test run failed: Permission Denial: starting instrumentation ComponentInfo???{com.test.contacts/android.test.InstrumentationTestRunner???} from pid=673, uid=673 not allowed because package com.test.contacts does not have a signature matching the target com.android.contacts"
To solve this problem, I signed the test application with the same keys of the Contacts native android application (shared.x509.pem and shared.pk8) and I also added the following line to the AndroidManifest.xml file (as suggested in How can I sign my application with the system signature key?):
“android:sharedUserId="android.uid.shared"
This seemed to solve the problem.
However, after this change, I only manage to run the first test from a test suite. When the second test is running, it gets lost in the getActivity() method from the class ActivityInstrumentationTestCase2, which my steps definitions class extends. More precisely, it doesn't get out of the mSync.wait() call in the method startActivitySync(Intent intent) from Instrumentation.java. The call to getActivity() calls launchActivityWithIntent(), from InstrumentationTestCase.java, which calls startActivitySync(Intent intent).
I found a similar issue on Why does getActivity() block during JUnit test when custom ImageView calls startAnimation(Animation)?, but the workaround described there doesn’t solve my problem.
My test application is really simple and it only checks the content of the buttons in the activity. I don’t have this problem if I use the same test application to test my own apps, only with native android applications such as Contacts and Settings.
Does anyone know something about this issue and could give me a light on how could I solve it?
Thanks in advance
I'm new to Android development and just experimented with unit testing inside Android Studio. I have 2 questions,
each time I need to run tests I need to create a "Run/Debug Configurations" for the test class that derives from InstrumentationTestCase, and in this configuration I can only specify 1 test class and 1 test method at a time. Is there a way to break this limitation so I can run a bunch of test classes and their test methods altogether?
I have Log statements in my tests, but I could not find where it logged the output messages to, I checked logcat and event log but didn't find it there.
Thanks.
1) You should be able to right/control click either a test method, or a test class (suite) and run the test from the context menu.
After you run it once, the run config should be, permanently, in the list of runnable configurations, and available in all of the handy ways.
2) The most likely reason that your log messages are not showing up, is that Studio has this annoying habit of automatically installing a filter, on a run, so that it only shows log messages that are from the package of the app you are running. Since your test and your app are in different packages, it is probably filtering some of the messages away.