【发布时间】:2016-05-09 05:25:53
【问题描述】:
我有登录屏幕,其中包含要输入的电子邮件和密码,底部有提交按钮。根据启用软键盘时的要求,我将提交按钮移到顶部,即向用户显示提交按钮使用以下代码在软键盘上方和我的电子邮件/密码布局下方。
mainrel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
mainrel.getWindowVisibleDisplayFrame(r);
int heightDiff = mainrel.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 128 && heightDiff != 146) {
//KeyBoard Enabled
moveButtonTo_Top();
} else {
//KeyBoard Disabled
moveButtonTo_Down();
}
}
});
//Floating down the button
private void moveButtonTo_Down() {
RelativeLayout.LayoutParams relativeparams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
relativeparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeparams.setMargins(0, 0, 0, Constants.NEXT_LAYOUT_MARGIN_TOP);
submitLay.setLayoutParams(relativeparams);
}
//Floating Up button
private void moveButtonTo_Top() {
RelativeLayout.LayoutParams relativeparams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeparams.addRule(RelativeLayout.BELOW, R.id.mainLinearlay);
relativeparams.setMargins(0, 20, 0, 0);
relativeparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
submitLay.setLayoutParams(relativeparams);
}
运行我的应用程序时一切正常。但问题是在运行 espresso UI 测试时,我看不到用于在电子邮件编辑文本中输入值的软键盘。这里我提到了我的 Espresso Ui 测试用例代码。
公共类 LoginActivityTest { @规则 public ActivityTestRule mActivityRule = new ActivityTestRule(LoginActivity.class);
@Test
public void login() {
onView(withId(R.id.ripplebtn_step)).perform(click());
Utils.sleep(3000);
//Sign In
onView(withId(R.id.edt_email)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_USER_EMAIL)).perform(closeSoftKeyboard());
onView(withId(R.id.edt_password)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_PASSWORD)).perform(closeSoftKeyboard());
onView(withId(R.id.txt_submit)).perform(click());
}
*** 注意:如果我为我的布局(即我的活动中的 mainrel)评论全局布局监听器,那么我能够执行测试用例。我认为使用 OnGlobalLayoutListener 上下移动按钮存在问题。
有人可以建议/帮助我吗?
public class SampleTest {
@Rule
public IntentsTestRule<StepSignInActivity> mAddIntentsTestRule =
new IntentsTestRule<StepSignInActivity>(SignInActivity.class);
IdlingResource idlingResource;
@Before
public void before() {
idlingResource = new ElapsedTimeIdlingResourc`enter code here`e(5000);
Espresso.registerIdlingResources(idlingResource);
}
@After
public void after() {
Espresso.unregisterIdlingResources(idlingResource);
}
@Test
public void runSequence() {
// this triggers our intent service, as we registered
// Espresso for it, Espresso wait for it to finish
onView(withId(R.id.edt_email)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_USER_EMAIL));
onView(withId(R.id.edt_password)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_PASSWORD)).perform(closeSoftKeyboard());
onView(withId(R.id.txt_submit)).perform(click());
}
【问题讨论】:
-
你定义
IntentTestRule了吗?
标签: android user-interface testing android-softkeyboard android-espresso