测试主屏幕小部件的最佳方法

问题描述 投票:5回答:2

测试android主屏幕小部件的最佳方法是什么?很难找到任何示例代码:/哪些框架支持该测试?

  1. 浓咖啡
  2. 机械人
  3. 其他
android android-widget android-testing android-espresso
2个回答
3
投票

尝试UIAutomator使用android-sdk / tools / uiautomatorviewer获取小部件属性

    import android.test.InstrumentationTestCase;
    import android.support.test.uiautomator.UiDevice;
    import android.support.test.uiautomator.By;

    public class CalculatorUiTest extends InstrumentationTestCase {

        private UiDevice mDevice;

        public void setUp() {
            // Initialize UiDevice instance
            mDevice = UiDevice.getInstance(getInstrumentation());

            // Start from the home screen
            mDevice.pressHome();

            UiObject widgetButton = mDevice.findObject(new UiSelector()
            .text("OK"))
            .className("android.widget.Button"));

            widgetButton.clickAndWaitForNewWindow();
        }
}

Android Testing Support Library


0
投票

2020年5月更新

这是我在项目中的工作方式

添加测试依赖项

//Testing
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:core:1.3.0-beta01';
androidTestImplementation 'androidx.test:runner:1.3.0-beta01';

// UiAutomator Testing
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0';
androidTestImplementation 'org.hamcrest:hamcrest-integration:1.3'

在Android项目视图中

转到src-> androidTest-> java-> packagename添加新的测试类

现在只需使用UIAutomator即可通过按下主屏幕按钮导航到启动器的主屏幕。

在主屏幕上,如果添加了小部件,则可以使用findObject方法获取小部件的视图。一旦有了RemoteView的视图对象,就可以做任何您想做的事情。

这是一个简单的小部件测试用例示例,用于检查是否添加了小部件并启动您的应用程序:

import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

@SdkSuppress(minSdkVersion = 18)
public class WidgetAddedTest {
    private static final String APP_PACKAGE_NAME = "com.example.myweather";
    private static final int LAUNCH_TIMEOUT = 5000;
    private UiDevice device;

    @Before
    public void startMainActivityFromHomeScreen() {
        // Initialize UiDevice instance
        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        // Start from the home screen
        device.pressHome();

        // Wait for launcher
        final String launcherPackage = device.getLauncherPackageName();
        assertThat(launcherPackage, notNullValue());
        device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
                LAUNCH_TIMEOUT);
    }
    @Test
    public void widgetAddedTest() {
        //Check text on Widget
        UiObject2 titleTextView = device.findObject(By.res(APP_PACKAGE_NAME, "title"));
        assertEquals("Weather", titleTextView.getText());

        //CHeck correct value on Widget
        UiObject2 temperatureTextView = device.findObject(By.res(APP_PACKAGE_NAME, "temperatureTextView"));
        assertEquals("20", temperatureTextView.getText());

        //Launch App From Widget Test
        temperatureTextView.click();

        // Wait for the app to appear
        device.wait(Until.hasObject(By.pkg(APP_PACKAGE_NAME).depth(0)),
                LAUNCH_TIMEOUT);

        //Check if the app is launched from the widget
        UiObject2 toolBar = device.findObject(By.res(APP_PACKAGE_NAME, "toolBar"));
        assertEquals("My Weather", toolBar.getText());

    }


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