【问题标题】:is it possible to use inheritance in uiautomator? if so how?是否可以在 uiautomator 中使用继承?如果是这样怎么办?
【发布时间】:2016-07-20 05:42:46
【问题描述】:

当我尝试实例化 testAYLogout() 并运行应用程序时,testAXEndTrip() 也会运行。这是为什么呢?

public class ApplicationTest extends UiAutomatorTestCase {

    public void testAYLogout() throws RemoteException, UiObjectNotFoundException {
        //Get the device state
        UiDevice myDevice = getUiDevice();
        //menu button
        UiObject menuButton = new UiObject(new UiSelector().className(android.widget.ImageButton.class.getName()).index(0));
        //logout button
        UiObject logoutButton = new UiObject(new UiSelector().className(android.widget.CheckedTextView.class.getName()).text("Logout"));
        //yes button
        UiObject yesButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("Yes"));
        //no button
        UiObject noButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("No"));
        UiObject loginText = new UiObject(new UiSelector().className(android.widget.TextView.class.getName()).text("Login"));

        if (!myDevice.isScreenOn()) {
            myDevice.wakeUp();
        }

        //click menu button and wait for new window
        menuButton.clickAndWaitForNewWindow();
        assertTrue(logoutButton.exists());
        //click logout
        logoutButton.clickAndWaitForNewWindow();
        //click no
        noButton.clickAndWaitForNewWindow();
        menuButton.clickAndWaitForNewWindow();
        logoutButton.clickAndWaitForNewWindow();
        //click yes button
        yesButton.clickAndWaitForNewWindow();
        assertTrue(loginText.exists());

    }

public void testAXEndTrip() throws RemoteException, UiObjectNotFoundException {


        //get device
        UiDevice myDevice = getUiDevice();
        //pick up location feild
        UiObject okButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("Ok").index(0));
        //end trip button
        UiObject endTripButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("END TRIP").index(1));
        //no button
        UiObject noButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("No"));
        //yes button
        UiObject yesButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("Yes"));
        //submit button
        UiObject submitButton = new UiObject(new UiSelector().className(android.widget.Button.class.getName()).text("Submit"));
        //rate button
        UiObject rateButton = new UiObject(new UiSelector().className(android.widget.ImageButton.class.getName()).index(4));
        //feedback field
        UiObject feedback = new UiObject(new UiSelector().className(android.widget.EditText.class.getName()).index(1));
        //map
        UiObject map = new UiObject(new UiSelector().className(android.view.View.class.getName()).index(0));

        //wake up device if screen is off
        if (!myDevice.isScreenOn())
            myDevice.wakeUp();

        rateButton.waitForExists(5000);
        rateButton.clickAndWaitForNewWindow();
        feedback.setText("ride or die ma niguh");
        submitButton.clickAndWaitForNewWindow();
        assertTrue(map.exists());

    }

}

那么这是我的主要课程

public class MainTest extends ApplicationTest{

    public static void TestMain() throws RemoteException, UiObjectNotFoundException {
        MainTest login = new MainTest();
        login.testAYLogout();

    }
}

【问题讨论】:

    标签: java android testing android-testing android-uiautomator


    【解决方案1】:

    你应该使用Testing Support Library

    首先将 uiautomator 依赖添加到你的 gradle 文件中

    dependencies {
        ...
        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    }
    

    然后,创建一个像这样的测试用例类

    @RunWith(AndroidJUnit4.class)
    @SdkSuppress(minSdkVersion = 18)
    public class MySampleTest extends MyAbstractTest {
       ...
    
       @Test
       public void myTest() {
           ...
       }
    
    }
    

    最后,运行测试。确保将 AndroidJUnitRunner 指定为项目中的默认检测运行器。

    android {
        defaultConfig {
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    }
    

    【讨论】:

    • 先生,我已经这样做了,我的应用程序正在运行并且没有任何问题。但是由于android是java,是否可以使用继承?
    • 找出我做错的地方感谢大家的帮助。
    • 如果您处于相同情况的其他人知道问题所在,将会很有帮助。如果这是解决方案,请接受关闭它的答案。
    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    相关资源
    最近更新 更多