selenium2java 一個利用mysql獲取驗證碼註冊新用戶的測試用例

本人在學習selenium2java的時候,需要寫一個註冊模塊的用例,想了一下決定用一個虛擬的手機號和mysql數據庫直接進行註冊流程,然後再去數據庫把這個賬號刪了。實驗了一下,感覺還不錯。分享出來,供大家參考。

public static void registerUserAndDelete(WebDriver driver) throws ClassNotFoundException, SQLException, IOException, InterruptedException {
String mobile = "13578965425";
findElementByIdAndClearSendkeys(driver, "UserName", mobile);//輸入手機號
findElementByIdAndClick(driver, "unit_t");//選擇學校
findElementByIdAndClick(driver, "410000");//選擇省
findElementByIdAndClick(driver, "411700");//選擇市
findElementByIdAndClick(driver, "411721");//選擇縣
findElementByIdAndClick(driver, "28342");//選擇學校
findElementByPartiaTextAndClick(driver, "點擊獲取驗證碼");
String code = getMobileCode(mobile);//獲取驗證碼
findElementByIdAndClearSendkeys(driver, "Vcode", "Vcode", code);//輸入驗證碼
findElementByIdAndClick(driver, "T_UserPasswordA");//輸入密碼
findElementByIdAndClick(driver, "T_UserPasswordB");//確認密碼
findElementByIdAndClick(driver, "RegPostBtn");//確認註冊
sleep(2);
deleteUserByMobile(mobile);//刪除用戶
}


下面是我自定義的方法:

//根據id獲取元素清除文本寫入文本
public static void findElementByIdAndClearSendkeys(WebDriver driver, String id1 , String id2, String text) {
driver.findElement(By.id(id1)).clear();
driver.findElement(By.id(id2)).sendKeys(text);
}
public static void findElementByIdAndClearSendkeys(WebDriver driver, String id, String text) {
driver.findElement(By.id(id)).clear();
driver.findElement(By.id(id)).sendKeys(text);
}
//根據文本模糊查找
public static void findElementByPartiaTextAndClick(WebDriver driver, String text) {
driver.findElement(By.partialLinkText(text)).click();
}
//獲取短信驗證碼
public static String getMobileCode(String mobile) throws ClassNotFoundException, SQLException, IOException {
//start 驅動程序名
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 = "efa803254d";
System.out.println("-----------------");
// 加載驅動程序
Class.forName(driver);
// 連續數據庫
Connection conn = DriverManager.getConnection(url, user, password);
//end
if(!conn.isClosed()) 
System.out.println("Succeeded connecting to the Database!");
//statement用來執行SQL語句
Statement statement = conn.createStatement();
// 要執行的SQL語句
String sql = "select * from verify where mobile = "+ mobile + " ORDER BY create_time DESC LIMIT 1";
            output(sql);
// 結果集
ResultSet rs = statement.executeQuery(sql);
            System.out.println("查詢結果如下所示:");
            System.out.println("驗證碼ID" + "\t" + "驗證碼");
            String code = null;
            while(rs.next()) {
            // 選擇列數據
            code = rs.getString("verify");
            // 輸出結果
            System.out.println(rs.getString("id") + "\t" + code);
            saveToFile(getNow()+rs.getString("id") + "\t" + code, "runlog.log", false);
            }
            rs.close();
            conn.close();
            return code;
}
//刪除用戶
public static void deleteUserByMobile(String mobile) throws ClassNotFoundException, SQLException, IOException {
//start 驅動程序名
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 = "efa803254d";
// 加載驅動程序
Class.forName(driver);
// 連續數據庫
Connection conn = DriverManager.getConnection(url, user, password);
//end
if(!conn.isClosed()){
output("Succeeded connecting to the Database!");
//statement用來執行SQL語句
Statement statement = conn.createStatement();
// 要執行的SQL語句
String sql = "delete from users where mobile = "+ mobile;
            output(sql);
statement.executeUpdate(sql);
saveToFile(getNow()+sql, "runlog.log", false);
            conn.close();
            }else {
            output("failed connecting to the Database!");
            }
}
//寫入日誌文件
public static void saveToFile(String text, String path, boolean isClose) throws IOException {
File file = new File("runlog.log");
BufferedWriter bf = null;
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();
}
}


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