android UiAutomator 藉助數據庫查詢來驗證結果並記錄在log中

如果能在測試中拿到數據庫的數據來驗證一下腳本執行的結果會更加佐證執行結果。藉助UiAutomatorhelper調試,把查詢的結果一道寫入到log文件中就方便多了。本文參考了一篇博客文章對裏面的代碼做了一些修改。

原文地址:http://qq163230530.blog.163.com/blog/static/4289250620081186262719/

我的代碼如下:比較粗糙,有具體需求了可以再詳細改一下。

package mytest;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.*;


public class JEBCTest {
public static void main(String[] args){
String table ="", column = "";int mobile = 0;
new JEBCTest(table, column, mobile);
}
public JEBCTest(String table, String column, int mobile) {
// 驅動程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要訪問的數據庫名scutcs
String url = "jdbc:mysql://192.168.1.14:3306/DZJY";
// MySQL配置時的用戶名
String user = "root"; 
// MySQL配置時的密碼
String password = "******";
System.out.println("-----------------");
try {
// 加載驅動程序
Class.forName(driver);
// 連續數據庫
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed()) 
System.out.println("Succeeded connecting to the Database!");
// statement用來執行SQL語句
Statement statement = conn.createStatement();
// 要執行的SQL語句
String sql = "select * from "+ table + " where id = "+ mobile;
// System.out.println(sql);
            // 結果集
ResultSet rs = statement.executeQuery(sql);
            System.out.println("查詢結果如下所示:");
//            System.out.println("" + "\t" + "");
            String name = null;
            while(rs.next()) {
            // 選擇列數據
            name = rs.getString(column);
            // 輸出結果
            System.out.println(rs.getString("id") + "\t" + name);
            saveToFile(rs.getString("id") + "\t" + name, "runlog.log", false);
            }
            rs.close();
            conn.close();
            } catch(ClassNotFoundException e) {
            System.out.println("Sorry,can`t find the Driver!"); 
            e.printStackTrace();
            } catch(SQLException e) {
            e.printStackTrace();
            } catch(Exception e) {
            e.printStackTrace();
            } 



}





public void saveToFile(String text, String path, boolean isClose) {
File file = new File("runlog.log");
BufferedWriter bf = null;
try {
FileOutputStream outputStream = new FileOutputStream(file, true);
OutputStreamWriter outWriter = new OutputStreamWriter(outputStream);
bf = new BufferedWriter(outWriter);
bf.append(text);
bf.newLine();
bf.flush();
if (isClose) {
bf.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}






在調試中的代碼如下:

public static void main(String[] args){
new UiAutomatorHelper("Demo", "mytest.Test", "testTest", "1");
new JEBCTest("users", "full_name", 545);
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章