I am using Espresso for UI testing android. I wanted to run a test with location off in the settings and I got stuck with the location enabler dialogue as it fails other test. I have mentioned my observations and what I have tried so far
Used Roboelectric, it had no effect on the problem.
Used Shadow operation, it had no effect on the problem.
I have also attached a sample image of the location dialogue.
Thank you.
Latest development (Android 9.0, API level 28 or higher) of espresso testing framework will be done in AndroidX library. testInstrumentationRunner in the android/defaultConfig sets AndroidJUnitRunner class to run the instrumentation tests.
UI Automator (Android 4.3, API level 18 or higher) is a UI testing framework suitable for cross-app functional UI testing across system and installed apps. The UI Automator APIs allows you to perform operations such as opening the Settings menu or the app launcher on a test device.
Add Espresso dependencies Open your app's build. gradle file. This is usually not the top-level build. gradle file but app/build.
check is a method which accepts an argument of type ViewAssertion and do assertion using passed in ViewAssertion object. matches(withText(“Hello”)) returns a view assertion, which will do the real job of asserting that both actual view (found using withId) and expected view (found using withText) are one and the same.
Currently I'm using grantPerms method which should be added to all test classes (all = test-classes which require access to android permissions) so I'm just call it at the right time (if app need perms).
Here is scheme:
public class MyClass {
// Rules etc.
@Test
public void myTestWithPerms() {
// click view, if it's need permissions to camera etc., just call
grantPerms(); //calling inner grantPerms method
// Another code, if you need access to gallery now
grantPerms(); //calling inner grantPerms method
// Some test code
}
private void grantPerms(){
// grantPermsCode
}
}
Or do you mean something specific?
Update I see, so I will show an example how I have resolved it on my side, for me this is good solution.
so YOU just need to create a inner private method inside your test class (which currently require access to android permissions) scheme was added above
UPDATE2: generally you can't turn ON GPS services programmatically, this is not allowed since android v.4.2, so generally it's better to turn ON GPS services manually before test was started, but take a look at this
solution, may be this is what you want:
public class MyTestClass {
// Rules etc.
@Test
public void myTestWithTurnOnGPS() {
// once access the map check alert presence
tapTurnOnGpsBtn(); //call inner **tapTurnOnGpsBtn** method
}
private void tapTurnOnGpsBtn() throws UiObjectNotFoundException {
UiObject allowGpsBtn = device.findObject(new UiSelector()
.className("android.widget.Button").packageName("com.google.android.gms")
.resourceId("android:id/button1")
.clickable(true).checkable(false));
device.pressDelete(); // just in case to turn ON blur screen (not a wake up) for some devices like HTC and some other
if (allowGpsBtn.exists() && allowGpsBtn.isEnabled()) {
do {
allowGpsBtn.click();
} while (allowGpsBtn.exists());
}
}
}
So this method should be call in all places IN YOUR APP which suppose to raise GPS alert ()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With