我怎么知道是否显示了一个片段(测试UI)
How I know if one fragment is displayed (Test UI)
如果我的片段之一,rattingfragment,我必须执行点击。
如果不是,请执行其他操作。
我怎么知道这是我屏幕上的片段?
Android UI 测试。
在我的代码下面进行测试
编辑
代码:
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(MainActivity.class);
@After
public void setUp(){
Logout();
}
@Test
public void shouldOfferRide(){
SystemClock.sleep(1500);
Login();
onView(withId(R.id.searchButton)).perform(typeText(emailSearch));
onView(withId(R.id.searchButton)).perform(pressImeActionButton());
onView(withId(R.id.community_user_ask_button)).perform(click());
SystemClock.sleep(1500);
onView(withId(R.id.button1)).perform(click());
onView(withId(R.id.send_request)).perform(click());
SystemClock.sleep(1500);
((MainActivity)mActivityRule.getActivity()).navItemClick(4);
SystemClock.sleep(3500);
LoginMyrides();
onView(withId(R.id.my_rides_tab_bar)).perform(click());
SystemClock.sleep(1000);
{I have to know is ratingfragment before perform click below}
onView(withId(R.id.rating_bar)).perform(click());
onView(withId(R.id.deny_btn)).perform(click());
}
public void Login(){
onView(withId(R.id.edt_new_login_email)).perform(typeText(emailLogin));
onView(withText(R.string.next_button)).perform(click());
onView(withId(R.id.edt_new_password)).perform(typeText(senha));
onView(withText(R.string.login_new_pass)).perform(click());
}
public void Logout(){
new SessionManager(mActivityRule.getActivity()).logoutUser();
}
}
If you want to know it in activity, then just use:
Fragment fragment = getFragmentManager().findFragmentByTag(stringTag);
if (fragment != null) {
//here it means the fragment is been shown
}
And If you want to know it in fragment then do your stuff in fragment's any of the following life cycle method:
onAttach();
onCreate();
onCreateView();
onActivityCreated();
onStart(); //or
onResume();
一旦您的片段显示在屏幕上,就会调用这些。参考:https://developer.android.com/guide/components/fragments.html#Creating
在您的片段中使用以下函数:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//fragment is visible on screen...do your stuff here
}
else {
// fragment is no longer visible
}
}
Espresso 专为测试而设计,可以按照配方一步步进行,无需做出任何决定。您应该考虑片段何时可见以及何时不可见的情况。然后为每个场景编写单独的测试。
如果我的片段之一,rattingfragment,我必须执行点击。 如果不是,请执行其他操作。
我怎么知道这是我屏幕上的片段?
Android UI 测试。 在我的代码下面进行测试 编辑 代码:
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(MainActivity.class);
@After
public void setUp(){
Logout();
}
@Test
public void shouldOfferRide(){
SystemClock.sleep(1500);
Login();
onView(withId(R.id.searchButton)).perform(typeText(emailSearch));
onView(withId(R.id.searchButton)).perform(pressImeActionButton());
onView(withId(R.id.community_user_ask_button)).perform(click());
SystemClock.sleep(1500);
onView(withId(R.id.button1)).perform(click());
onView(withId(R.id.send_request)).perform(click());
SystemClock.sleep(1500);
((MainActivity)mActivityRule.getActivity()).navItemClick(4);
SystemClock.sleep(3500);
LoginMyrides();
onView(withId(R.id.my_rides_tab_bar)).perform(click());
SystemClock.sleep(1000);
{I have to know is ratingfragment before perform click below}
onView(withId(R.id.rating_bar)).perform(click());
onView(withId(R.id.deny_btn)).perform(click());
}
public void Login(){
onView(withId(R.id.edt_new_login_email)).perform(typeText(emailLogin));
onView(withText(R.string.next_button)).perform(click());
onView(withId(R.id.edt_new_password)).perform(typeText(senha));
onView(withText(R.string.login_new_pass)).perform(click());
}
public void Logout(){
new SessionManager(mActivityRule.getActivity()).logoutUser();
}
}
If you want to know it in activity, then just use:
Fragment fragment = getFragmentManager().findFragmentByTag(stringTag);
if (fragment != null) {
//here it means the fragment is been shown
}
And If you want to know it in fragment then do your stuff in fragment's any of the following life cycle method:
onAttach();
onCreate();
onCreateView();
onActivityCreated();
onStart(); //or
onResume();
一旦您的片段显示在屏幕上,就会调用这些。参考:https://developer.android.com/guide/components/fragments.html#Creating
在您的片段中使用以下函数:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//fragment is visible on screen...do your stuff here
}
else {
// fragment is no longer visible
}
}
Espresso 专为测试而设计,可以按照配方一步步进行,无需做出任何决定。您应该考虑片段何时可见以及何时不可见的情况。然后为每个场景编写单独的测试。