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

查看:116
本文介绍了如何编写模拟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?

推荐答案

基本上,您必须在devs选项中启用该应用程序(选择模拟位置应用程序).由于您无法控制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设备上的允许模拟位置"?

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天全站免登陆