慕課網web自動化測試實戰之購買商品(六)

慕課網自動化測試實戰

購買商品

需求:
  1. 實現簡單的商品購買流程:登錄->立即購買->提交訂單->立即支付
腳本代碼(代碼中調用的類和方法,請參考之前的文章)
/**
     * 下單流程
     */
    @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();
//        this.getOrder();
        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();
    }

    //獲取element
    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 getOrder() throws IOException {
//        return this.getCourseText(this.getElement(ByLocation.getLocator("order")));
//    }

    /**
     * 功能:立即支付,並獲取支付中心商品信息
     */
    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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章