概要
继承与接口(图片)
接口实现(图片)
文件管理器冒烟测试实例 代码演示
演示代码
文件流
文件与多线程
演示代码
package com.threadAndfile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class LogcatThread implements Runnable{
public BufferedWriter bw = null;
BufferedReader br =null;
private String pathdir ="/data/local/tmp"; //文件夹路径
private String path ="";
public static void main(String[] args) {
}
//run方法就是另一个线程要做的事情
@Override
public void run() {
//File.separator 等价于 /
path = pathdir+File.separator+"test.txt";
try {
//清除系统旧的的logcat
Runtime.getRuntime().exec("logcat -c");
//创建文件夹
//Runtime.getRuntime().exec("mkdir"+pathdir);
//创建文件夹
Runtime.getRuntime().exec("touch"+path);
//获取logcat 按照时间格式输出
Process process = Runtime.getRuntime().exec("logcat -v time");
br =new BufferedReader(new InputStreamReader(process.getInputStream()));
String line ="";
while((line=br.readLine())!=null){
System.out.println(line);
savefile(line,path);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("resource")
public BufferedWriter savefile(String line,String path){
File file = new File(path);
try {
//获取文件流
FileOutputStream fs = new FileOutputStream(file,true);
//字符流保存成字符
OutputStreamWriter out = new OutputStreamWriter(fs);
//包装成buffered 使用它的方法逐行写入
bw = new BufferedWriter(out);
//一行行写入
bw.append(line);
//新建一行
bw.newLine();
//刷新,把字符刷进内存
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bw;
}
}
package com.threadAndfile;
import android.provider.Contacts.Intents.UI;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.configurator.UiAutomatorHelper;
public class LogcatCase extends UiAutomatorTestCase {
public static void main(String[] args) {
String jarName, testClass, testName, androidId;
jarName ="Demo";
testClass ="com.threadAndfile.LogcatCase";
testName ="testLog";
androidId ="1";
new UiAutomatorHelper(
jarName, testClass, testName, androidId);
}
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
//启动线程
new Thread(new LogcatThread()).start();
}
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
}
public void testLog(){
for (int i = 0; i <10; i++) {
UiDevice.getInstance().pressMenu();
}
}
}
网络编程
Socket通讯基本模型(图片)
代码演示
服务端
package com.server.socket;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.ProcessBuilder.Redirect;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @param args
* @throws IOException
*/
//服务器
public static void main(String[] args) throws IOException {
//端口号 (尽量大一点,以免和系统端口冲突)
int port =9998;
final ServerSocket server = new ServerSocket(port);
System.out.println("The server is runing...");
System.out.println(server);
//使用while 无限循环监听
while(true){
//accept() 的特性是阻塞性,如没有接受到,会一直停在这里
final Socket socket = server.accept();
//新建一个线程用来处理
new Thread(new Runnable() {
@Override
//处理事件
public void run() {
Reader reader;
try {
reader = new InputStreamReader(socket.getInputStream());
char[] chars = new char[64];
int len;
//当读取的内容在不断变化的时候,使用StringBuilde更好
StringBuilder sb = new StringBuilder();
//不等于-1 代表读完了
while((len=reader.read(chars))!=-1){
//网路封包是有固定长度,所以要使用固定长度读取
sb.append(new String(chars,0,len));
}
//打印出结果
System.out.println("From client:"+sb);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
}
客户端
package snippet;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.net.UnknownHostException;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class SocketCase extends UiAutomatorTestCase{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String jarName, testClass, testName, androidId ;
jarName ="Demo";
testClass ="snippet.SocketCase";
testName ="testSSocket";
androidId ="1";
new UiAutomatorHelper
(jarName, testClass, testName, androidId);
}
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
}
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
}
/*目标任务
收集测试信息,自动向服务器发送消息
*
* */
public void testSSocket(){
//指定发送的消息内容
String classname = getClass().toString(); //获取到当前的类名
String testname = getName().toString(); //用例名
String flag = "mms test"; //附加的消息
String mms = classname+","+testname+","+flag;
sendMMSSocket(mms,"192.168.63.2", 9998);
}
//发送消息的方法
public void sendMMSSocket(String mms,String host,int port){
Socket client;
Writer writer;
try {
client = new Socket(host,port);
//使用文件流向服务器输出
writer = new OutputStreamWriter(client.getOutputStream());
writer.write(mms);
writer.flush();
writer.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void testDemo(){
}
}