android UiAutomator 借助数据库查询来验证结果并记录在log中

来源:互联网 发布:孕五个月胎动知男女 编辑:程序博客网 时间:2024/06/06 00:50

如果能在测试中拿到数据库的数据来验证一下脚本执行的结果会更加佐证执行结果。借助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指向要访问的数据库名scutcsString 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);}





0 0
原创粉丝点击