好久没发过帖子了,最近公司主力做微站了,以前 appium 的框架改了改,主要用 selenium 的 webdriver 来实现。好了说下主要的设计思路吧
接下来还是贴上一些相关代码
1. 可以设定多轮测试,每轮测试数据都不一样,测试数据来源于 excel 的 sheet_2
/**设定测试轮次**/
public static void main() {
try{
for(k=0;k<numTotal;k++){
File file = new File("e:\\data.xls"); //采用的是jxl.jar读取写入表格方式
InputStream in = new FileInputStream(file); //新建一可读取本地内容的文件
Workbook workbook = null;
workbook = Workbook.getWorkbook(in); //获取本地可读取的文件
mWorkBook = workbook;
getExcel();
Sheet1DataList.clear();
/**获取cookies**/
for (Cookie ck : driver.manage().getCookies()) {
String CK = ck.getName() + "=" + ck.getValue() + ";";
System.out.println("cookie:" + CK);
// sendGet(CK);
}
}
}
2. 从 excel 获取 webdriver 关键字以及相关测试用例数据
private static void getExcel() {
try{
getTestSettingFromSheet1();
getDataFromSheet2();
Thread.sleep(1000);
sqlDriver.findElement(By.name("auth[password]")).sendKeys("test@628");;
sqlDriver.findElement(By.cssSelector("#content > form > p > input[type='submit']")).click();
sql = sqlDriver.findElement(By.tagName("pre"));
if(Sheet0MethodList.size() != Sheet0ItemList.size()){
System.out.println("Input Sheet Value Wrong!!!!Please Check Your Sheet" );
}
for(int listId=0;listId<Sheet0ActionList.size();listId++){
System.out.println("Sheet0ActionList.size:" + Sheet0ActionList.size());
System.out.println("OK,Begin Commit!!!" );
Thread.sleep(500);
if(Sheet0PlatList.get(listId).equals("后台"))
driver = backDriver;
loginBtn = getMethods(Sheet0MethodList.get(listId),Sheet0ItemList.get(listId),driver);
String strCmd = Sheet0CheckList.get(listId);
String strAct = Sheet0ActionList.get(listId);
getAssert(strAct,strCmd,listId);
//InputCmdId++;
}
}
3.将测试结果保存到 excel 中
private static void getAssert(String getStrAct,String getStrCmd,int getListId){
try{
Workbook wb=Workbook.getWorkbook(new File("e:\\data.xls")); //获取本地路径的excel文件
WritableWorkbook book= Workbook.createWorkbook(new File("e:\\data.xls"),wb); //将本地已存在的可读文件转成可写文件
WritableSheet sheet0_1=book.getSheet(0);
if(getStrAct.equals("frame")){
Thread.sleep(1000);
driver.switchTo().frame(loginBtn);
}
if(getStrAct.equals("back"))
driver.navigate().back();
if(getStrCmd.equals("输入值获取")){
Thread.sleep(1000);
String text = Sheet1DataList.get(getInputCmdId);
System.out.println("SenKeys:"+text);
loginBtn.sendKeys(text);
Thread.sleep(1000);
if(loginBtn.getAttribute("value").equals(text)){
System.out.println("成功numTotal :" + numTotal);
sheet0_1.addCell(new Label(10+numTotal+k,getListId+1,"true"));
Cell AI = sheet0_1.getCell(10+numTotal+k,getListId+1);
String strAI = AI.getContents();
System.out.println(">>>>>>>>>>>>" + strAI);
}
else{
System.out.println("失败 numTotal:" + numTotal);
sheet0_1.addCell(new Label(10+numTotal+k,getListId+1,"false"));
}
getInputCmdId++;
}
if(getStrCmd.equals("文本校验")){
loginBtn.click();
Thread.sleep(1000);
isContentAppeared(driver,Sheet0ReseltList.get(clickId));
System.out.println("status:" + status);
String clickResult = String.valueOf(status);
sheet0_1.addCell(new Label(10+numTotal+k,getListId+1,clickResult));
clickId++;
}
if(getStrCmd.equals("数据库校验")){
String currentUrl = driver.getCurrentUrl();
System.out.println("当前页面url---:" + currentUrl);
String[] urlList = currentUrl.split("\\/");
String[] urlArry = urlList[urlList.length-1].split("\\.");
String orderId = urlArry[0];
System.out.println("订单ID号:" + orderId);
sql.clear();
sql.sendKeys("select * from order_info where order_id = " + orderId);
sqlDriver.findElement(By.cssSelector("#form > p:nth-child(2) > input[type='submit']:nth-child(1)")).click();
String sqlStr = "无数据";
isContentAppeared(sqlDriver,sqlStr);
System.out.println("status:" + status);
String clickResult = String.valueOf(status);
if(clickResult.equals("false")){
clickResult = "true";
}else{
clickResult = "false";
}
sheet0_1.addCell(new Label(10+numTotal+k,getListId+1,clickResult));
Thread.sleep(1000);
loginBtn.click();
// clickId++;
}
book.write();
book.close();
}