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

慕课网web自动化测试实战

订单支付

需求:

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

PO模型的基本思路:

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

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

OrderPayPage

package page;

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

import java.io.IOException;

public class OrderPayPage extends BasePage {
    public OrderPayPage(BaseDriver driver) {
        super(driver);
    }
    /**
     * 获取订单号element
     */
    public WebElement getOrderNumElement() throws IOException {
        return element(ByLocation.getLocator("order"));
    }
    /**
     * 获取课程名称element
     */
    public WebElement getOrderCourseNameElement() throws IOException {
        return element(ByLocation.getLocator("orderCourseNode"));
    }
    /**
     * 获取支付宝element
     */
    public WebElement getAliPayElement() throws IOException {
        return element(ByLocation.getLocator("alipay"));
    }
    /**
     * 获取立即支付element
     */
    public WebElement getOrderPayElement() throws IOException {
        return element(ByLocation.getLocator("orderpay"));
    }
}

Handle层

OrderPayPageHandle

package handle;

import base.BaseDriver;
import page.OrderPayPage;

import java.io.IOException;

public class OrderPayPageHandle {
    public BaseDriver driver;
    public OrderPayPage orderPayPage;
    public OrderPayPageHandle(BaseDriver driver){
        this.driver=driver;
        orderPayPage=new OrderPayPage(driver);
    }
    /**
     * 获取订单文字
     */
    public String getOrderName() throws IOException {
        return orderPayPage.getText(orderPayPage.getOrderNumElement());
    }
    /**
     * 获取课程名称
     */
    public String getOrderCourseName() throws IOException {
        return orderPayPage.getText(orderPayPage.getOrderCourseNameElement());
    }
    /**
     * 点击支付宝支付
     */
    public void clickAliPay() throws IOException {
        orderPayPage.click(orderPayPage.getAliPayElement());
    }
    /**
     * 点击立即支付
     */
    public void clickPayElement() throws IOException {
        orderPayPage.click(orderPayPage.getOrderPayElement());
    }
}

Business层

OrderPayPageBusiness

package business;

import base.BaseDriver;
import handle.OrderPayPageHandle;

import java.io.IOException;

public class OrderPayPageBusiness {
    public BaseDriver driver;
    public OrderPayPageHandle orderPayPageHandle;
    public OrderPayPageBusiness(BaseDriver driver){
        this.driver=driver;
        orderPayPageHandle=new OrderPayPageHandle(driver);
    }
    /**
     * 根据课程名、订单号判断是否为支付页面
     */
    public void orderPayBusiness() throws IOException {
        String courseName=orderPayPageHandle.getOrderCourseName();
        String orderName=orderPayPageHandle.getOrderName();
        if (courseName!=null && orderName!=null){
            orderPayPageHandle.clickAliPay();
            orderPayPageHandle.clickPayElement();
        }
    }
}

Case用例

LoginCase

package testCase;

import base.BaseDriver;
import base.ByLocation;
import business.CoursePageBusiness;
import business.LoginPageBusiness;
import business.OrderPayPageBusiness;
import business.SureOrderPageBusiness;
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;
    public SureOrderPageBusiness sureOrderPageBusiness;
    public OrderPayPageBusiness orderPayPageBusiness;
    static Logger logger= Logger.getLogger(LoginCase.class);
    public LoginCase(){
        this.driver=InitDriver("firefox");
        loginPageBusiness=new LoginPageBusiness(driver);
        coursePageBusiness=new CoursePageBusiness(driver);
        sureOrderPageBusiness=new SureOrderPageBusiness(driver);
        orderPayPageBusiness=new OrderPayPageBusiness(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("2794974296@qq.com","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 buyNow() throws InterruptedException, IOException {
        Thread.sleep(3000);
        driver.get("https://coding.imooc.com/class/411.html");
        coursePageBusiness.buyNow();
    }

    /**
     *提交订单
     */
    @Test(dependsOnMethods = {"testLogin","getLoginHome","buyNow"})
    public void sureOrder() throws IOException {
        sureOrderPageBusiness.sureOrder();
    }
    /**
     * 立即支付
     */
    @Test(dependsOnMethods = {"testLogin","getLoginHome","buyNow","sureOrder"})
    public void orderGoPay() throws IOException, InterruptedException {
        Thread.sleep(2000);
        orderPayPageBusiness.orderPayBusiness();
    }
}

效果:在这里插入图片描述
生成报告:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值