1 sqlite下載及安裝html
2 安裝firefox插件sqliteManagerjava
3 新建數據庫表web
4 添加sqlite jar包sql
5 數據庫操做代碼數據庫
package com.selenium.tool; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DB { private Connection conn = null; private Statement stat = null; private String fileName = ".//sqlite/selenium.sqlite"; public DB(){ this.conn(); } public void conn(){ try { Class.forName("org.sqlite.JDBC"); conn = DriverManager.getConnection("jdbc:sqlite:"+fileName); stat = conn.createStatement(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public String getXpath(String tableName,String itemName){ String xpath = null; String sql = "select * from " + tableName + " where name = '" + itemName + "';"; try { ResultSet rs = stat.executeQuery(sql); while (rs.next()) { xpath = rs.getString("xpath"); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } return xpath; } public String getValue(String tableName,String itemName){ String value = null; String sql = "select * from " + tableName + " where name = '" + itemName + "';"; try { ResultSet rs = stat.executeQuery(sql); while (rs.next()) { value = rs.getString("value"); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } return value; }
6 測試代碼post
package com.selenium.util; import com.seleniu.objects.BaiduAccountSetting; import com.seleniu.objects.BaiduHome; import com.seleniu.objects.BaiduPersonalSetting; import com.selenium.tool.DB; public class MyBrowserTest3{ public static void main(String[] args) throws InterruptedException { MyBrowser2 myBrowser = new MyBrowser2(Config.browser); DB db = new DB(); myBrowser.navigateTo(Config.url); myBrowser.link(db.getXpath("BaiduHome", "login")).click(); Thread.sleep(3000); //登陸 myBrowser.webEdit(db.getXpath("BaiduHome", "username")).sendKeys(db.getValue("BaiduHome", "username")); myBrowser.webEdit(db.getXpath("BaiduHome", "password")).sendKeys(db.getValue("BaiduHome", "password")); myBrowser.webButton(db.getXpath("BaiduHome", "wb_login")).click(); }