I have a test case that accesses the browser and does some things with it. But I need to programmatically change what is entered for some text fields. I have 8 lines and a boolean that requires the test to run correctly. The problem is that you need to run the test file through ant and the build process before you can run it. I would like it to be possible to build, put on the device and then somehow transfer the data to the device when I call it. But I do not know if this is possible. The only option I can think of now is to write a quick java program to write this to a file, change the variables, and then build it and click, etc. However, this seems like a very difficult solution to my problem. Here is 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) {
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();
}
}
}