使用excel驱动的话首先要下载JXL.jar包,导入maven项目中,可以直接拖到resouces里,然后右键add as library;然后把excel文件也同样拖到resouces里,然后代码编写获取路径;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Date;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class Test {
public String time = null;//创建测试结果报告保存到本地
public void createReport() throws IOException {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
time=df.format(new Date());
File parentDir = new File("/Users/liuli/Desktop/UIreport");//报告存储的文件地址
parentDir.mkdir();
String hash = "Report_" + time;
String fileName = hash + ".txt";
File file = new File(parentDir, fileName);
try {
file.createNewFile();
System.out.print("create report successful!");
}
catch (IOException e){
throw new IOException("Exception: " + e);
}
}
//把元素以属性来分类编写入方法,然后遍历excel去读取详细的元素
public void buttonClick_xpath(String path,ChromeDriver driver) throws IOException {
try{
if (driver.findElement(By.xpath(path))!=null){
driver.findElement(By.xpath(path)).click();
writeToFile(path, "is clicked");
}
else {
writeToFile(path,"is not found");
}
}catch (IOException e){
e.printStackTrace();
}
}
/* public void buttonClick_id(String path,ChromeDriver driver) throws IOException {
try{
if (driver.findElement(By.id(path))!=null){
driver.findElement(By.id(path)).click();
<