robotium的好处就是可以做Android的白盒自动化,你想要什么自己写就行了,前两天写了一个获取Android系统文件的demo,现在附上
工程文件:
public class MainActivity extends Activity {
private Button btn;
public String sdcardPath = Environment.getExternalStorageDirectory()
.getPath();
public String filePath = sdcardPath + "/fyy";
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
tv = (TextView) findViewById(R.id.tv);
btn.setOnClickListener(new BtnClick());
System.out.println("filePath:" + sdcardPath);
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
System.out.println("filepath1:" + sdcardPath);
System.out.println("filePaht:" + filePath);
}
//点击写文件按钮
class BtnClick implements OnClickListener {
File fileOnCreate;
public void onClick(View v) {
fileOnCreate = new File(filePath + "/" + "a.txt");
System.out.println("**********" + filePath + "/" + "a.txt");
if (!fileOnCreate.exists()) {
try {
fileOnCreate.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(fileOnCreate);
fos.write("hello world".getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
getText();
} catch (IOException e) {
System.out.println("找不到系统文件");
e.printStackTrace();
}
}
//获取文件内容
public String getText() throws IOException {
File fileread = new File(filePath + "/" + "a.txt");
FileInputStream fis = new FileInputStream(fileread);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String renadtex = null;
while ((renadtex = br.readLine()) != null) {
tv.setText(renadtex);
}
return renadtex;
}
}
上面的内容主要做了两个操作,第一步是点击按钮后创建一个文件,创建文件的同时获取文件的内容并写到textview上
下面是测试程序,获取文件的内容:
//测试方法,点击按钮获取文本内容
public void testGetText() throws Exception {
solo.assertCurrentActivity("进入首页", MainActivity.class);
solo.clickOnView((Button) solo.getView(R.id.btn));
solo.sleep(1000);
String s = getText();
if (solo.getText(s) != null) {
System.out.println("*****resule*****" + "成功"+s);
}
}
/**
* 获取文本内容方法
*/
public String getText() throws IOException {
String getTxt = null;
String renadtex = null;
File fileread = new File(filePath + "/" + "a.txt");
FileInputStream fis = new FileInputStream(fileread);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
while ((renadtex = br.readLine()) != null) {
getTxt = renadtex;
}
return getTxt;
}
以上内容是获取txt文本的方法,获取xml的内容的话只需要建立xml的factory就可以原理相同,后续增加