如何编写模拟 GPS 位置的 Espresso 测试并在 Google Testlab 中使用它们? [英] How to write Espresso Tests which are mocking GPS locations and use them in Google Testlab?

查看:29
本文介绍了如何编写模拟 GPS 位置的 Espresso 测试并在 Google Testlab 中使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Espresso Recorder 录制了一次浓缩咖啡测试.我想在我的应用中测试一些位置更改.

I recorded an espresso test with Espresso Recorder. I want to test some location changes in my app.

目前我正在使用此代码模拟该位置:

Currently I'm mocking the location with this code:

LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy( Criteria.ACCURACY_FINE );

String mocLocationProvider = LocationManager.GPS_PROVIDER;//lm.getBestProvider( criteria, true );

lm.addTestProvider(mocLocationProvider, false, false,
        false, false, true, true, true, 0, 5);
lm.setTestProviderEnabled(mocLocationProvider, true);

Location loc = new Location(mocLocationProvider);
Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(-26.902038);  // double
mockLocation.setLongitude(-48.671337);
mockLocation.setAltitude(loc.getAltitude());
mockLocation.setTime(System.currentTimeMillis());
mockLocation.setAccuracy(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
lm.setTestProviderLocation( mocLocationProvider, mockLocation);

我还在调试清单文件中添加了权限:

I also added the permission to the debug manifest file:

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

但不幸的是,我仍然收到安全异常:

But unluckily I still get a Security Exception:

java.lang.SecurityException: mypackage.test from uid not allowed to perform MOCK_LOCATION

我想在 Google Firebase 测试实验室中使用模拟位置运行录制的测试用例.我该如何解决这个问题?

I want to run the recorded test case with the mocked location in Google Firebase Test Lab. How can I solve this problem?

推荐答案

基本上,您必须在开发者选项中启用该应用程序(选择模拟位置应用程序).由于您无法在 google 平台上控制设备,因此您必须使用 ADB 命令来启用它.

Basically you have to enable the application in the devs options (select mock location app). Since you cannot control devices on google plateform, you have to use an ADB command to enable it.

appops set {yourPackageName} android:mock_location allow

例如,这就是我在 @before 函数中为我的模拟位置测试所做的:

As for example, this is what i am doing in a @before function for my mocking location tests :

@Before
fun grantPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        with(getInstrumentation().uiAutomation) {
            ...
            executeShellCommand("appops set " + InstrumentationRegistry.getTargetContext().packageName + " android:mock_location allow")
            Thread.sleep(1000)
            ...
        }
    }
}

基于这种方法,请注意,如果您插入任何新设备,您还可以为您的 gradle 任务创建一个片段,以便在您的工作站上自动执行此操作.参见例如:如何在使用 uiautomator 和 espresso 执行 AndroidTest 之前,在 Android 设备上设置 Allow Mock Location?

Based on this approach, note that you can also create a snippet for your gradle task to do it automatically on your workstation if you plug any new device. See for example : How to set Allow Mock Location on Android Device before executing AndroidTest with uiautomator and espresso?

希望对您有所帮助!

这篇关于如何编写模拟 GPS 位置的 Espresso 测试并在 Google Testlab 中使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