简体   繁体   English

UiAutomator更改变量

[英]UiAutomator changing variables

I've got a test case that accesses the browser and does some stuff with it. 我有一个测试用例,可以访问浏览器并对其进行一些处理。 But I need to programmatically change what the inputs are for some of the text boxes. 但是我需要以编程方式更改某些文本框的输入内容。 I have 8 strings and a boolean value that the test needs the run properly. 我有8个字符串和一个布尔值,表示测试需要正确运行。 The problem is, is that you have to run the test case through ant and the build process before you can actually run it. 问题是,您必须先通过ant和构建过程运行测试用例,然后才能实际运行它。 I'd love to be able to build this, put it on the device and then somehow pass the data to the device when I call it. 我希望能够构建它,将其放在设备上,然后在调用它时以某种方式将数据传递给设备。 But I don't know if that is possible. 但是我不知道这是否可能。 The only option I can think of at the moment, is to write a quick java program to write this to a file, change the variables, and then build it and push is and such. 目前,我唯一想到的选择是编写一个快速的Java程序,将此程序写入文件,更改变量,然后构建它,然后按“ push”等。 However, that seems like a very unelegant solution to my problem. 但是,这似乎是解决我的问题的非常简单的方法。 Here's the code: 这是代码:

   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();
    }
 }


}

If I understand your question, you have variables that change. 如果我理解您的问题,那么您的变量会发生变化。 You want your test to get these variables without hardcoding it? 您希望您的测试无需硬编码就可以获取这些变量吗? If that is the case, I would try this. 如果是这样,我会尝试一下。

You can send in parameters via command line: 您可以通过命令行发送参数:

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

You can access the value using the bundle that is available if you override the onCreate method of the InstrumentationTestRunner . 如果覆盖InstrumentationTestRunneronCreate方法,则可以使用可用的捆绑包访问值。

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      value = (String) savedInstanceState.get("name");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM