慕课网web自动化测试实战之购买商品(七)

慕课网web自动化测试实战

添加购物车

需求:

1、项目实战中使用PO模型的设计与封装,详情见PO模型介绍
2、使用testng测试框架
3、使用testng-xslt生成测试报告

PO模型的基本思路:

CoursePage(查找页面元素类) —>CoursePageHandle(操作层,将查找到的元素位置上传递数据) —>CoursePageBusiness(业务层:调用操作层,根据操作层的传递的结果进行判断场景,如邮箱错误场景等) —> LoginCase(封装调用业务层,进行测试用例的场景组装)

注意:代码中调用的类和方法,见同系列四
Page层

CoursePage

package page;

import base.BaseDriver;
import base.ByLocation;
import org.openqa.selenium.WebElement;

import java.io.IOException;

/**
 * 获取商品课程页面元素封装
 */
public class CoursePage extends BasePage {
    public CoursePage(BaseDriver driver) {
        super(driver);
    }

    /**
     * 获取立即购买element
     */
    public WebElement getBuyTriggerElement() throws IOException {
        return element(ByLocation.getLocator("buyNow"));
    }
    /**
     * 获取添加购物车element
     */
    public WebElement getAddCartElement() throws IOException {
        return element(ByLocation.getLocator("addCart"));
    }
    /**
     * 获取右上角购物车element
     */
    public WebElement getShopCartElement() throws IOException{
        return element(ByLocation.getLocator("shopCart"));
    }
    /**
     * 获取购物车商品数量
     */
    public WebElement getShopCartNumElement() throws IOException {
        return element(ByLocation.getLocator("cartNum"));
    }
    /**
     * 获取课程详情页左上角的课程名element
     */
    public WebElement getCourseNameElement() throws IOException{
        return element(ByLocation.getLocator("courseInfoText"));
    }
    /**
     * 当购物车有相同课程时的弹出框获取“继续逛逛”按钮
     */
    public WebElement getReadBy() throws IOException {
        return element(ByLocation.getLocator("readybuySureNode"));
    }
}

Handle层

CoursePageHandle

package handle;

import base.BaseDriver;
import org.openqa.selenium.WebElement;
import page.CoursePage;

import java.io.IOException;

/**
 * 课程页面元素操作类handle
 */
public class CoursePageHandle{
    public BaseDriver baseDriver;
    public CoursePage coursePage;
    public CoursePageHandle(BaseDriver baseDriver){
        this.baseDriver=baseDriver;
        coursePage=new CoursePage(baseDriver);
    }
    /**
     * 点击立即购买按钮
     */
    public void clickBuyNow() throws IOException {
        coursePage.click(coursePage.getBuyTriggerElement());
    }
    /**
     * 点击添加购物车按钮
     */
    public void clickAddCart() throws IOException {
        coursePage.click(coursePage.getAddCartElement());
    }
    /**
     * 点击右上角购物车
     */
    public void clickShopCart() throws IOException {
        coursePage.click(coursePage.getShopCartElement());
    }
    /**
     * 获取购物车商品数量
     */
    public String getShopCartNum() throws IOException {
        WebElement element=coursePage.getShopCartNumElement();
        return element.getText();
    }
    /**
     * 获取商品详情页左上角课程名
     */
    public String getCourseName() throws IOException {
        WebElement element=coursePage.getCourseNameElement();
        return element.getText();
    }
    /**
     * 点击重复添加时的弹出框“继续逛逛”按钮
     */
    public void clickReadBuy() throws IOException {
        coursePage.click(coursePage.getReadBy());
    }
}

Business层

CoursePageBusiness

package business;

import base.BaseDriver;
import handle.CoursePageHandle;

import java.io.IOException;

