慕課網自動化測試實戰
購買商品
需求:
實現簡單的商品購買流程:登錄->立即購買->提交訂單->立即支付
腳本代碼(代碼中調用的類和方法,請參考之前的文章)
@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