本人在学习使用selenium2java的过程中,遇到过需要测试方案购买的问题,每次买完之后都得手动清除一下数据,重新测试购买流程。在写自动化用例的时候用到了数据库相关方法,分享一下,供参考。
//购买志愿方案(使用兑换卡)
public static void buyWishProjectByCoupon(WebDriver driver) throws ClassNotFoundException, SQLException, IOException, InterruptedException {
MySql.UpdateOrderStutas(user_id);//使用户当前已购买订单过期
findElementByIdAndClick(driver, "btnIndexPay");//点击购买
findElementByXpathAndClick(driver, ".//*[@id='payment_channel']/li[1]");//点击兑换卡购买
findElementByIdAndClick(driver, "btnOrderCreatePay");//点击立即支付
/*如果用户之前购买过,则在第一行设置为过期
* 如果用户之前生成过订单,会提示雷同,跳转订单页面,继续选择兑换卡支付
* 如果用户没有购买过或者订单全部过期,直接使用兑换卡支付
*/
if (exists(driver, By.className("confirm"))) {
sleep(0);
findElementByClassNameAndClick(driver, "confirm");
findElementByTextAndClick(driver, "立即支付");
findElementByIdAndClick(driver, "btnOrderCreatePay");//点击立即支付
payByCoupon(driver);
}else{
payByCoupon(driver);
}
driver.get("http://172.21.134.15:5555/purchase/order/list");//跳转我的订单页面
refresh(driver);//强制刷新
String status = getTextByClassName(driver, "pull-right");//获取最新订单状态
assertTrue("购买课程失败!", status.equals("已付款"));
}
下面是使用兑换卡的方法:
//兑换卡支付
public static void payByCoupon(WebDriver driver) throws ClassNotFoundException, SQLException {
String coupon_id = MySql.getCouponIdAndPassword();//获取帐号
findElementByIdAndClearSendkeys(driver, "input-coupon_number", coupon_id);//输入帐号
findElementByIdAndClearSendkeys(driver, "input-coupon_password", "123456");//输入密码
findElementByXpathAndClick(driver, ".//*[@id='modal-pay_card']/div/div/div[2]/div/div/div[2]/div/div[4]/div/button");//点击支付
}
下面是修改用户订单状态的方法:
//修改用户订单状态
public static void UpdateOrderStutas(int id) throws ClassNotFoundException, SQLException, IOException {
// 加载驱动程序
Class.forName(driver);
// 连接数据库
Connection connection = DriverManager.getConnection(url, user, password);
if(!connection.isClosed()){
Statement statement = connection.createStatement();
//1是代付款,2是已付款,5是已过期
String sql = "UPDATE op_orders SET order_status = 5 where user_id = "+id+" and order_status= 2";
statement.executeUpdate(sql);
connection.close();
}else {
output("failed connecting to the Database!");
}
}
下面是获取尚未使用的兑换卡的方法:
//查找尚未使用的兑换卡帐号和密码
public static String getCouponIdAndPassword() throws SQLException, ClassNotFoundException {
String coupon_id = null;
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
if (!connection.isClosed()) {
Statement statement = connection.createStatement();
String sql = "SELECT * FROM op_coupon LEFT JOIN op_coupon_used on op_coupon.coupon_id = op_coupon_used.coupon_id WHERE op_coupon_used.order_no is null limit 1";
ResultSet result = statement.executeQuery(sql);
// output("帐号"+"\t"+"密码");
while(result.next()){
coupon_id = result.getString("serial_no");
// output(result.getString("serial_no")+"\t"+result.getString("serial_password"));
}
result.close();
connection.close();
}
return coupon_id;
}