1

我正在尝试在以沉浸式模式运行的应用程序上运行测试。

但是,当我尝试单击导航栏通常所在的位置时,什么也没有发生,因为UiDevice我认为屏幕比实际小。

即它认为屏幕是 2076 像素高,而实际上它现在是 2152。有没有办法访问额外的 76 像素的屏幕?

我的应用程序代码的粗略示例如下...

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ScreenShotTest {

private static final String BASIC_SAMPLE_PACKAGE = "my.package.name";

private UiDevice mDevice;

@Before
public void startMainActivityFromHomeScreen() {
    context = InstrumentationRegistry.getTargetContext();
    pref = context.getSharedPreferences("my_preferences", MODE_PRIVATE);

    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Wait for launcher
    final String launcherPackage = getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

    // Launch the blueprint app
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
    context.startActivity(intent);
}

@Test
public void allTests() throws InterruptedException, UiObjectNotFoundException {
    int i = 0;
    boolean mainMenuFinished = false;
    while(!mainMenuFinished && i < 600) {
        i++;
        sleep(1000);
        mainMenuFinished = pref.getBoolean(MAIN_MENU_FINISHED, false);
    }

    int j = 0;
    int[] buttonBounds = new int[2];
    for (String s : pref.getString(name, "").split(",")) {
        buttonBounds[j] = Math.round(Float.parseFloat(s));
        j++;
    }

    mDevice.click(buttonBounds[0], buttonBounds[1]);
}

我在我的实际 apk 中的共享首选项中设置按钮的像素位置,然后我的测试 apk 正在读取该值并在该位置单击。

我的真实应用程序的屏幕宽度为 2152(横向)我的测试 apk 只认为屏幕是 2076。

4

0 回答 0