UiAutomator改变变量

时间:2013-12-26 18:03:00

标签: java android uiautomator

我有一个访问浏览器的测试用例并用它来做一些事情。但我需要以编程方式更改某些文本框的输入内容。 我有8个字符串和一个布尔值,测试需要正确运行。问题是,您必须先通过ant和构建过程运行测试用例才能实际运行它。我很乐意能够构建它,将它放在设备上,然后在我调用它时以某种方式将数据传递给设备。但我不知道这是否可行。我现在唯一能想到的选择是编写一个快速java程序将其写入文件,更改变量,然后构建它,push就是这样。然而,这对我的问题来说似乎是一个非常不优雅的解决方案。这是代码:

   import android.os.RemoteException;
    import com.android.uiautomator.core.UiDevice;
    import com.android.uiautomator.core.UiObject;
    import com.android.uiautomator.core.UiObjectNotFoundException;
    import com.android.uiautomator.core.UiSelector;
    import com.android.uiautomator.testrunner.UiAutomatorTestCase;

    public class AndroidSetupTest extends UiAutomatorTestCase {   

    public void testBasic(String user, String password, String router, String rpassword, boolean Basic,
                        String ip, String netmask, String gateway, String dns) throws    UiObjectNotFoundException {   

    try{  
    connectToNetwork(router);
    UiDevice.getInstance().pressHome();

    UiObject chromeLauncher = new UiObject(new UiSelector().text("Chrome").className("android.widget.TextView"));
    chromeLauncher.clickAndWaitForNewWindow();

    UiObject enterUrl = new UiObject(new UiSelector().description("Search or type url"));
    enterUrl.setText("thewebsite");
    UiDevice.getInstance().pressEnter();

    Thread.sleep(5000);

    UiObject signIn = new UiObject(new UiSelector().description("SIGN IN Link"));
    if(signIn.exists()){
    signIn.clickAndWaitForNewWindow();

    UiObject userName = new UiObject(new UiSelector().className("android.view.View").index(7).childSelector(new UiSelector().className("android.widget.EditText")));
    userName.setText(user);
    UiObject Password = new UiObject(new UiSelector().className("android.view.View").index(9).childSelector(new UiSelector().className("android.widget.EditText")));
    Password.setText(password + " ");

     Thread.sleep(500);

     UiDevice.getInstance().pressEnter();
     }

     enterUrl.setText("another website");

     UiDevice.getInstance().pressEnter();

     Thread.sleep(1000);

     connectToNetwork("specific network");


     UiDevice.getInstance().pressHome();

     chromeLauncher.clickAndWaitForNewWindow();

     Thread.sleep(1000);


     UiObject setupModule = new UiObject(new UiSelector().className("android.view.View")
           .childSelector(new       UiSelector().className("android.view.View").className("android.widget.Button")));

    getUiDevice().setOrientationNatural();
    Thread.sleep(300);
    setupModule.clickAndWaitForNewWindow(2000);


    Thread.sleep(2000);


    UiObject chooseAp = new UiObject(new UiSelector().description("Choose an access point..."));
    chooseAp.clickAndWaitForNewWindow();
    UiObject pickAp = new UiObject (new UiSelector().className("android.widget.ListView")
           .childSelector(new UiSelector().textContains(router)));
    pickAp.clickAndWaitForNewWindow();

    UiObject routerPassword1 = new UiObject(new UiSelector().className("android.view.View").index(9)
           .childSelector(new UiSelector().className("android.widget.EditText")));
  UiObject routerPassword2 = new UiObject(new UiSelector().className("android.view.View").index(11)
           .childSelector(new UiSelector().className("android.widget.EditText")));

    if(!Basic){
       int i = 0;
       if (!routerPassword2.exists()) i = 4;

       UiObject selectAdvanced = new UiObject(new UiSelector().description("Advanced"));
       selectAdvanced.click();

       UiObject IP = new UiObject(new UiSelector().className("android.view.View").index(14-i)
               .childSelector(new UiSelector().className("android.widget.EditText")));
       UiObject Netmask = new UiObject(new UiSelector().className("android.view.View").index(16-i)
               .childSelector(new UiSelector().className("android.widget.EditText")));
       UiObject Gateway = new UiObject(new UiSelector().className("android.view.View").index(18-i)
               .childSelector(new UiSelector().className("android.widget.EditText")));
       UiObject DNS = new UiObject(new UiSelector().className("android.view.View").index(20-i)
               .childSelector(new UiSelector().className("android.widget.EditText")));
       IP.setText(ip);
       Netmask.setText(netmask);
       Gateway.setText(gateway);
       DNS.setText(dns);

   }

   routerPassword1.setText(password);
   routerPassword2.setText(password);

   UiObject finishSetup = new UiObject(new UiSelector().description("Submit"));
   finishSetup.clickAndWaitForNewWindow();

} catch (InterruptedException e) {
    e.printStackTrace();

} catch (RemoteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

 private void connectToNetwork(String Network)
 {
     try{
       UiDevice.getInstance().pressHome();

       UiObject settings = new UiObject(new UiSelector().className("android.widget.TextView").text("Settings"));
       settings.clickAndWaitForNewWindow();

       UiObject wifiSettings = new UiObject(new UiSelector().className("android.widget.RelativeLayout").index(1));
       wifiSettings.clickAndWaitForNewWindow();

       UiObject changeWifi = new UiObject(new UiSelector().className("android.widget.TextView").text(Network));
               changeWifi.clickAndWaitForNewWindow();

       UiObject connect = new UiObject(new UiSelector().text("Connect"));
       connect.clickAndWaitForNewWindow();

       Thread.sleep(5000);
     } catch (UiObjectNotFoundException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
 }


}

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你就会有变化的变量。你希望你的测试得到这些变量而不用硬编码吗?如果是这样的话,我会试试这个。

您可以通过命令行发送参数:

adb shell am insrument -e <NAME> <VALUE> <package/runner>

如果您覆盖onCreate的{​​{1}}方法,则可以使用可用的捆绑包来访问该值。

InstrumentationTestRunner