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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值