7_Selenium結合數據庫sqlite

1 sqlite下載及安裝html

  • 地址:http://www.sqlite.org/download.html
  • 安裝:解壓sqlite-tools-win32-x86-3100200到C:\sqlite
  • 配置環境變量:path中加入C:\sqlite

2 安裝firefox插件sqliteManagerjava

3 新建數據庫表web

 

4 添加sqlite jar包sql

  • 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 測試代碼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();
        
    }

 

栗子測試

  • 全部文章均爲原創,是栗子測試全部人員智慧的結晶,若有轉載請標明出處
  • 若是您在閱讀以後以爲有所收穫,請點擊右下角推薦
  • QQ:2472471982,歡迎你們前來諮詢和探討(暗號:栗子測試)
相關文章
相關標籤/搜索