UiAutomator变化的变量变量、UiAutomator

由网友(不要走太远)分享简介:我有一个测试用例,访问浏览器,并做一些事情吧。但我需要以编程方式更改输入是什么一些文本框。我有8字符串和测试所需要的正常运行一个布尔值。现在的问题是,是,你必须通过蚂蚁和构建过程运行测试的情况下才可以实际运行它。我很想能够建立这一点,把它放在设备上,然后以某种方式传递数据到设备时,我把它。但我不知道这是否是可能的。唯...

我有一个测试用例,访问浏览器,并做一些事情吧。但我需要以编程方式更改输入是什么一些文本框。 我有8字符串和测试所需要的正常运行一个布尔值。现在的问题是,是,你必须通过蚂蚁和构建过程运行测试的情况下才可以实际运行它。我很想能够建立这一点,把它放在设备上,然后以某种方式传递数据到设备时,我把它。但我不知道这是否是可能的。唯一的选择我能想到的此刻,是写一个快速的java程序写这一个文件,改变的变量,然后建立并推动是和这样的。然而,这似乎是一个非常unelegant解决我的问题。这里的code:

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

    公共类AndroidSetupTest扩展UiAutomatorTestCase {

    公共无效testBasic(用户字符串,字符串密码,路由器的字符串,字符串rpassword,布尔基础,
                        IP字符串,字符串子网掩码,网关的字符串,字符串DNS)抛出UiObjectNotFoundException {

    尝试{
    connectToNetwork(路由器);
    UiDevice.getInstance()pressHome()。

    UIObject类chromeLauncher =新UIObject类(新UiSelector()文本(铬)类名(android.widget.TextView));
    chromeLauncher.clickAndWaitForNewWindow();

    UIObject类enterUrl =新UIObject类(新UiSelector()的描述(搜索或输入网址));
    enterUrl.setText(thewebsite);
    UiDevice.getInstance()pressEnter()。

    视频下载(5000);

    UIObject的签到=新UIObject类(新UiSelector()的描述(登录链接)。);
    如果(signIn.exists()){
    signIn.clickAndWaitForNewWindow();

    UIObject类的userName =新UIObject类(。新UiSelector()类名(android.view.View)的索引(7).childSelector(新UiSelector()类名(android.widget.EditText)));
    userName.setText(用户);
    UIObject的密码=新UIObject的(新UiSelector()类名(android.view.View)指数(9).childSelector(新UiSelector()类名(android.widget.EditText))。。。);
    Password.setText(密码+);

     视频下载(500);

     UiDevice.getInstance()pressEnter()。
     }

     enterUrl.setText(另一个网站);

     UiDevice.getInstance()pressEnter()。

     视频下载(1000);

     connectToNetwork(特定网络);


     UiDevice.getInstance()pressHome()。

     chromeLauncher.clickAndWaitForNewWindow();

     视频下载(1000);


     UIObject类setupModule =新UIObject类(新UiSelector()类名(android.view.View)
           .childSelector(新UiSelector()类名(android.view.View)类名(android.widget.Button)。。));

    。getUiDevice()setOrientationNatural();
    视频下载(300);
    setupModule.clickAndWaitForNewWindow(2000);


    视频下载(2000);


    UIObject类chooseAp =新UIObject类(新UiSelector()的描述(选择接入点......));
    chooseAp.clickAndWaitForNewWindow();
    UIObject类pickAp =新UIObject类(新UiSelector()类名(android.widget.ListView)
           .childSelector(新UiSelector()textContains(路由器)));
    pickAp.clickAndWaitForNewWindow();

    UIObject类routerPassword1 =新UIObject类(新UiSelector()类名(android.view.View)。指数(9)
           .childSelector(新UiSelector()类名(android.widget.EditText)。));
  UIObject类routerPassword2 =新UIObject类(新UiSelector()类名(android.view.View)。指数(11)
           .childSelector(新UiSelector()类名(android.widget.EditText)。));

    如果(!基本){
       INT I = 0;
       若i = 4(routerPassword2.exists()!);

       UIObject类selectAdvanced =新UIObject类(新UiSelector()的描述(先进));
       selectAdvanced.click();

       UIObject的IP =新UIObject的(新UiSelector()。类名(android.view.View)。索引(14-i)的
               .childSelector(新UiSelector()类名(android.widget.EditText)。));
       UIObject类网络掩码=新UIObject类(新UiSelector()类名(android.view.View)。指数(16-I)
               .childSelector(新UiSelector()类名(android.widget.EditText)。));
       UIObject的网关=新UIObject类(新UiSelector()类名(android.view.View)。指数(18-I)
               .childSelector(新UiSelector()类名(android.widget.EditText)。));
       UIObject的DNS =新UIObject类(新UiSelector()类名(android.view.View)。指数(20-I)
               .childSelector(新UiSelector()类名(android.widget.EditText)。));
       IP.setText(IP);
       Netmask.setText(子网掩码);
       Gateway.setText(网关);
       DNS.setText(DNS);

   }

   routerPassword1.setText(密码);
   routerPassword2.setText(密码);

   UIObject类finishSetup =新UIObject类(新UiSelector()的描述(提交));
   finishSetup.clickAndWaitForNewWindow();

}赶上(InterruptedException异常E){
    e.printStackTrace();

}赶上(RemoteException的E){
    // TODO自动生成的catch块
    e.printStackTrace();
}
}

 私人无效connectToNetwork(串网)
 {
     尝试{
       UiDevice.getInstance()pressHome()。

       UIObject类设置=新UIObject类(新UiSelector()类名(android.widget.TextView)文本(设置));
       settings.clickAndWaitForNewWindow();

       UIObject的wifiSettings =新UIObject的(。新UiSelector()类名(android.widget.RelativeLayout)指数(1));
       wifiSettings.clickAndWaitForNewWindow();

       UIObject类changeWifi =新UIObject类(新UiSelector()类名(android.widget.TextView)文本(网络));
               changeWifi.clickAndWaitForNewWindow();

       UIObject类连接=新UIObject类(新UiSelector()文本(连接));
       connect.clickAndWaitForNewWindow();

       视频下载(5000);
     }赶上(UiObjectNotFoundException E){
        e.printStackTrace();
    }赶上(InterruptedException异常E){
        e.printStackTrace();
    }
 }


}
 

解决方案

如果我理解你的问题,你有改变的变量。您希望您的测试,以获得这些变量的没有的硬编码呢?如果是这样的话,我会尝试这个。

如何用python实现哑变量的转换

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

 亚行外壳时insrument -e<名称> < VALUE> <封装/亚军>
 

您可以使用捆绑可用,如果你重写 InstrumentationTestRunner 的onCreate 方法来访问值。

 公共无效的onCreate(包savedInstanceState){
      super.onCreate(savedInstanceState);
      值=(字符串)savedInstanceState.get(姓名);
}
 

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. The problem is, is that you have to run the test case through ant and the build process before you can actually run it. 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. 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 onCreatemethod of the InstrumentationTestRunner.

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

阅读全文

相关推荐

最新文章