device.getCurrentPackagename在espresso测试案例中返回为null

问题描述 投票:0回答:1

这是我的测试方法

@Test
    public void testLeadChangeStageToUnqualifiedReason() throws InterruptedException {
        List<Map<String, Object>> leadData = LeadData.simpleLead();
        lead.createLead(leadData);
        Thread.sleep(3000);
        LeadPage.waitForLandingPage();
        lead.changeStage("Unqualified","Not interested");
        onView(withText("Lead stage changed")).inRoot(new ToastMatcher())
                .check(matches(isDisplayed()));
                    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
            device.wait(Until.findObject(By.res(device.getCurrentPackageName(), "lead_stage_txt")), 7000);
            stage = device.findObject(By.res(device.getCurrentPackageName(), "lead_stage_txt")).getText();
        CommonHelpers.tapLandingBackButton();
        CommonHelpers.tapHomeScreenButton();
        Assert.assertEquals("Stage name differed", stage,"Unqualified");
    }

因此,在验证了Toast消息之后,我试图使用UIObject获取元素的文本,不幸的是,我得到device.getCurrentPackageName()为Null并导致异常

我也尝试过InstrumentationRegistry.getInstrumentation().getUiAutomation().getRootInActiveWindow();,它也返回Null

任何人都可以帮助解决此问题

将根节点设为空

以上也没有用当我打电话给device.findObject时我将rootNode设为null

AccessibilityNodeInfo getRootNode() {
        final int maxRetry = 6;
        long waitInterval = 250;
        AccessibilityNodeInfo rootNode = null;
        for (int x = 0; x < maxRetry; x++) {
            rootNode = mUiAutomatorBridge.getRootInActiveWindow();
            if (rootNode != null) {
                return rootNode;
            }
            if (x < maxRetry - 1) {
                Log.e(LOG_TAG, "Got null root node from accessibility - Retrying...");
                SystemClock.sleep(waitInterval);
                waitInterval *= 2;
            }
        }
        return rootNode;
    }
android-espresso
1个回答
0
投票

您可以尝试使用InstrumentationRegistry.getTargetContext()。getPackageName()吗?您也可以将其包含在const变量中,该变量可以在测试之间重复使用。私有静态最终字符串TARGET_PACKAGE_NAME =InstrumentationRegistry.getTargetContext()。getPackageName();

© www.soinside.com 2019 - 2024. All rights reserved.