7_Selenium结合数据库sqlite

1 sqlite下载及安装

  • 地址:http://www.sqlite.org/download.html
  • 安装:解压sqlite-tools-win32-x86-3100200到C:\sqlite
  • 配置环境变量:path中加入C:\sqlite

2 安装firefox插件sqliteManager

3 新建数据库表

 

4 添加sqlite jar包

  • sqlitejdbc-v033-nested.jar

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 测试代码

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();
        
    }

 

栗子测试

  • 所有文章均为原创,是栗子测试所有人员智慧的结晶,如有转载请标明出处
  • 如果您在阅读之后觉得有所收获,请点击右下角推荐
  • QQ:2472471982,欢迎大家前来咨询和探讨(暗号:栗子测试)

 

 

posted on 2016-01-24 22:30  栗子测试  阅读(888)  评论(0编辑  收藏  举报