[英]Unlock emulator screen using espresso
I'm developing my first android application and I was setting up the CI server. 我正在开发我的第一个Android应用程序,我正在设置CI服务器。 My espresso tests run fine on my machine but travis errors out with the following
我的espresso咖啡测试在我的机器上正常运行,但travis错误出现以下情况
java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds.
java.lang.RuntimeException:等待视图层次结构的根目录具有窗口焦点,而不是请求布局超过10秒。
It seems that I need to unlock the emulator screen before I run the tests. 在运行测试之前,我似乎需要解锁仿真器屏幕。 To do so I have to add a manifest to
src/debug
with the required permissions and then unlock the screen with: 为此,我必须使用所需权限向
src/debug
添加清单,然后使用以下命令解锁屏幕:
KeyguardManager mKeyGuardManager = (KeyguardManager) ctx.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mLock = mKeyGuardManager.newKeyguardLock(name);
mLock.disableKeyguard();
The thing is I don't want to litter my activities with the above code wrapped in if blocks. 问题是我不想乱丢我的活动,上面的代码包含在if块中。 Is there a way to unlock the screen from the espresso test itself?
有没有办法从浓缩咖啡测试中解锁屏幕?
My espresso test: 我的浓咖啡测试:
@RunWith(AndroidJUnit4.class)
public class EspressoSetupTest {
@Rule
public final ActivityTestRule<WelcomeActivity> activity =
new ActivityTestRule<>(WelcomeActivity.class, true, true);
@Test
public void launchTest() {
onView(withId(R.id.welcome_textView_hello))
.perform(click())
.check(matches(withText("RetroLambda is working")));
}
}
You can use the setUp()
method in your Espresso Test like: 您可以在Espresso Test中使用
setUp()
方法,如:
@UiThreadTest
@Before
public void setUp() throws Exception {
final Activity activity = mActivityRule.getActivity();
mActivityRule.runOnUiThread(new Runnable() {
@Override
public void run() {
KeyguardManager mKG = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mLock = mKG.newKeyguardLock(KEYGUARD_SERVICE);
mLock.disableKeyguard();
//turn the screen on
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
}
});
}
src/debug/AndroidManifest.xml SRC /调试/ AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.