您的位置:首页 > 其它

selenium测试框架篇

2016-05-31 11:27 239 查看
做自动化框架,不可避免的就是对象库。

有一个好的对象库,可以让整个测试体系:

更容易维护

大大增加代码重用

增加测试系统的稳定性

这里先了解一下我所说的对象库:

package com.dbyl.libarary.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

public class ReadExcelUtil {

static String path;

/**
* @author Young
* @return
* @throws IOException
*/
public static String[][] getLocatorMap() throws IOException {
path = "C:/Users/Young/workspace/Demo/src/com/dbyl/libarary/pageAction/UILibrary.xls";
File f1 = new File(path);
FileInputStream in = new FileInputStream(f1);
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(in));
Sheet sheet = wb.getSheetAt(0);
Row header = sheet.getRow(0);
String[][] locatorMap = new String[sheet.getLastRowNum() + 1][header
.getLastCellNum()];
for (int rownum = 0; rownum <= sheet.getLastRowNum(); rownum++) {
// for (Cell cell : row)
Row row = sheet.getRow(rownum);

if (row == null) {

continue;

}
String value;
for (int cellnum = 0; cellnum <= row.getLastCellNum(); cellnum++) {
Cell cell = row.getCell(cellnum);
if (cell == null) {
continue;
} else {
value = "";
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
value = cell.getDateCellValue().toString();

} else {
value = Double.toString((int) cell
.getNumericCellValue());

}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = Boolean.toString(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
value = cell.getCellFormula().toLowerCase();
break;
default:
value = " ";
System.out.println();
}
locatorMap[rownum][cellnum] = value;

}
}
in.close();
wb.close();

return locatorMap;
}

}


页面类

package com.dbyl.libarary.pageAction;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import com.dbyl.libarary.utils.BasePage;
import com.dbyl.libarary.utils.Locator;

public class LoginPage extends BasePage {

WebDriver driver;

public WebDriver getDriver() {
return driver;
}

public LoginPage(WebDriver driver) throws IOException {
super(driver);
driver.get("http://www.zhihu.com/#signin");
}

Locator loginEmailInputBox = new Locator("loginEmailInputBox");

Locator loginPasswordInputBox = new Locator("loginPasswordInputBox");
Locator loginButton = new Locator("loginButton");
Locator profile = new Locator(
"profile");

public void typeEmailInputBox(String email) throws Exception {
type(loginEmailInputBox, email);
}

public void typePasswordInputBox(String password) throws Exception {
type(loginPasswordInputBox, password);
}

public void clickOnLoginButton() throws Exception {
click(loginButton);
}

public boolean isPrestentProfile() throws IOException {
return isElementPresent(profile, 20);

}

public void waitForPageLoad() {
super.getDriver().manage().timeouts()
.pageLoadTimeout(30, TimeUnit.SECONDS);
}

}


接下来就是登陆的Action

package com.dbyl.libarary.action;

import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import com.dbyl.libarary.pageAction.HomePage;
import com.dbyl.libarary.pageAction.LoginPage;

public class CommonLogin {

private static WebDriver driver;

public static WebDriver getDriver() {
return driver;
}

static LoginPage loginPage;

public static HomePage login(String email, String password)
throws Exception {
loginPage = new LoginPage(getDriver());
loginPage.waitForPageLoad();
loginPage.typeEmailInputBox(email);
loginPage.typePasswordInputBox(password);
loginPage.clickOnLoginButton();
Assert.assertTrue(loginPage.isPrestentProfile(), "login failed");
return new HomePage(getDriver());
}

public static HomePage login() throws Exception {
return CommonLogin.login("seleniumcookies@126.com", "cookies123");
}

public static void setDriver(WebDriver driver) {
CommonLogin.driver = driver;
}

}


至此为止,已经封装完毕

接下来就能在测试用例直接调用者

  

demo的下载地址:https://github.com/tobecrazy/Demo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: