Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Studio中的Ui Automator 2.0项目_Android_Android Studio_Android Uiautomator_Android Instrumentation - Fatal编程技术网

Android Studio中的Ui Automator 2.0项目

Android Studio中的Ui Automator 2.0项目,android,android-studio,android-uiautomator,android-instrumentation,Android,Android Studio,Android Uiautomator,Android Instrumentation,我想在Android Studio中建立一个项目但是,我不想要Android应用程序,只想要测试项目。 在上一次测试之后,我尝试设置一个扩展ActivityInstrumentationTestCase2的类,并从那里开始测试 然而,我在一件事上绊倒了:如果不把它变成一个应用程序,我就不知道如何创建这个项目 创建新项目的选项包括: 启动一个新的Android Studio项目 打开现有项目 导入项目 我做到了: 启动一个新项目,为其命名,设置minSDK并选择“无活动” 打开build.gr

我想在Android Studio中建立一个项目但是,我不想要Android应用程序,只想要测试项目。

在上一次测试之后,我尝试设置一个扩展ActivityInstrumentationTestCase2的类,并从那里开始测试

然而,我在一件事上绊倒了:如果不把它变成一个应用程序,我就不知道如何创建这个项目

创建新项目的选项包括:

  • 启动一个新的Android Studio项目
  • 打开现有项目
  • 导入项目
