问题
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();
}
}
}
回答1:
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
.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
value = (String) savedInstanceState.get("name");
}
来源:https://stackoverflow.com/questions/20789124/uiautomator-changing-variables