2014-12-17 17 views
9

Tôi đã bật nút trang chủ để trở về giao diện trước đó. Đơn giản chỉ cần, làm điều này:Làm cách nào để kiểm tra nút trang chủ trên Thanh tác vụ bằng Espresso?

getActionBar().setDisplayHomeAsUpEnabled(true);

Tôi đang sử dụng phiên bản cuối cùng của com.android.support:appcompat-v7:21.0.2. Tuy nhiên, khi tôi sử dụng mã dưới đây, nó không hoạt động ném một ngoại lệ.

Espresso.onView(ViewMatchers.withId(android.R.id.home)).perform(ViewActions.click()); Espresso.onView(ViewMatchers.withId(R.id.home)).perform(ViewActions.click());

Ngoại lệ:

com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: is <2131296261> ...

+0

Hmm, thú vị. Tôi sử dụng cùng một mã để thực hiện tác vụ trên nút trang chủ: onView (withId (android.R.id.home)).thực hiện (nhấp()); Rất tiếc, chúng tôi vẫn đang sử dụng appcompat-v7: 20.0. Có lẽ điều này có liên quan đến thanh công cụ android mới? Bạn nhận được ID gì, nếu bạn sẽ kiểm tra chế độ xem bằng Màn hình Android? – Christopher

+0

Rõ ràng, không có id với Thanh công cụ Android mới, thay vì chúng tôi sử dụng "Điều hướng": ( –

Trả lời

4

tôi đã thực hiện giải pháp sau đây:

private String getString(int resId){ 
    return getInstrumentation().getTargetContext().getString(resId); 
} 

public void testUI() { 
    onView(withContentDescription(getString(R.string.navigation_drawer_open))).perform(click()); 
    onView(withContentDescription(getString(R.string.navigation_drawer_close))).perform(click()); 
} 

Về cơ bản tôi sử dụng thuộc tính mô tả nội dung thay vì xem id.

0

tôi trả lời bản thân mình. Theo bài đăng trên click home icon with espresso, Không thể chỉ định một Id cho nút Trang chủ trong ActionBar, ít nhất là với phiên bản 7 của thư viện Hỗ trợ, vì vậy chúng tôi nên sử dụng "Điều hướng". Nhưng tại sao?

Đây là lý do nhờ vào lỗi dấu vết Espresso:

--------> ActionMenuView 
      { 
      id=-1, visibility=VISIBLE, width=144, height=168, has-focus=false, 
      has-focusable=true, has-window-focus=true, is-clickable=false, 
      is-enabled=true, is-focused=false, is-focusable=false, 
      is-layout-requested=false, is-selected=false, root-is-layout-requested=false, 
      has-input-connection=false, x=936.0, y=0.0, child-count=1 
      } 
| 
--------> ActionMenuItemView 
      { 
      id=2131296554, res-name=general, desc=, visibility=VISIBLE, 
      width=144, height=144, has-focus=false, has-focusable=true, 
      has-window-focus=true, is-clickable=true, is-enabled=true, 
      is-focused=false, is-focusable=true, is-layout-requested=false, 
      is-selected=false, root-is-layout-requested=false, 
      has-input-connection=false, x=0.0, y=12.0, text=, input-type=0, ime-target=false 
      } 
| 
--------> ImageButton 
      { 
      id=-1, desc=Navigate up, visibility=VISIBLE, width=168, height=168, 
      has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, 
      is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, 
      is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0 
      } 
| 
-1

Tôi không biết đã có một sự khác biệt lớn sử dụng thanh hành động appCompat của. Như các bạn đã viết trong các bình luận "điều hướng lên" thực sự hoạt động. Đối với những người, như bản thân mình, không thể nghĩ làm thế nào để làm điều đó bằng espresso, đây là cách:

import static android.support.test.espresso.Espresso.onView; 
import static android.support.test.espresso.action.ViewActions.click; 
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; 

onView(withContentDescription("Navigate up")).perform(click());

Depracated cho AppCompat v7

Trong dự án của tôi Tôi sử dụng Robotium và Espresso cùng nhau. Đối với nút bấm nhà tôi sử dụng Robotiums Solo#clickOnActionBarHomeButton. Có nhiều hơn 1 dòng như trong click home icon with espresso nhưng bạn không phải chỉ định tiêu đề. (Nó có thể hữu ích trong một số trường hợp đặc biệt ...). Dù sao tôi quyết định cái nào để sử dụng theo SDK phiên bản:

public static void actionBarBack(Instrumentation instrumentation, final Solo solo, String actionBarText) { 
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { 
     onView(allOf(withText(actionBarText), isDisplayed())).perform(click()); 
    } 
    else { 
     instrumentation.runOnMainSync(new Runnable() { 
      @Override 
      public void run() { 
       solo.clickOnActionBarHomeButton(); 
      } 
     }); 
    } 
} 

Dưới đây là cách sử dụng:

Compatibility.actionBarBack(getInstrumentation(), solo, R.string.any); 

Nó phải được bọc trong runOnMainSync vì khuôn khổ kiểm tra Robotium là một chút lazier chút và nếu bạn có xác nhận, ví dụ cho tiêu đề thanh hành động, ngay sau tuyên bố nó sẽ vẫn không quay trở lại. Nhưng chỉ có thể thử với solo.clickOnActionBarHomeButton();. Nó có thể có thể làm việc cho bạn.

14

Có thể nhấp vào nút khi bạn sử dụng mô tả từ tài nguyên.

onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click()); 

trình tốt với appcompat-v7: 23.1.1

+0

Hãy nhớ rằng 'R.string.abc_action_bar_up_description' là riêng tư và có thể thay đổi bất cứ lúc nào. Nhưng hiện tại, đây là lựa chọn tốt nhất tôi có thể tìm thấy. –

Các vấn đề liên quan