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

慕课网web自动化测试实战

提交订单

需求:

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

PO模型的基本思路:

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

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

SureOrderPage

package page;

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

import java.io.IOException;

public class SureOrderPage extends BasePage {
    public SureOrderPage(BaseDriver driver) {
        super(driver);
    }
    /**
     * 获取提交订单按钮
     */
    public WebElement getSubmitOrderElement() throws IOException {
        return element(ByLocation.getLocator("sureOrder"));
    }
}

Handle层

SureOrderPageHandle

package handle;

import base.BaseDriver;
import page.SureOrderPage;

import java.io.IOException;

public class SureOrderPageHandle {
    public BaseDriver driver;
    public SureOrderPage sureOrderPage;
    public SureOrderPageHandle(BaseDriver baseDriver){
        this.driver=baseDriver;
        sureOrderPage=new SureOrderPage(driver);
    }
    /**
     * 点击提交订单按钮
     */
    public void clickSubmitOrderElement() throws IOException {
        sureOrderPage.click(sureOrderPage.getSubmitOrderElement());
    }
}

Business层

SureOrderPageBusiness

package business;

import base.BaseDriver;
import handle.SureOrderPageHandle;

import java.io.IOException;

public class SureOrderPageBusiness {
    public BaseDriver driver;
    public SureOrderPageHandle sureOrderPageHandle;
    public SureOrderPageBusiness(BaseDriver baseDriver){
        this.driver=baseDriver;
        sureOrderPageHandle=new SureOrderPageHandle(driver);
    }
    /**
     * 提交订单
     */
    public void sureOrder() throws IOException {
        sureOrderPageHandle.clickSubmitOrderElement();
    }
}

Case用例

LoginCase

package testCase;

import base.BaseDriver;
import base.ByLocation;
import business.CoursePageBusiness;
import business.LoginPageBusiness;
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;
    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);
    }
    //慕课网点击登录链接,进入登录界面
    @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();
    }
}

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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值