慕課網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