慕课网自动化测试实战
购买商品
需求:
实现简单的商品购买流程:登录->立即购买->提交订单->立即支付
脚本代码(代码中调用的类和方法,请参考之前的文章)
@Test ( dependsOnMethods = { "testLogin" , "getLoginHome" } )
public void downOrder ( ) throws IOException, InterruptedException {
Thread. sleep ( 3000 ) ;
driver. get ( "https://coding.imooc.com/class/411.html" ) ;
String currentText= this . buyCourseNow ( ) ;
System. out. println ( "课程页面信息为:" + currentText) ;
this . sureOrder ( ) ;
String orderCourseText= this . getOrderCourse ( ) ;
System. out. println ( "订单页面课程信息为:" + orderCourseText) ;
if ( currentText. equals ( orderCourseText) ) {
System. out. println ( "下单成功!" ) ;
}
Thread. sleep ( 2000 ) ;
driver. stop ( ) ;
}
public String getCourseText ( WebElement element) {
return element. getText ( ) ;
}
public WebElement getElement ( By by) {
return driver. findElement ( by) ;
}
public String buyCourseNow ( ) throws IOException {
WebElement elementNode= this . getElement ( ByLocation. getLocator ( "courseInfoText" ) ) ;
String currentText= this . getCourseText ( elementNode) ;
driver. click ( this . getElement ( ByLocation. getLocator ( "buyNow" ) ) ) ;
return currentText;
}
public void sureOrder ( ) throws IOException {
driver. click ( this . getElement ( ByLocation. getLocator ( "sureOrder" ) ) ) ;
}
public String getOrderCourse ( ) throws IOException {
Thread. sleep ( 3000 ) ;
WebElement elementNode= this . getElement ( ByLocation. getLocator ( "orderCourseNode" ) ) ;
driver. click ( this . getElement ( ByLocation. getLocator ( "wxpay" ) ) ) ;
driver. click ( this . getElement ( ByLocation. getLocator ( "orderpay" ) ) ) ;
return this . getCourseText ( elementNode) ;
}
}
效果:
控制台输出:
课程页面信息为:全面系统学测试 从小白入门到实战就业
JavaScript warning: https://order.imooc.com/static/lib/util/placeholder.js?v=202003111810, line 101: unreachable code after return statement
JavaScript warning: https://order.imooc.com/static/lib/backbone/backbone-1.0.0.min.js?v=202003111810, line 3: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
订单页面课程信息为:全面系统学测试 从小白入门到实战就业
下单成功!
stop driver