自动化测试中用到的一些功能类
作者:网络转载 发布时间:[ 2013/9/9 14:47:28 ] 推荐标签:
6、更新EXCEL
import java.io.File;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class UpdateExcel{
public static void main(String args[]){
try{
// Excel获得文件
Workbook wb = Workbook.getWorkbook(new File("test.xls"));
// 打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook book = Workbook.createWorkbook(new File("test.xls"),wb);
// 添加一个工作表
WritableSheet sheet = book.createSheet("第二页",1);
sheet.addCell(new Label(0,0,"第二页的测试数据"));
book.write();
book.close();
}catch(Exception e){
System.out.println(e);
}
}
}
7、WebDriver判断元素是否存在
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
/*
* 判断一个元素是否存在
*/
public class ElementIsExsit {
//查找一个元素是否存在
public boolean isElementExsit(WebDriver driver, By locator) {
boolean flag = false;
try {
WebElement element=driver.findElement(locator);
//flag = true;
flag=null!=element;
System.out.println("元素: " + locator.toString()+ " 存在!");
}catch(NoSuchElementException e) {
System.out.println("元素: " + locator.toString()+ " 不存在!");
flag = false;
}
return flag;
}
//如何使用上面的方法
public void test(){
WebDriver driver = new InternetExplorerDriver();
//显性等待
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
By locator = By.id("id");
isElementExsit(driver,locator);
}
}
8、java下载图片
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class Movision_verifyImage {
private static HttpClient hc = new DefaultHttpClient();
public static void main(String args[]) throws ClientProtocolException, IOException, ParseException, URISyntaxException{
//获取图片验证码页面随机参数(当前时间)
long date = new Date().getTime();
System.out.println(date);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("random",Long.toString(date)));
get("http://172.16.3.6/admin/portalVerifyImage",params);
}
/*
* 带参数的GET请求
*
*/
public static void get(String url,List<NameValuePair> params) throws ParseException, UnsupportedEncodingException, IOException, URISyntaxException{
//get请求
HttpGet httpget = new HttpGet(url);
//设置参数
String str = EntityUtils.toString(new UrlEncodedFormEntity(params));
httpget.setURI(new URI(httpget.getURI().toString()+"?"+str));
//发送请求
HttpResponse re = hc.execute(httpget);
//获取相应实体
HttpEntity entity = re.getEntity();
if (entity != null && entity.isStreaming()) {
File storeFile = new File("F:\test.jpg");
FileOutputStream fos = new FileOutputStream(storeFile);
// 将取得的文件文件流写入目标文件
InputStream is = entity.getContent();
byte[] b = new byte[1024];
int j = 0;
while ((j = is.read(b)) != -1) {
fos.write(b, 0, j);
}
fos.flush();
fos.close();
} else {
System.out.println("[" + url + "] 未找到.");
return;
}
//关闭连接
hc.getConnectionManager().shutdown();
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11