我做到了:

  • 启动一个新项目,为其命名,设置minSDK并选择“无活动”
  • 打开build.gradle(在app下)并添加本文末尾提到的依赖项和检测信息
  • 在src下打开androidTest并更改主文件:更改为ActivityInstrumentationTestCase2,添加了安装和拆卸;使用Junit4定义的RUNT4(如中所示)
  • 我构建项目(构建成功)-按“操作栏”中构建旁边的绿色箭头
  • 我的问题是:

    • 如何将其安装到设备中
    • 如何在设备中运行它
    • 我需要在AndroidManifest中做些什么吗
    • 我在正确的地方编辑吗?我应该在src/main下做些什么吗
    我很感激安装和运行说明将是如何通过Android Studio和使用命令行来完成的(如果你只知道其中一个,请发布)

    注意:这是我第一次使用Android Studio

    提前谢谢

    编辑:

    现在我可以构建和运行,但它告诉我我没有要运行的测试(空的测试套件)。这是我的毕业证书和代码

    我的build.graddle如下所示:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 22
        buildToolsVersion "21.1.2"
    
        defaultConfig {
            applicationId "androidexp.com.ceninhas"
            minSdkVersion 21
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
          testInstrumentationRunner="android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        packagingOptions {
            exclude 'LICENSE.txt'
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
    }
    
    我的源代码(在src/androidTest/java/package下)是:


    我做错了什么?

    我也在使用AndroidStudio的uiautomator 2.0。以下是你的问题的一些答案

    如何将其安装到设备中? 如何在设备中运行它

    确保您的设备已使用连接

    adb devices
    
    如果没有,则必须使用

    adb kill-server
    adb connect xxx.xxx.xxx.xxx
    
    然后在AndroidStudio中,右键单击您的测试类并单击“运行您的测试用例”

    我需要在AndroidManifest中做些什么吗

    我的舱单上没有什么特别的东西,但你一定要加上

    android {
        defaultConfig {
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    }
    
    在你的身材里,格雷德尔

    我在正确的地方编辑吗?我应该在src/main下做些什么吗

    是的,您正在正确的位置编辑。但是您可以将代码移动到
    src/main
    。为此,您需要在build.gradle文件中将
    androidTestCompile
    更改为
    compile

    我还并没有尝试从命令行运行测试,但你们可以看到AndroidStudio命令,也许它会有所帮助

    我希望这对你有帮助

    编辑1

    我使用这个代码

    build.gradle(projectRoot) LoginTestCase(projectRoot/src/main/LoginTestCase.java)
    实际上,您不应该以这种方式编写测试代码。只需将代码保存在src/androidTest文件夹下,并编写如下测试代码:

    @RunWith(AndroidJUnit4.class)
    @SdkSuppress(minSdkVersion = 18)
    public class ChangeTextBehaviorTest {
    
        private static final String BASIC_SAMPLE_PACKAGE
                = "com.example.android.testing.uiautomator.BasicSample";
        private static final int LAUNCH_TIMEOUT = 5000;
        private static final String STRING_TO_BE_TYPED = "UiAutomator";
        private UiDevice mDevice;
    
        @Before
        public void startMainActivityFromHomeScreen() {
            // Initialize UiDevice instance
            mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    
            // Start from the home screen
            mDevice.pressHome();
    
            // Wait for launcher
            final String launcherPackage = mDevice.getLauncherPackageName();
            assertThat(launcherPackage, notNullValue());
            mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
                    LAUNCH_TIMEOUT);
    
            // Launch the app
            Context context = InstrumentationRegistry.getContext();
            final Intent intent = context.getPackageManager()
                    .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
            // Clear out any previous instances
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
    
            // Wait for the app to appear
            mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
                    LAUNCH_TIMEOUT);
        }
        @Test
        public void checkPreconditions() {
            assertThat(mDevice, notNullValue());
        }
    
        @Test
        public void testChangeText_sameActivity() {
            // Type text and then press the button.
            mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput"))
                    .setText(STRING_TO_BE_TYPED);
            mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "changeTextBt"))
                    .click();
    
            // Verify the test is displayed in the Ui
            UiObject2 changedText = mDevice
                    .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "textToBeChanged")),
                            500 /* wait 500ms */);
            assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED)));
        }
    }
    

    详情请注意:

    你是说我应该还是可以在src/main下完成?目前我是在src/androidTest下做的?我能够建造和运行(按照您的指示,但它告诉我没有要运行的测试。我将使用build.graddle和我的代码编辑我的问题抱歉,我的答案很混乱。您可以移动它。明天我将使用一个工作示例编辑我的答案WBTW,我的测试只是一个断言,我还没有做任何特定于UiAutomator的操作,我只是希望能够运行它并然后我会做更具体的测试。所以你可以尝试将它移动到
    src/main
    并通过
    compile
    更改
    AndroidCompileTest
    。你是如何将appName发送给它的?你在哪里定义它?嗨@inês,我正在尝试做同样的事情…迁移我现有的仅测试的uiautomator项目。你能成功完成你的项目吗?…你在github上有项目吗?我是按照Maloubobola的答案做的,我标记为正确:)所讨论的项目不在github中。
    android {
        defaultConfig {
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    }
    
    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.0"
    
        lintOptions {
            abortOnError false
        }
        packagingOptions {
            exclude 'NOTICE'
            exclude 'LICENSE.txt'
        }
        defaultConfig {
            minSdkVersion 19
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.0.0'
        compile 'com.android.support.test:testing-support-lib:0.1'
        compile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
        compile project(':aiccore')
    }
    
    public class LoginTestCase extends InstrumentationTestCase {
    
        protected UiDevice device = null;
        protected String appName;
    
        public LoginTestCase() {
            this("YourAppName")
        }
    
        public LoginTestCase(String appName) {
            this.appName = appName;
        }
    
        public void runApp(String appName) throws UiObjectNotFoundException, RemoteException {
            device = UiDevice.getInstance(getInstrumentation());
            device.pressHome();
            device.waitForWindowUpdate("", 2000);
    
            UiObject2 allAppsButton = device.findObject(By.desc("Apps"));
            allAppsButton.click();
            device.waitForWindowUpdate("", 2000);
    
            UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
            appViews.setAsHorizontalList();
    
            UiObject settingsApp = appViews.getChildByText(new UiSelector().className(TextView.class.getName()), appName);
            settingsApp.clickAndWaitForNewWindow();
    
            assertTrue("Unable to detect app", settingsApp != null);
        }
    
        @Override
        public void setUp() throws RemoteException, UiObjectNotFoundException {
            this.runApp(appName);
        }
    
        @Override
        public void tearDown() throws RemoteException, UiObjectNotFoundException {
            //Empty for the moment
        }
    
        public void testUS1() {
            UiObject2 usernameLabel = device.findObject(By.clazz(TextView.class.getName()).text("Username"));
            assertTrue("Username label not found", usernameLabel != null);
        }
    
    @RunWith(AndroidJUnit4.class)
    @SdkSuppress(minSdkVersion = 18)
    public class ChangeTextBehaviorTest {
    
        private static final String BASIC_SAMPLE_PACKAGE
                = "com.example.android.testing.uiautomator.BasicSample";
        private static final int LAUNCH_TIMEOUT = 5000;
        private static final String STRING_TO_BE_TYPED = "UiAutomator";
        private UiDevice mDevice;
    
        @Before
        public void startMainActivityFromHomeScreen() {
            // Initialize UiDevice instance
            mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    
            // Start from the home screen
            mDevice.pressHome();
    
            // Wait for launcher
            final String launcherPackage = mDevice.getLauncherPackageName();
            assertThat(launcherPackage, notNullValue());
            mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
                    LAUNCH_TIMEOUT);
    
            // Launch the app
            Context context = InstrumentationRegistry.getContext();
            final Intent intent = context.getPackageManager()
                    .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
            // Clear out any previous instances
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
    
            // Wait for the app to appear
            mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
                    LAUNCH_TIMEOUT);
        }
        @Test
        public void checkPreconditions() {
            assertThat(mDevice, notNullValue());
        }
    
        @Test
        public void testChangeText_sameActivity() {
            // Type text and then press the button.
            mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput"))
                    .setText(STRING_TO_BE_TYPED);
            mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "changeTextBt"))
                    .click();
    
            // Verify the test is displayed in the Ui
            UiObject2 changedText = mDevice
                    .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "textToBeChanged")),
                            500 /* wait 500ms */);
            assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED)));
        }
    }