慕課網web自動化測試實戰
賬號登錄
需求:
1、項目實戰中使用PO模型的設計與封裝,詳情見PO模型介紹
2、基類的封裝,包括:BaseDriver、ByLocation、SelectDriver
3、使用testng測試框架
4、使用log4j打印日誌,配置見博文
5、使用testng-xslt生成測試報告,配置見博文
PO模型的基本思路:
LoginPage(查找頁面元素類) —>LoginPageHandle(操作層,將查找到的元素位置上傳遞數據) —> LoginPageBusiness(業務層:調用操作層,根據操作層的傳遞的結果進行判斷場景,如郵箱錯誤場景等) —> LoginCase(封裝調用業務層,進行測試用例的場景組裝)
Page層
1、page基類BasePage
package page;
import base.BaseDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
/**
* page層:頁面元素的基類,對元素的定位、點擊、輸入、是否顯示的功能進行封裝
*/
public class BasePage {
public BaseDriver baseDriver;
public BasePage(BaseDriver driver){
this.baseDriver=driver;
}
/**
* 定位Element
*/
public WebElement element(By by){
WebElement element=baseDriver.findElement(by);
return element;
}
/**
* 元素的點擊事件
*/
public void click(WebElement element){
if (element!=null){
element.click();
}else {
System.out.println("元素不能點擊!");
}
}
/**
* 封裝輸入sendkeys
*/
public void sendKeys(WebElement element,String value){
if (element!=null){
element.sendKeys(value);
}else {
System.out.println("輸入失敗");
}
}
/**
* 判斷元素是否顯示
*/
public boolean assertElement(WebElement element){
return element.isDisplayed();
}
}
2、登錄頁面LoginPage
package page;
import base.BaseDriver;
import base.ByLocation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.io.IOException;
/**
* 功能:登錄的界面類,用於獲取元素element
*/
public class LoginPage extends BasePage{
public LoginPage(BaseDriver driver) {
super(driver);
}
//獲取用戶名輸入框element
public WebElement getUserElement() throws IOException {
return element(ByLocation.getLocator("userString"));
}
//獲取密碼輸入框element
public WebElement getPasswordElement() throws IOException {
return element(ByLocation.getLocator("passwordString"));
}
//獲取登錄按鈕element
public WebElement getLoginButtonElement() throws IOException {
return element(ByLocation.getLocator("loginButtonString"));
}
//獲取自動登錄element
public WebElement getAutoSigninElement() throws IOException {
return element(ByLocation.getLocator("autoSignin"));
}
}
handle操作層
package handle;
import base.BaseDriver;
import page.LoginPage;
import java.io.IOException;
/**
* 登錄頁面的操作類,用於輸入內容、點擊事件等
*/
public class LoginPageHandle {
public BaseDriver driver;
public LoginPage loginPage;
public LoginPageHandle(BaseDriver driver){
this.driver=driver;
loginPage=new LoginPage(driver);
}
//輸入用戶名
public void sendKeyUser(String username) throws IOException {
loginPage.sendKeys(loginPage.getUserElement(),username);
}
//輸入密碼
public void sendKeyPassword(String password) throws IOException {
loginPage.sendKeys(loginPage.getPasswordElement(),password);
}
//登錄的點擊事件
public void clickLoginButton() throws IOException {
loginPage.click(loginPage.getLoginButtonElement());
}
//選中自動登錄
public void clickAutoSignin() throws IOException {
loginPage.click(loginPage.getAutoSigninElement());
}
//判斷是否是登錄界面
public boolean assertLoginPage() throws IOException {
return loginPage.assertElement(loginPage.getUserElement());
}
}
business業務層
package business;
import base.BaseDriver;
import handle.LoginPageHandle;
import java.io.IOException;
/**
* 功能:登錄的業務層,將多個元素操作組合起來,實現一個業務功能
*/
public class LoginPageBusiness {
public LoginPageHandle handle;
public LoginPageBusiness(BaseDriver driver){
handle=new LoginPageHandle(driver);
}
public void login(String username,String password) throws IOException {
//判斷是否是登錄界面
if (handle.assertLoginPage()){
//實現業務
handle.sendKeyUser(username);
handle.sendKeyPassword(password);
handle.clickAutoSignin();
handle.clickLoginButton();
}else {
System.out.println("頁面不存在,請檢查網絡!");
}
}
}
設置基類,方便模塊調用
1、BaseDriver
package base;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import java.io.File;
import java.io.IOException;
/**
* Driver的基類
*/
public class BaseDriver {
WebDriver driver;
public BaseDriver(String browser){
SelectDriver selectDriver=new SelectDriver();
this.driver = selectDriver.driverName(browser);
}
/**
* 截圖功能
*/
public void takeScreenShot() throws IOException {
//獲取的是當前的系統時間(以時間作爲截圖的文件名)
long time=System.currentTimeMillis();
String imageTime=String.valueOf(time);
imageTime=imageTime+".png";
//獲取當前工程路徑
String currentPath=System.getProperty("user.dir");
//截圖存放路徑爲=工程路徑+文件名
String screenPath=currentPath+"/"+imageTime;
//截圖函數getScreenshotAs()來截取當前窗口
File screen=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screen,new File(screenPath)); //文件名複製到截圖
}
//關閉driver
public void stop(){
System.out.println("stop driver");
driver.close();
}
//封裝Element的方法
public WebElement findElement(By by){
WebElement element=driver.findElement(by);
return element;
}
//封裝get,打開網址
public void get(String url){
driver.get(url);
}
}
2、SelectDriver
package base;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
* 功能:瀏覽器的選擇
*/
public class SelectDriver {
public WebDriver driverName(String browser){
if (browser.equalsIgnoreCase("firefox")){
return new FirefoxDriver();
}else {
return new ChromeDriver();
}
}
}
3、ByLocation
package base;
import org.openqa.selenium.By;
import util.PropertyUtil;
import java.io.IOException;
/**
* 功能:定位方式的封裝,讀取配置文件,返回元素的屬性值
*/
public class ByLocation {
public static By getLocator(String key) throws IOException {
//使用配置文件中的html元素屬性數據(key->value)
PropertyUtil propertyUtil=new PropertyUtil("element.properties");
String locator=propertyUtil.getProperty(key);
String locatorType=locator.split(">")[0];
String locatorValue=locator.split(">")[1];
if (locatorType.equals("id")){
return By.id(locatorValue);
}else if (locatorType.equals("name")){
return By.name(locatorValue);
}else if (locatorType.equals("className")){
return By.className(locatorValue);
}else if (locatorType.equals("linkText")){
return By.linkText(locatorValue);
}else if (locatorType.equals("partialLinkText")){
return By.partialLinkText(locatorValue);
}else if(locatorType.equals("tagName")){
return By.tagName(locatorValue);
}else if (locatorType.equals("cssSelector")){
return By.cssSelector(locatorValue);
}else{
return By.xpath(locatorValue);
}
}
}
讀取配置文件的頁面元素屬性值
1、配置文件element.properties
#key->value
advert=cssSelector>.redrain-closeBtn
come_login=xpath>.//*[@id='js-signin-btn']
userString=name>email
passwordString=name>password
loginButtonString=cssSelector>.moco-btn.moco-btn-red.moco-btn-lg.btn-full.xa-login
autoSignin=id>auto-signin
headString=xpath>.//*[@id='header-avator']/img
userIDString=xpath>.//*[@id='header-user-card']/div/div/div[1]/div/a/span
2、 讀取配置文件工具類
package util;
import java.io.*;
import java.util.Properties;
/**
* 定位元素數據存入的配置文件的工具類
*/
public class PropertyUtil {
private Properties properties;
private String filePath;//配置文件的路徑
public PropertyUtil(String filePath) throws IOException {
this.filePath=filePath;
this.properties=readProperties();
}
/**
* 讀取配置文件
*/
private Properties readProperties() throws IOException {
properties=new Properties();
InputStream inputStream=new FileInputStream(filePath);
BufferedInputStream inputStream1=new BufferedInputStream(inputStream);
//load方法其實就是傳進去一個輸入流,字節流或者字符流
properties.load(inputStream);
return properties;
}
/**
* 獲取配置文件中的指定數據
*/
public String getProperty(String key) throws IOException {
if (properties.containsKey(key)) {
String userString=properties.getProperty(key);
return userString;
}else {
System.out.println("你獲取的key不存在");
return "";
}
}
}
設計測試用例代碼
1、BaseCase
package testCase;
import base.BaseDriver;
/**
* 功能:用例的基類,初始化Driver
*/
public class CaseBase {
public BaseDriver InitDriver(String browser){
return new BaseDriver(browser);
}
}
2、登錄測試用例LoginCase
package testCase;
import base.BaseDriver;
import business.LoginPageBusiness;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
import java.io.IOException;
/**
* 登錄慕課網自動化測試用例
*/
public class LoginCase extends CaseBase{
public BaseDriver driver;
public LoginPageBusiness loginPageBusiness;
static Logger logger= Logger.getLogger(LoginCase.class);
public LoginCase(){
this.driver=InitDriver("firefox");
loginPageBusiness=new LoginPageBusiness(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");
Thread.sleep(2000);
driver.stop();
}
}