public class CoursePageBusiness {
    public BaseDriver driver;
    public CoursePageHandle coursePageHandle;
    public CoursePageBusiness(BaseDriver baseDriver){
        this.driver=baseDriver;
        coursePageHandle=new CoursePageHandle(driver);
    }
    /**
     * 添加购物车
     */
    public void addCart() throws IOException, InterruptedException {
        //购物车商品数量逻辑处理
        int beforeNum;
        int afterNum;
        String afterCourseNum;
        String courseNum=coursePageHandle.getShopCartNum();
        try {
            beforeNum = Integer.valueOf(courseNum);
            System.out.println(beforeNum);
        }catch (Exception e){
            beforeNum=0;
        }
        //添加购物车
        coursePageHandle.clickAddCart();
        try{
//            driver.switchToMode();
            //处理弹出框,继续逛逛
            coursePageHandle.clickReadBuy();
        }catch (Exception e){}
        Thread.sleep(2000);
        afterCourseNum=coursePageHandle.getShopCartNum();
        try {
            afterNum=Integer.valueOf(afterCourseNum);
            System.out.println(afterNum);
        }catch (Exception e){
            afterNum=beforeNum;
        }
        Thread.sleep(2000);
        if (afterNum==beforeNum+1){
            System.out.println("购物车添加成功!");
            //点击购物车,进入去结算页面
            coursePageHandle.clickShopCart();
        }else if (afterNum>0){
            coursePageHandle.clickShopCart();
        }
        Thread.sleep(2000);
    }
}

Case用例

LoginCase

package testCase;

import base.BaseDriver;
import base.ByLocation;
import business.CoursePageBusiness;
import business.LoginPageBusiness;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;


/**
 * 登录慕课网自动化测试用例
 */
public class LoginCase extends CaseBase{
    public BaseDriver driver;
    public LoginPageBusiness loginPageBusiness;
    public CoursePageBusiness coursePageBusiness;
    static Logger logger= Logger.getLogger(LoginCase.class);
    public LoginCase(){
        this.driver=InitDriver("firefox");
        loginPageBusiness=new LoginPageBusiness(driver);
        coursePageBusiness=new CoursePageBusiness(driver);
    }
    //慕课网点击登录链接,进入登录界面
    @Test
    public void getLoginHome() throws InterruptedException {
        driver.get("https://www.imooc.com/");
        Thread.sleep(3000);
        //关掉广告
        //driver.findElement(By.cssSelector(".redrain-closeBtn")).click();
        //Thread.sleep(2000);
        driver.findElement(By.id("js-signin-btn")).click();
        Thread.sleep(3000);
    }
    //测试登录界面
    @Test(dependsOnMethods = {"getLoginHome"})
    public void testLogin() throws IOException, InterruptedException {
//        logger.debug("这是一条log4j日志");
        logger.info("这是一条log4j日志");
//        logger.error("这是一条log4j error日志");
        loginPageBusiness.login("[email protected]","dpl12345");

    }

    /**
     * 测试添加购物车
     */
    @Test(dependsOnMethods = {"testLogin","getLoginHome"})
    public void addCart() throws IOException, InterruptedException {
        Thread.sleep(3000);
        driver.get("https://coding.imooc.com/class/411.html");
        coursePageBusiness.addCart();
        driver.stop();
    }
    /**
     * 测试下单流程
     */
//    @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, InterruptedException {
        Thread.sleep(2000);
        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);
    }
}

配置文件:

element.properties

#courseDetil
courseInfoText=xpath>html/body/div[3]/div[1]/div[1]/span
buyNow=id>buy-trigger
addCart=className>js-addcart
shopCart=xpath>.//*[@id='shop-cart']/a/span[1]
cartNum=xpath>.//*[@id='shop-cart']/a/span[2]
sureOrder=linkText>\u63D0\u4EA4\u8BA2\u5355
shopGoPay=id>js-cart-body-table
shopGoPayNode=className>btn
order=className>order91
orderCourse=className>item-left
orderCourseNode=xpath>html/body/div[3]/div[2]/div[1]/ul/li/div[1]/dl/a/dt
orderpay=className>pay-summary-submit
alipay=className>alipay
wxpay=className>wxpay
readybuy=className>moco-modal-layer
readybuySure=className>moco-modal-inner
readybuySureNode=xpath>html/body/div[14]/div/div/div/div[2]/div
效果在这里插入图片描述
生成的Html报告:

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章